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

tec*_*kid 4 yaml ansible

我无法在输出变量中搜索我用于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"}
Run Code Online (Sandbox Code Playgroud)

以下是导致问题的代码:

 - ios_command:
     commands: show run | sec ospf
     provider: "{{cli}}"
   register: output
 - debug:
     msg: "{{output.stdout_lines}}"
   when: output | search("distribute-list")                           
Run Code Online (Sandbox Code Playgroud)

会感激一些帮助.提前致谢.

Kon*_*rov 7

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

你应该好好的

when: output.stdout | join('') | search('distribute-list')
Run Code Online (Sandbox Code Playgroud)

你需要join这里的中间,因为 - ios家庭模块stdout是一个字符串列表,并且stdout_lines是一个列表列表(而普通command模块stdout是一个字符串,stdout_lines是一个字符串列表).