【问题标题】:Using when conditional to match string in output register (Ansible)使用条件匹配输出寄存器中的字符串(Ansible)
【发布时间】:2017-08-17 14:02:49
【问题描述】:

我无法在我的输出变量中搜索我用于 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")                           

不胜感激。提前致谢。

【问题讨论】:

    标签: ansible yaml


    【解决方案1】:

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

    你应该擅长

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

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

    【讨论】:

    • 非常感谢。将此贴在另一个论坛上,但无法获得帮助。发布在 Stack Overflow 上,不到一小时后我得到了帮助。非常感谢
    猜你喜欢
    • 2015-12-23
    • 2022-08-23
    • 1970-01-01
    • 2017-12-24
    • 2020-01-08
    • 1970-01-01
    • 2021-12-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多