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

时间:2017-08-17 14:02:50

标签: ansible yaml

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

会感激一些帮助。提前谢谢。

1 个答案:

答案 0 :(得分:7)

output期望字符串作为输入,但when: output.stdout | join('') | search('distribute-list') 是具有不同属性的字典。

你应该好好用

join

这里需要中间ios,因为对于stdout - 系列模块stdout_lines是一个字符串列表,而command是一个列表列表(而对于stdout {1}}模块stdout_lines是一个字符串,public static String decrypt(String sessionId) { try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); final SecretKeySpec secretKey = new SecretKeySpec(key, "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKey); final String decryptedSessionId = new String(cipher.doFinal(Base64.decodeBase64(sessionId))); return decryptedSessionId; } catch (Exception e) { e.printStackTrace(); } return null; } 是一个字符串列表。)