2

I want to pull out the lines of a Juniper config that assign an IPv6 address to an interface and save that output to a file.

The output I am after is generated with the command 'show configuration | display set| match "inet6 address" '

I'm building an Ansible playbook and have pinballed off of errors to end up with the below task. It is basically giving me the complete interface configs, and I just want to narrow it down to the lines that would fit the "match" line in the manual command. The commented out filters aren't working and I can't find documentation that explains filters in a way that I understand.

- name: "Get selected configuration hierarchies"
  juniper_junos_config:
    host: "{{ ansible_host }}"
    retrieve: "committed"
    config_mode : "private"
    filter: "<configuration><interfaces/></configuration>"
    #filter: "<configuration><interfaces/><family/><inet6/><address/></configuration>"
    #filter: "inet6/address"
    format: "set"
    options:
      inherit: "inherit"
    dest: "{{ inventory_hostname }}-inet6-config"
  register: response
- name: "Print result"
  debug:
    var: response

Output:

ok: [LAB-QFX5110-1] => {
    "response": {
        "changed": false,
"config": "\nset interfaces xe-0/0/0 apply-groups-except jumbo-frames\nset interfaces xe-0/0/0 description \"Test Laptop - DMZ;000\"\nset interfaces xe-0/0/0 gigether-options 802.3ad ae12\n<SNIP>\nset interfaces lo0 unit 10 family inet address 100.126.0.x/32\nset interfaces lo0 unit 10 family inet6 address ABCD:EF00:0000:01c4::1/128\n<SNIP>/n",
"config_lines": [
            "",
            "set interfaces xe-0/0/0 apply-groups-except jumbo-frames",
            "set interfaces xe-0/0/0 description \"Test Laptop - DMZ;000\"",
            "set interfaces xe-0/0/0 gigether-options 802.3ad ae12",
            "<SNIP>",
            "set interfaces lo0 unit 10 family inet address 100.126.0.x/32",
            "set interfaces lo0 unit 10 family inet6 address ABCD:EF00:0000:01c4::1/128",
            "<SNIP>",
        ],
        "failed": false,
        "msg": "Configuration has been: opened, retrieved, closed."
    }
}

I just want the lines that read:

set interfaces unit X family inet6 address XXXX:YYYY:ZZZZ:1234::1/127

But I can't seem to plug in the correct filter.

I will also mention that if there is a better way to gather this, I am open to exploring it. It just seems like this is the task Ansible was created to perform.

  • not familiar with Juniper ansible module, but do you think you could show us the `response` variable structure and contents? it would help provide (hopefully) an answer to your problem. assuming your task is the optimal one, my goal would be to try parse the `response` variable, and catch the lines containing the `inet6` string. – ilias-sp Aug 29 '19 at 21:40
  • i presume the output of the variable is loooooooong. in this case, then just its structure - so we can see the fields it contains - would suffice. – ilias-sp Aug 29 '19 at 21:45
  • I have edited the question to provide the requested output. If I am reading your comments correctly, you are saying I have to now program Ansible to process the output? – penfold1972 Aug 30 '19 at 13:07
  • yes, please see answer on how you can parse the `response` and get the lines you need. – ilias-sp Aug 30 '19 at 13:48

1 Answers1

0

here is how to do it. since your response dictionary contains the output split by lines, we will use the config_lines key, process line by line:

code:

---
- hosts: localhost
  gather_facts: false
  vars:
    response:
      changed: false
      config: |2-

        set interfaces xe-0/0/0 apply-groups-except jumbo-frames
        set interfaces xe-0/0/0 description "Test Laptop - DMZ;000"
        set interfaces xe-0/0/0 gigether-options 802.3ad ae12
        <SNIP>
        set interfaces lo0 unit 10 family inet address 100.126.0.x/32
        set interfaces lo0 unit 10 family inet6 address ABCD:EF00:0000:01c4::1/128
        <SNIP>/n
      config_lines:
      - ''
      - set interfaces xe-0/0/0 apply-groups-except jumbo-frames
      - set interfaces xe-0/0/0 description "Test Laptop - DMZ;000"
      - set interfaces xe-0/0/0 gigether-options 802.3ad ae12
      - "<SNIP>"
      - set interfaces lo0 unit 10 family inet address 100.126.0.x/32
      - set interfaces lo0 unit 10 family inet6 address ABCD:EF00:0000:01c4::1/128
      - "<SNIP>"
      failed: false
      msg: 'Configuration has been: opened, retrieved, closed.'

  tasks:

  - name: find entries containing inet6 address, add to results
    set_fact:
      interfaces: "{{ interfaces | default([]) + [item] }}"
    when: item is search('inet6 address')
    with_items:
    - "{{ response.config_lines }}"

  - debug:
      var: interfaces

  - name: save results to file
    template:
      src: results.j2
      dest: /tmp/results.out

you will need a jinja2 template for the last task to work (under same dir as your playbook), with contents:

results.j2:

# processing results:

{% for interface in interfaces -%}
{{ interface }}
{%- endfor %}

1st task parses each line and if the when condition is met, its adding the given line to a results , the interfaces list. 2nd task prints that variable. 3rd task saves the results to a file under /tmp/.

hope it helps

ilias-sp
  • 6,135
  • 4
  • 28
  • 41
  • This is SO close to what I was looking for. I am hoping to save the "interfaces" output into a file. When I add the "dest: " line to the task, it seems to invalidate the whole task. I figure I've got the syntax wrong, but I cannot figure it out. – penfold1972 Aug 30 '19 at 18:21
  • have a look at [template module](https://docs.ansible.com/ansible/latest/modules/template_module.html?highlight=template). plz see updated answer – ilias-sp Aug 30 '19 at 18:35
  • 1
    After I took out the minus sign in the template, I got exactly the output I was looking for. Thanks! – penfold1972 Aug 30 '19 at 19:05