Ansible Print IOS 字符串匹配输出
Ansible Print IOS output on string match
我正在尝试使用 Ansible 攻击一堆 (100+) Cisco Catalyst 交换机并检查它们是否安装了特定的线卡。通过 SSH,可以使用 "sh mod" 命令完成此操作。我想在剧本中解析该命令的输出,然后在某个字符串匹配时显示命令的输出。现在使用下面的剧本我得到以下错误:
fatal: [redacted-hostname]: FAILED! => {"failed": true, "msg": "The
conditional check 'showmod | search(\"4548\")' failed. The error was:
Unexpected templating type error occurred on ({% if showmod |
search(\"4548\") %} True {% else %} False {% endif %}): expected
string or buffer\n\nThe error appears to have been in
'/etc/ansible/playbooks/linecard-4548.yaml': line 22, column 5, but
may\nbe elsewhere in the file depending on the exact syntax
problem.\n\nThe offending line appears to be:\n\n\n - debug:
\"msg='4548 Card Found'\"\n ^ here\n"}
当前剧本代码:
---
- hosts: redacted-hostname
gather_facts: yes
connection: local
tasks:
- name: SYS | Define provider
set_fact:
provider:
host: "{{ inventory_hostname }}"
username: redacted-user
password: redacted-password
- name: IOS | Get Module List
ios_command:
provider: "{{ provider }}"
commands:
- sh mod | inc 4548
register: showmod
- debug: "msg='4548 Card Found'"
when: showmod.stdout | search("/4548/")
我已经尝试在调试中使用 when
和不使用 .stdout
都无济于事。我做了一些研究,我得到的错误通常发生在 showmod
未定义的情况下,但它确实是。如果我用以下代码片段替换调试,剧本运行正常,但当然它会打印每个开关的输出,这不是我想要的。
- name: IOS | Show Output
debug:
var: showmod
有什么建议吗?
ios_command returns stdout
作为列表,stdout_lines
作为列表列表(而 command
模块 return stdout
作为字符串,stdout_lines
作为列表)。
所以在你的情况下,你可能想尝试:
- debug: "msg='4548 Card Found'"
when: showmod.stdout | join(" ") | search("/4548/")
我正在尝试使用 Ansible 攻击一堆 (100+) Cisco Catalyst 交换机并检查它们是否安装了特定的线卡。通过 SSH,可以使用 "sh mod" 命令完成此操作。我想在剧本中解析该命令的输出,然后在某个字符串匹配时显示命令的输出。现在使用下面的剧本我得到以下错误:
fatal: [redacted-hostname]: FAILED! => {"failed": true, "msg": "The conditional check 'showmod | search(\"4548\")' failed. The error was: Unexpected templating type error occurred on ({% if showmod | search(\"4548\") %} True {% else %} False {% endif %}): expected string or buffer\n\nThe error appears to have been in '/etc/ansible/playbooks/linecard-4548.yaml': line 22, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - debug: \"msg='4548 Card Found'\"\n ^ here\n"}
当前剧本代码:
---
- hosts: redacted-hostname
gather_facts: yes
connection: local
tasks:
- name: SYS | Define provider
set_fact:
provider:
host: "{{ inventory_hostname }}"
username: redacted-user
password: redacted-password
- name: IOS | Get Module List
ios_command:
provider: "{{ provider }}"
commands:
- sh mod | inc 4548
register: showmod
- debug: "msg='4548 Card Found'"
when: showmod.stdout | search("/4548/")
我已经尝试在调试中使用 when
和不使用 .stdout
都无济于事。我做了一些研究,我得到的错误通常发生在 showmod
未定义的情况下,但它确实是。如果我用以下代码片段替换调试,剧本运行正常,但当然它会打印每个开关的输出,这不是我想要的。
- name: IOS | Show Output
debug:
var: showmod
有什么建议吗?
ios_command returns stdout
作为列表,stdout_lines
作为列表列表(而 command
模块 return stdout
作为字符串,stdout_lines
作为列表)。
所以在你的情况下,你可能想尝试:
- debug: "msg='4548 Card Found'"
when: showmod.stdout | join(" ") | search("/4548/")