If condition in ansible jinja2 template

33 views
Skip to first unread message

Pandu jh

unread,
Feb 19, 2019, 9:41:03 AM2/19/19
to Ansible Project
Search keyword in "host_fqdn" variable. If the value has "lab.com" keyword in it, it should directly store the value to "host_fqdn" again
or else it should add "lab.com" keyword to the value and store it to  "host_fqdn" variable. Please assist.

vars:
    host_fqdn:  server
    host_fqdn: |
    {%  if  'lab.com' in {{  host_fqdn }} %}
        {%  host_fqdn = "{{ host_fqdn }}" %}
    {% else %}
        {%  host_fqdn = "{{ host_fqdn }}.lab.com" %}
    {%  endif %}

Kai Stian Olstad

unread,
Feb 19, 2019, 10:41:59 AM2/19/19
to ansible...@googlegroups.com
I'm pretty sure you can't use Jinja in vars like that, but you can have
a set_fact task like this

- set_fact:
host_fqdn: "{{ host_fqdn if 'lab.com' in host_fqdn else host_fqdn
~ '.lab.com' }}"

--
Kai Stian Olstad

Pandu jh

unread,
Feb 19, 2019, 10:49:00 AM2/19/19
to Ansible Project
Can you please confirm, if the below syntax is correct. I am getting error.

    vars_prompt:
      - name: host_fqdn
        prompt: Enter Hostname of a new VM
        private:  no
    tasks:
      - set_fact:
          host_fqdn:  "{{ host_fqdn if 'lab.com' in host_fqdn else host_fqdn '.lab.com' }}"
      - debug:  var=host_fqdn

Kai Stian Olstad

unread,
Feb 19, 2019, 10:54:38 AM2/19/19
to ansible...@googlegroups.com
On 19.02.2019 16:49, Pandu jh wrote:
> Can you please confirm, if the below syntax is correct. I am getting
> error.
>
> vars_prompt:
> - name: host_fqdn
> prompt: Enter Hostname of a new VM
> private: no
> tasks:
> - set_fact:
> host_fqdn: "{{ host_fqdn if 'lab.com' in host_fqdn else
> host_fqdn '.lab.com' }}"
> - debug: var=host_fqdn

What is the error then? We are not psychic.

--
Kai Stian Olstad

Pandu jh

unread,
Feb 19, 2019, 10:57:24 AM2/19/19
to Ansible Project
This was the error.

TASK [set_fact] ****************************************************************************************************************************************
fatal: [rchtest01]: FAILED! => {"msg": "template error while templating string: expected token 'end of print statement', got 'string'. String: {{ host_fqdn if 'lab.com' in host_fqdn else host_fqdn  '.lab.com' }}"}

Kai Stian Olstad

unread,
Feb 19, 2019, 11:03:26 AM2/19/19
to ansible...@googlegroups.com
On 19.02.2019 16:57, Pandu jh wrote:
> This was the error.
>
> TASK [set_fact]
> ****************************************************************************************************************************************
> fatal: [rchtest01]: FAILED! => {"msg": "template error while templating
> string: expected token 'end of print statement', got 'string'. String:
> {{
> host_fqdn if 'lab.com' in host_fqdn else host_fqdn '.lab.com' }}"}

You are missing the tilde ~ in you syntax, check my previous mail and
you'll see the tilde.

--
Kai Stian Olstad

Pandu jh

unread,
Feb 19, 2019, 11:06:41 AM2/19/19
to Ansible Project
Sorry my bad, it missed it. it's working perfectly now, Thanks much !!

TASK [debug] *******************************************************************************************************************************************
ok: [rchtest01] => {
    "host_fqdn": "rchtest01.lab.com"
}

Pandu jh

unread,
Feb 19, 2019, 11:07:59 AM2/19/19
to Ansible Project
If possible could you please explain the syntax, It will be very helpful for me to use it in other cases.

Kai Stian Olstad

unread,
Feb 19, 2019, 11:24:23 AM2/19/19
to ansible...@googlegroups.com
On 19.02.2019 17:07, Pandu jh wrote:
> If possible could you please explain the syntax, It will be very
> helpful
> for me to use it in other cases.

I could try, not sure if it's understandable

Just split it in multiple lines

[1] {{ host_fqdn
[2] if 'lab.com' in host_fqdn
[3] else
[4] host_fqdn ~ '.lab.com' }}

[2]
If the variable host_fqdn contains the string lab.com the choose [1].

[3]
If the variable host_fqdn dosen't contains the string lab.com the choose
[4]

[4]
Tilde is used to concatenate two strings.
So this will concatenate the content of variable fqdn_host with the
string .lab.com


You can also use the Ansible ternary filter

{{ ('lab.com' in host_fqdn) | ternary(host_fqdn, host_fqdn ~ '.lab.com')
}}


--
Kai Stian Olstad

Pandu jh

unread,
Feb 19, 2019, 11:51:53 AM2/19/19
to Ansible Project
Thank you so much for the explanation.
Reply all
Reply to author
Forward
0 new messages