你可以将正则表达式传递给Juniper的匹配命令如何做?

huangapple go评论65阅读模式
英文:

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?

英文:

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?

答案1

得分: 2

我不是完全确定,因为我没有Juniper设备,也从未使用过该模块...但我可以确定它不支持管道作为命令的一部分。我在当前文档中没有找到任何提及,但在Juniper网站上的旧版本文档上有指定。

与此同时,除非收集整个VLAN集合需要非常多的资源,否则在Ansible中进行筛选非常容易:

    - name: 收集活动的VLAN..
      junipernetworks.junos.junos_command:
        commands:
          - "show vlans"
      register: vlan_list

    - name: 仅显示数字VLAN
      debug:
        msg: "{{ vlan_list.stdout_lines | select('match', '\\d+') }}"

请注意,上述内容仅回答了您的直接问题。我怀疑获得此信息的更好方式是通过junos_facts模块

英文:

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

huangapple
  • 本文由 发表于 2023年2月27日 13:53:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577144.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定