从Ansible的junos列表中获取特定项目

时间:2019-02-11 02:51:13

标签: ansible juniper

可用版本:可用2.7.7
Juniper.junos 2.1.0

我的问题是我有一个接口数组,一个开关,我只想在其中找到物理接口(例如ge-0 / 0/0或ge-0 / 0/14或xe-0 / 0 / 3)

我尝试过

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

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

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

Yaml文件:

---
- 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)") }}'

inter.stdout_lines的输出是: 我知道它会返回一些东西,所以我知道它不是空的。但是我不确定inter.stdout_lines返回哪种类型。

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"
    ]
}

当任务Print results运行时,它返回一个错误: 从我可以看出来的错误是一个空变量或错误的类型,但我不知道如何更改数组的类型。

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

当预期输出是

        "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",

如果仅包含不包含.0的接口,则为奖励

任何帮助都会得到解决。

1 个答案:

答案 0 :(得分:1)

这是您要查找的代码吗?

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