[英]Ansible compare stdout with a variable
我有一個問題,因為我正在發瘋。 我想將標准輸出結果與存儲在我的 vars 文件中的一個變量進行比較。 例如,不是使用變量而是使用字符串(IP 22.22.2.2),它可以工作:
網絡.yml
- List_nets_find:
- host 22.22.2.2
劇本:
---
- name: Black List
hosts: routers
gather_facts: false
vars_files:
- vars/Nets.yml
tasks:
- name: Check the object-group
ios_command:
commands:
- command: "show object-group BLOCK-LIST"
register: output
tags:
- see
- debug:
msg: included
when: output.stdout is search('22.22.2.2')
tags:
- see
結果:
TASK [debug] **************************************************************************************************************************************************************************************
skipping: [Router1]
ok: [Router3] => {
"msg": "included"
}
ok: [Router2] => {
"msg": "included"
}
但是我沒有使用字符串,而是使用了變量,它不起作用。
- name: Black List
hosts: routers
gather_facts: false
vars_files:
- vars/Nets.yml
tasks:
- name: Check the object-group
ios_command:
commands:
- command: "show object-group BLOCK-LIST"
register: output
tags:
- see
- debug:
msg: included
when: output.stdout is search('{{List_net_find}}')
tags:
- see
這是錯誤:
fatal: [Router1]: FAILED! => {"msg": "The conditional check 'output.stdout is search('{{List_net_find}}')' failed. The error was: error while evaluating conditional (output.stdout is search('{{List_net_find}}')): 'List_net_find' is undefined\n\nThe error appears to be in '/home/ansible/Ios_ACL_LAB/object-group.yml': line 23, column 10, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - see\n - debug:\n ^ here\n"}
fatal: [Router3]: FAILED! => {"msg": "The conditional check 'output.stdout is search('{{List_net_find}}')' failed. The error was: error while evaluating conditional (output.stdout is search('{{List_net_find}}')): 'List_net_find' is undefined\n\nThe error appears to be in '/home/ansible/Ios_ACL_LAB/object-group.yml': line 23, column 10, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - see\n - debug:\n ^ here\n"}
fatal: [Router2]: FAILED! => {"msg": "The conditional check 'output.stdout is search('{{List_net_find}}')' failed. The error was: error while evaluating conditional (output.stdout is search('{{List_net_find}}')): 'List_net_find' is undefined\n\nThe error appears to be in '/home/ansible/Ios_ACL_LAB/object-group.yml': line 23, column 10, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n - see\n - debug:\n ^ here\n"}
有什么建議嗎? 或其他方式來做到這一點?
非常感謝!
您的vars/Nets.yml
沒有聲明名為List_nets_find
的變量,它聲明了一個匿名列表,其第一個元素是一個dict
,其唯一鍵名為List_nets_find
如果您想要一個名為該變量的變量,則將- List_nets_find:
更改為List_nets_find:
另外,您要問的下一個問題是“為什么我的搜索不匹配任何內容”,這是因為List_nets_find
是 str 的列表,因此您需要將該測試更改為output.stdout is search(List_nets_find | map("regex_escape") | join("|"))
,使用regex_escape
過濾器將這些字符串轉換為可以安全地輸入search
替換的內容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.