ansiblenetwork-programmingjunos-automation

How can you pass regex to Junipers match command?


I'm looking to limit the returned data from a Juniper switch in Ansible and I don't see to be able to get the match command passed across to the device.

On the device I can run;

show vlans | match "(\d+)"

This returns a list of all active vlans on the device, however ignores the interfaces set to each one. I want to return this same result using Ansible, however when I passed it the following code, it returns the entire data set for show vlans.

    - name: Gathering active vlans..
      junipernetworks.junos.junos_command:
        commands:
          - "show vlans | match '(\\d+)'"
      register: vlan_list
      tags:
        - core
        - access
    - name: test
      debug:
        msg: "{{vlan_list}}"
      tags:
        - always

How can I update my playbook to ensure the match regex is sent to the device?


Solution

  • I'm not entirely sure since I don't have a Juniper device and never used that module... but I'd put a dime on the fact it does not support pipes as part of the command. I did not find any mention of that in the current documentation but it is specified on an older version of the documentation on juniper website

    Meanwhile, unless gathering the entire set of vlans is extremely resource intensive, it's quite easy to do the filtering in ansible:

        - name: Gathering active vlans..
          junipernetworks.junos.junos_command:
            commands:
              - "show vlans"
          register: vlan_list
    
        - name: Show numeric vlans only
          debug:
            msg: "{{ vlan_list.stdout_lines | select('match', '\\d+') }}"
    

    Note that the above is only answering your direct question. I suspect that a much better way to get this information is through the junos_facts module