ansiblejuniper

Get specific items out of junos list in Ansible


ansible version: ansible 2.7.7
Juniper.junos 2.1.0

My problem is the I have an array of interfaces a switch in which I would like to find only the physical interfaces (e.g. ge-0/0/0 or ge-0/0/14 or xe-0/0/3)

I have tried

msg: '{{inter.stdout_lines | regex_search("(ge)") }}

msg: '{{inter.stdout_lines is search("ge") }}'

msg: '{{inter.stdout_lines | search("ge") }}'

Yaml file:

---
- name: Get device uptime
  hosts:
    - switches
    - all
  connection: local
  roles:
    - Juniper.junos
  gather_facts: no

  tasks:
    - name: Get uptime
      juniper_junos_command:
        commands:
          - show interfaces terse
      register: inter

    - name: test iner stdout_lines
      debug:
        msg: "{{inter.stdout_lines}}"

    - name: Print results
      debug:
        var: inter.stdout_lines
        msg: '{{inter | regex_search("(ge)") }}'

The output of inter.stdout_lines is: I know that it will return some thing so I know the it isn't empty. However I'm not sure on what type inter.stdout_lines returns.

TASK [test iner stdout_lines] ********************
ok: [192.168.10.254] => {
    "msg": [
        "",
        "Interface               Admin Link Proto    Local                 Remote",
        "ge-0/0/0                up    down",
        "ge-0/0/0.0              up    down eth-switch",
        "pfe-0/0/0               up    up",
        "pfe-0/0/0.16383         up    up   inet    ",
        "                                   inet6   ",
        "pfh-0/0/0               up    up",
        "pfh-0/0/0.16383         up    up   inet    ",
        "ge-0/0/1                up    down",
        "ge-0/0/1.0              up    down eth-switch",
        "ge-0/0/2                up    down",
        "ge-0/0/2.0              up    down eth-switch",
        "ge-0/0/3                up    down",
        "ge-0/0/3.0              up    down eth-switch",
        "ge-0/0/4                up    up",
        "ge-0/0/4.0              up    up   eth-switch",
        "ge-0/0/5                up    down",
        "ge-0/0/5.0              up    down eth-switch",
        "ge-0/0/6                up    down",
        "ge-0/0/6.0              up    down eth-switch",
        "ge-0/0/7                up    down",
        "ge-0/0/7.0              up    down eth-switch",
        "ge-0/0/8                up    down",
        "ge-0/0/8.0              up    down eth-switch",
        "ge-0/0/9                up    down",
        "ge-0/0/9.0              up    down eth-switch",
        "ge-0/0/10               up    down",
        "ge-0/0/10.0             up    down eth-switch",
        "ge-0/0/11               up    down",
        "ge-0/0/11.0             up    down eth-switch",
        "ge-0/0/12               up    down",
        "ge-0/0/12.0             up    down eth-switch",
        "ge-0/0/13               up    down",
        "ge-0/0/13.0             up    down eth-switch",
        "ge-0/0/14               up    down",
        "ge-0/0/14.0             up    down eth-switch",
        "ge-0/0/15               up    down",
        "ge-0/0/15.0             up    down eth-switch",
        "ge-0/0/16               up    down",
        "ge-0/0/16.0             up    down eth-switch",
        "ge-0/0/17               up    down",
        "ge-0/0/17.0             up    down eth-switch",
        "ge-0/0/18               up    down",
        "ge-0/0/18.0             up    down eth-switch",
        "ge-0/0/19               up    down",
        "ge-0/0/19.0             up    down eth-switch",
        "ge-0/0/20               up    down",
        "ge-0/0/20.0             up    down eth-switch",
        "ge-0/0/21               up    down",
        "ge-0/0/21.0             up    down eth-switch",
        "ge-0/0/22               up    down",
        "ge-0/0/22.0             up    down eth-switch",
        "ge-0/0/23               up    down",
        "ge-0/0/23.0             up    down inet     192.168.254.253/30",
        "bme0                    up    up",
        "bme0.0                  up    up   inet     128.0.0.1/2     ",
        "                                            128.0.0.4/2     ",
        "                                            128.0.0.16/2    ",
        "                                            128.0.0.63/2    ",
        "cbp0                    up    up",
        "dsc                     up    up",
        "esi                     up    up",
        "gre                     up    up",
        "ipip                    up    up",
        "irb                     up    up",
        "irb.0                   up    down",
        "irb.3                   up    down inet     192.168.3.254/24 ",
        "irb.10                  up    up   inet     192.168.10.254/24",
        "irb.11                  up    down inet     192.168.11.254/24",
        "irb.12                  up    down inet     192.168.12.254/24",
        "irb.13                  up    up   inet     192.168.13.254/24",
        "irb.14                  up    down inet     192.168.14.254/24",
        "irb.23                  up    down inet     192.168.23.254/24",
        "irb.88                  up    down inet     192.168.88.254/24",
        "jsrv                    up    up",
        "jsrv.1                  up    up   inet     128.0.0.127/2   ",
        "lo0                     up    up",
        "lo0.16385               up    up   inet    ",
        "lsi                     up    up",
        "me0                     up    down",
        "me0.0                   up    down eth-switch",
        "mtun                    up    up",
        "pimd                    up    up",
        "pime                    up    up",
        "pip0                    up    up",
        "tap                     up    up",
        "vme                     up    down",
        "vme.0                   up    down",
        "vtep                    up    up"
    ]
}

