gpt4 book ai didi

ansible - 使用条件匹配输出寄存器中的字符串(Ansible)

转载 作者:行者123 更新时间:2023-12-04 23:38:49 24 4
gpt4 key购买 nike

我无法在我的输出变量中搜索我用于 when 语句的指定字符串。下面的代码应该检查输出变量中的字符串“distribute-list”,但是在运行 playbook 时会出现错误。

fatal: [192.168.3.252]: FAILED! => {"failed": true, "msg": "The conditional check 'output | search(\"distribute-list\")' failed. The error was: Unexpected templating type error occurred on ({% if output | search(\"distribute-list\") %} True {% else %} False {% endif %}): expected string or buffer\n\nThe error appears to have been in '/home/khibiny/test4.yml': line 26, 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:\n    ^ here\n"}

这是导致问题的代码:
 - ios_command:
commands: show run | sec ospf
provider: "{{cli}}"
register: output
- debug:
msg: "{{output.stdout_lines}}"
when: output | search("distribute-list")

将不胜感激一些帮助。提前致谢。

最佳答案

search期望字符串作为输入,但 output是具有不同属性的字典。

你应该擅长

when: output.stdout | join('') | search('distribute-list')

你需要中级 join在这里,因为对于 ios -家庭模块 stdout是字符串列表, stdout_lines是一个列表列表(而通常 command 模块 stdout 是一个字符串, stdout_lines 是一个字符串列表)。

关于ansible - 使用条件匹配输出寄存器中的字符串(Ansible),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45737295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com