我需要有关具有多个条件的打印语句的语法的帮助。目前,报价为'{{inventory_hostname}}'
导致错误,如果我删除剧本运行的引号,但列出文本 inventory_hostname 而不是变量。我想知道如何打印变量以及 if else 语句中的语法是否良好。
- debug:
msg: "{{ 'LTE status on '{{inventory_hostname}}' is good to go!' if output.stdout | join('') is search('Selected = LTE') else 'LTE status on '{{inventory_hostname}}' is not operational!' }}"
您可以改用以下语法:
"{% if test_var == true %} LTE status on '{{ inventory_hostname }}' is good to go!{% else %} LTE status on '{{inventory_hostname}}' is not operational!{% endif %}"
请参阅下面的完整工作示例,我正在使用布尔值test_var
控制输出:
---
- hosts: localhost
gather_facts: false
vars:
test_var: true
tasks:
- debug:
msg: "{% if test_var == true %} LTE status on '{{ inventory_hostname }}' is good to go!{% else %} LTE status on '{{inventory_hostname}}' is not operational!{% endif %}"
output:
[http_offline@greenhat-29 tests]$ ansible-playbook test.yml
PLAY [localhost] *******************************************************************************************************************************************************************************************************
TASK [debug] ***********************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": " LTE status on 'localhost' is good to go!"
}
PLAY RECAP *************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0
[http_offline@greenhat-29 tests]$
EDIT:
使用多行变量更新 PB:
---
- hosts: localhost
gather_facts: false
vars:
test_var: ['text line 1', 'texttttttttttt Selected = LTE more text', 'text line 3']
tasks:
- debug:
msg: "{% if test_var | join('') is search('Selected = LTE') %} LTE status on '{{ inventory_hostname }}' is good to go!{% else %} LTE status on '{{inventory_hostname}}' is not operational!{% endif %}"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)