And when the task Print results runs it returns an error: From what I can tell this error is either a empty variable or wrong type but I do not know how to change the type of the array.

TASK [Print results] ******************
fatal: [192.168.10.254]: FAILED! => {"msg": "Unexpected templating type error occurred on ({{inter | regex_search(\"(ge)\") }}): expected string or buffer"}

When the expected output is something like is

        "ge-0/0/1.0              up    down eth-switch",
        "ge-0/0/2                up    down",
        "ge-0/0/2.0              up    down eth-switch",
        "ge-0/0/3                up    down",
        "ge-0/0/3.0              up    down eth-switch",
        "ge-0/0/4                up    up",
        "ge-0/0/4.0              up    up   eth-switch",
        "ge-0/0/5                up    down",
        "ge-0/0/5.0              up    down eth-switch",
        "ge-0/0/6                up    down",
        "ge-0/0/6.0              up    down eth-switch",
        "ge-0/0/7                up    down",
        "ge-0/0/7.0              up    down eth-switch",
        "ge-0/0/8                up    down",
        "ge-0/0/8.0              up    down eth-switch",
        "ge-0/0/9                up    down",
        "ge-0/0/9.0              up    down eth-switch",
        "ge-0/0/10               up    down",
        "ge-0/0/10.0             up    down eth-switch",
        "ge-0/0/11               up    down",
        "ge-0/0/11.0             up    down eth-switch",
        "ge-0/0/12               up    down",
        "ge-0/0/12.0             up    down eth-switch",
        "ge-0/0/13               up    down",
        "ge-0/0/13.0             up    down eth-switch",
        "ge-0/0/14               up    down",
        "ge-0/0/14.0             up    down eth-switch",
        "ge-0/0/15               up    down",
        "ge-0/0/15.0             up    down eth-switch",
        "ge-0/0/16               up    down",
        "ge-0/0/16.0             up    down eth-switch",
        "ge-0/0/17               up    down",
        "ge-0/0/17.0             up    down eth-switch",
        "ge-0/0/18               up    down",
        "ge-0/0/18.0             up    down eth-switch",
        "ge-0/0/19               up    down",
        "ge-0/0/19.0             up    down eth-switch",
        "ge-0/0/20               up    down",
        "ge-0/0/20.0             up    down eth-switch",
        "ge-0/0/21               up    down",
        "ge-0/0/21.0             up    down eth-switch",
        "ge-0/0/22               up    down",
        "ge-0/0/22.0             up    down eth-switch",
        "ge-0/0/23               up    down",
        "ge-0/0/23.0             up    down inet     10.152.254.253/30",

Bonus if it only contained the interfaces without the .0

Any help would be appericated.


Solution

  • Is this the code you're looking for?

    - debug:
        msg: "{{ item.split(' ')[0] }}"
      loop: "{{ inter.stdout_lines }}"
      when: item | regex_search('(^ge)')