I would like some help with syntax for a print statment that has multiple conditions. Currently, the quotes for '{{inventory_hostname}}'
is causing errors and if I remove the quotes the playbook runs but lists the text inventory_hostname instead of the variable. I would like know how I can get the variable to print and also if the syntax in the if else statement is good.
- 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!' }}"
you can use this syntax instead:
"{% if test_var == true %} LTE status on '{{ inventory_hostname }}' is good to go!{% else %} LTE status on '{{inventory_hostname}}' is not operational!{% endif %}"
see full working example below, i am using a boolean test_var
to control the output:
---
- 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:
updated PB with a multi-line variable:
---
- 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 %}"
Try this:
- debug:
msg: "{{ output.stdout is search('Selected = LTE') | ternary('LTE status on ' + inventory_hostname + ' is good to go!', 'LTE status on ' + inventory_hostname + ' is not operational!') }}"
You are better simplifying where you can, and sticking to pure Jinja2 filters as much as possible. Hopefully this is a little more readable.
join('')
. The join filter is used for joining arrays into a single string. stdout
is a string. stdout_lines
is the array based version of the output, therefore join('')
appears redundant in this case. {{}}
which are invalid. If you check out the ternary filter you will see that inside {{}}
'string' + variable_name
combines a literal string with a variableIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With