6

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!' }}"
techkid
  • 139
  • 2
  • 2
  • 9

3 Answers3

10

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 %}"
ilias-sp
  • 6,135
  • 4
  • 28
  • 41
  • How would I implement the when statement to check if LTE is in the output? This is what would set the condition to true. – techkid Jan 29 '19 at 19:47
  • i believe the filtering you have applied is correct, i just didnt have the `output.stdout` to test it on my own. i am adding a better example in the answer. – ilias-sp Jan 29 '19 at 20:08
4

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.

  • removed 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.
  • removed all the if/else stuff and replaced with the ternary filter. This simply takes a boolean and returns the first string if true and the second if false
  • removed the nested {{}} which are invalid. If you check out the ternary filter you will see that inside {{}} 'string' + variable_name combines a literal string with a variable
clockworknet
  • 2,736
  • 1
  • 15
  • 19
  • When I try this i get the error FAILED! => {"msg": "Unexpected templating type error occurred on ({{ output.stdout | search('Selected = LTE') | ternary('LTE status on ' + inventory_hostname + ' is good to go!', 'LTE status on ' + inventory_hostname + ' is not operational!') }}): expected string or buffer"} – techkid Jan 29 '19 at 19:46
  • Somehow, despite copying and pasting, I ended up with a syntax error, tho it did not produce the error you were seeing, so perhaps we both have a copy and paste issue :) Anyway, I have edited the code in my answer and double checked it works. (I am testing on Ansible 2.5) – clockworknet Jan 29 '19 at 20:11
0

This piece of code from this page save my time. "{% if test_var == true %} LTE status on '{{ inventory_hostname }}' is good to go!{% else %} LTE status on '{{inventory_hostname}}' is not operational!{% endif %}"

I tried to put the 2 variables inside a template module and with_nested, but not giving me results. Because I used "{% if VAR1== 'VALUE') %}{{ '/tmp/MYTESTINGDIR/' }}{% else %}{{ '/tmp/MYNEWTESTINGDIR/'{{ item[0] }} }}{% endif %}. Because the "/" was considering as special character and I have to use the "Sigle quote" for that. But it make the variable as invalid.

Realized later that "flower braces" not required after the if condition.

I tried multiple forums and Ansible docs, not found any place. Finally went to basics and this recommended solution helped.

Thank you so much.

mohan babu
  • 31
  • 3