【问题标题】:Ansible extract values from stdout_lines to use in subsequent loopAnsible 从 stdout_lines 提取值以在后续循环中使用
【发布时间】:2021-05-03 21:08:40
【问题描述】:

我正在尝试编写一个角色来删除 Cisco 路由器接口的所有帮助地址。 stdout_lines 中可以返回一个或多个帮助地址。我使用过滤器从行中删除标签和额外的花键,并将结果放入一个数组中以在后续命令循环中使用。我似乎得到了正确的信息,但使用该值失败。 这是我的角色:

---
- name: read helpers
  register: helper_facts
  ios_command:
     commands:
        - show ip interface {{ interface }} | section Helper

- set_fact:
    helper_addr: []

- set_fact:
    helper_addr: "{{ helper_addr }}  + [ '{{ item | regex_replace ('Helper address.* ','') | trim  }}' ]"
  with_items: "{{ helper_facts.stdout_lines }}"

- name: remove helpers
  ios_command:
     commands:
        - no ip helper-address {{ item }}
  with_items: "{{ helper_addr }}"

这是我得到的错误输出的片段:

ok: [ONTMDFRTR01] => (item=                       172.16.0.22) => {
    "ansible_facts": {
        "helper_addr": [
            "192.1.0.1",
            "172.16.0.22"
        ]
    },
    "ansible_loop_var": "item",
    "changed": false,
    "item": "                       172.16.0.22"
}

TASK [remove_helpers : remove helpers] **********************************************************************************
task path: /home/msimon/USPS/roles/remove_helpers/tasks/main.yml:15
Monday 03 May 2021  16:44:23 -0400 (0:00:00.704)       0:00:05.874 ************
<10.32.178.252> ESTABLISH LOCAL CONNECTION FOR USER: root
<10.32.178.252> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /root/.ansible/tmp/ansible-local-23291P_KVGE `"&& mkdir "1-258083848203070 `" && echo ansible-tmp-1620074664.59-23371-258083848203070="` echo /root/.ansible/tmp/ansible-local-232
Using module file /usr/lib/python2.7/site-packages/ansible/modules/network/ios/ios_command.py
<10.32.178.252> PUT /root/.ansible/tmp/ansible-local-23291P_KVGE/tmpuSDML5 TO /root/.ansible/tmp/ansible-local-23291P_KVG
<10.32.178.252> EXEC /bin/sh -c 'chmod u+x /root/.ansible/tmp/ansible-local-23291P_KVGE/ansible-tmp-1620074664.59-23371-24.59-23371-258083848203070/AnsiballZ_ios_command.py && sleep 0'
<10.32.178.252> EXEC /bin/sh -c '/usr/bin/python /root/.ansible/tmp/ansible-local-23291P_KVGE/ansible-tmp-1620074664.59-2
<10.32.178.252> EXEC /bin/sh -c 'rm -f -r /root/.ansible/tmp/ansible-local-23291P_KVGE/ansible-tmp-1620074664.59-23371-25
The full traceback is:
WARNING: The below traceback may *not* be related to the actual failure.
  File "/tmp/ansible_ios_command_payload_IFZQ7s/ansible_ios_command_payload.zip/ansible/module_utils/network/ios/ios.py",
    return connection.run_commands(commands=commands, check_rc=check_rc)
  File "/tmp/ansible_ios_command_payload_IFZQ7s/ansible_ios_command_payload.zip/ansible/module_utils/connection.py", line
    raise ConnectionError(to_text(msg, errors='surrogate_then_replace'), code=code)
failed: [ONTMDFRTR01] (item=192.1.0.1) => {
    "ansible_loop_var": "item",
    "changed": false,
    "invocation": {
        "module_args": {
            "auth_pass": null,
            "authorize": null,
            "commands": [
                "no ip helper-address 192.1.0.1"
            ],
            "host": null,
            "interval": 1,
            "match": "all",
            "password": null,
            "port": null,
            "provider": null,
            "retries": 10,
            "ssh_keyfile": null,
            "timeout": null,
            "username": null,
            "wait_for": null
        }
    },
    "item": "192.1.0.1",
    "msg": "no ip helper-address 192.1.0.1\r\nno ip helper-address 192.1.0.1\r\n   ^\r\n% Invalid input detected at '^' m
}```

【问题讨论】:

  • 嗨,马克,欢迎来到 SO。请不要截断错误消息,因为其余部分可能是帮助您所需的关键信息。我没有 IOS 设备才能知道,但是如果您在 ansible 之外键入确切的命令,它会达到您的预期吗?
  • 感谢您慢跑我的大脑。我从一个更大的剧本中剪掉了这个,并省略了设置接口的命令(“config t”,“int GigabitEthernet0/0/0”)。我的角色现在按预期工作。谢谢,我希望这对其他人有帮助。

标签: ansible iso cisco


【解决方案1】:

感谢 mdaniel 的评论,我找到了答案。我从一个更大的剧本中删掉了这个,并省略了在 no helper_addres 命令之前设置接口(“config t”、“int GigabitEthernet0/0/0”)所需的命令。我的角色现在按预期工作。谢谢,我希望这对其他人有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 2019-12-21
    • 1970-01-01
    • 1970-01-01
    • 2015-07-22
    相关资源
    最近更新 更多