【问题标题】:Ansible regex_findall multiple setsAnsible regex_findall 多组
【发布时间】:2017-04-27 14:37:15
【问题描述】:

我正在尝试使用 Ansible 从 IOS 设备收集数据,然后使用收集到的数据来运行任务。

我正在运行一个任务来执行 show running-config 命令来查找具有特定描述的接口:

- name: get current running-config
  ios_command:
    host: "{{ inventory_hostname }}"
    commands:
      - show running-config | i interface|description
  register: config  

该任务的输出:

interface GigabitEthernet1/0/35
 description TEST PHONE
interface GigabitEthernet1/0/36
 description TEST PHONE
interface GigabitEthernet1/0/37
 description *W137
interface GigabitEthernet1/0/38
 description *W138
interface GigabitEthernet1/0/39
 description *W139
interface GigabitEthernet1/0/40
 description *W140

我希望获取所有以 * 开头的描述的接口名称/编号。我正在使用 set_fact 从 regex_findall() 中获取它:

- name: get current interfaces with special description
  set_fact: noAuthInts="{{ config.stdout[0] | regex_findall('(.*?)\n description \*(.*)')}}"

正则表达式工作正常并从两组中获取数据。输出被视为:

msg": [
            [
                "interface GigabitEthernet1/0/37",
                "W137"
            ],
            [
                "interface GigabitEthernet1/0/38",
                "W138"
            ],
            [
                "interface GigabitEthernet1/0/39",
                "W139"
            ],
            [
                "interface GigabitEthernet1/0/40",
                "W140"
            ],
            [
                "interface GigabitEthernet1/0/41",
                "W141"
            ],
            [
                "interface GigabitEthernet1/0/42",
                "W142"
            ],
            [
                "interface GigabitEthernet1/0/43",
                "W143"
            ]
        ]

我不知道如何获取该列表中的两个项目并分别使用它们。我想使用 ios_config 运行一个任务,以从该列表中的接口名称/编号配置接口,并使用我收集的该描述。

这是一个嵌套列表吗?我尝试了不同的变体:

noAuthInts.0 或 noAuthInts.0.0 和 noAuthInts.0.1,但不要产生我正在寻找的东西。

我也尝试使用 with_nested: "{{ noAuthInts }}" 并遍历它,但似乎没有正确循环。

我怎样才能从该列表中取出这两个部分并单独使用它们?

【问题讨论】:

    标签: ansible ansible-facts


    【解决方案1】:

    我可以使用with_list 而不是with_itemswith_nested。现在与列表列表一起工作正常。引用了我的另一篇帖子:Ansible list of lists - flattening

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-10
      • 1970-01-01
      • 2019-06-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多