如果其他在 ansible 打印语句中

tec*_*kid 4 conditional ansible

我想要一些有关具有多个条件的打印语句的语法的帮助。目前, for 的引号'{{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!' }}"
Run Code Online (Sandbox Code Playgroud)

ili*_*-sp 5

您可以改用此语法:

"{% if test_var == true %} LTE status on '{{ inventory_hostname }}' is good to go!{% else %} LTE status on '{{inventory_hostname}}' is not operational!{% endif %}"
Run Code Online (Sandbox Code Playgroud)

请参阅下面的完整工作示例,我使用布尔值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 %}"
Run Code Online (Sandbox Code Playgroud)

输出:

[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]$ 
Run Code Online (Sandbox Code Playgroud)

编辑:

使用多行变量更新 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 %}"
Run Code Online (Sandbox Code Playgroud)