ansiblecentos7junos-automation

Gather a List of Configured IP Addresses


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.


Solution

  • 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