使用 when 条件匹配输出寄存器中的字符串(Ansible) [英] Using when conditional to match string in output register (Ansible)
问题描述
我无法在我的输出变量中搜索我用于 when 语句的指定字符串.下面的代码应该检查输出变量中的字符串distribute-list",但是在运行剧本时它给出了错误.
Im unable to search my output variable for a specified string that im using for a when statement. The code below is supposed to check for the string "distribute-list" in the output variable but when run the playbook it gives the error.
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")
希望得到一些帮助.提前致谢.
Would appreciate some help. Thanks in advance.
推荐答案
search
需要字符串作为输入,但 output
是一个具有不同属性的字典.
search
expects string as input, but output
is a dict with different properties.
你应该擅长
when: output.stdout | join('') | search('distribute-list')
这里需要中间join
,因为对于ios
-family 模块stdout
是一个字符串列表,而stdout_lines
是一个列表列表(而对于通常的 command
模块,stdout
是一个字符串,stdout_lines
是一个字符串列表).
you need intermediate join
here, because for ios
-family modules stdout
is a list of strings, and stdout_lines
is a list of lists (whereas for usual command
module stdout
is a string and stdout_lines
is a list of strings).
这篇关于使用 when 条件匹配输出寄存器中的字符串(Ansible)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!