ansible syntax to use "if-else" and "set"

199 views
Skip to first unread message

Ansar Sahit

unread,
Jun 2, 2019, 12:49:28 PM6/2/19
to Ansible Project
Hi All,

Im trying to choose ansible inventory host group conditionally.

- name: Set group
  set_fact:
    group: |
        {% if c == 1 %}
        {% set group = 'grp1' %}
        {% elif c == 2 %}
        {% set group == 'grp3' %}
        {% else %}
        {% group == 'grp4' %}
        {% endif %}

- debug:
     var=group


This doesnt seem to work.

Whats the right way to use if-else with ansible set??

Kinldy assist.

S C Rigler

unread,
Jun 3, 2019, 2:52:08 AM6/3/19
to ansible...@googlegroups.com
In your example of what you're trying to do, you're setting "group" inside of the template but never returning it to the task.  What you might want is something like this:

- name: Set Group
  set_fact:
    group: |
      {% if c == 1 %}
      grp1
      {% elif c ==2 %}
      grp3
      {% else %}
      grp4
      {% endif %}

-- Steve

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/de181027-dc27-4190-8d1e-1e5feec12546%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Brian Coca

unread,
Jun 13, 2019, 7:13:20 PM6/13/19
to Ansible Project
vars:
group: ' {{ c ==1|ternary('gr1', c==2|ternary('grp3', 'grp4')) }}'

----------
Brian Coca

Ansar Sahit

unread,
Jun 15, 2019, 1:14:47 PM6/15/19
to Ansible Project
Hi Brian,

how can I use this if no of groups I have is like 10 ?

Kai Stian Olstad

unread,
Jun 15, 2019, 1:25:32 PM6/15/19
to ansible...@googlegroups.com
On 15.06.2019 19:14, Ansar Sahit wrote:
> how can I use this if no of groups I have is like 10 ?

Just extent it with parentheses inside parentheses, not recommended since it makes it more or less unreadable so just use the solution Steve Rigler gave you.


--
Kai Stian Olstad

Karl Auer

unread,
Jun 15, 2019, 1:39:39 PM6/15/19
to ansible-project
Or maybe a series of set_fact: statements:

- set_fact
     group: 'BAD'

- set_fact:
     group: grp1
  when: c == 1
- set_fact:
     group: grp2
  when: c == 2

- set_fact:
     group: grp3
  when: c == 3

[...etc...]

- fail:
     msg: "Invalid value for 'c' or 'c' not set!"
  when: group == 'BAD'

Regards, K.


For more options, visit https://groups.google.com/d/optout.


--
Karl Auer

Email  : ka...@2pisoftware.com
Website: http://2pisoftware.com


GPG/PGP : 301B 1F4E 624D AD99 242C 7A68 EC24 7113 E854 4A4E
Previous:
958A 2647 6C44 D376 3D63 86A5 FFB2 20BC 0257 5816

Ansar Sahit

unread,
Jun 16, 2019, 7:21:18 AM6/16/19
to Ansible Project
Hi All,

Thankyou for the response, I used Rigler's set_fact to determine the hostgroup like below

- name: Set Group
  set_fact:
    group: |
      {% if c == 1 %}
      grp1
      {% elif c ==2 %}
      grp3
      {% else %}
      grp4
      {% endif %}

- name: Run the playbook
  hosts: "{{ hostvars.localhost.group }}"
  roles:
    - myplaybook.yml

My inventory file is like below:

hosts.ini

[appdagents]
grp1
grp2
grp3
.
.
.
grp8

[grp1:vars]
var1="asfadfdsg"

[grp2:vars]
var1="gsfgfh"
.
.
.

[grp1]
host1/db1 ansible_host=host1 db=db1
host1/db2 ansible_host=host1 db=db2

.
.
.
etc

Im getting below error.

 [WARNING]: **Could not match supplied host pattern, ignoring: grp1**

I did look into few github issues and it says any commented lines in hosts.ini could cause a problem and I dont have any commented lines. 
Kindly help
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.

Kai Stian Olstad

unread,
Jun 16, 2019, 8:06:21 AM6/16/19
to ansible...@googlegroups.com
On 16.06.2019 13:21, Ansar Sahit wrote:
> Hi All,
>
> Thankyou for the response, I used Rigler's set_fact to determine the
> hostgroup like below
>
> - name: Set Group
> set_fact:
> group: |
> {% if c == 1 %}
> grp1
> {% elif c ==2 %}
> grp3
> {% else %}
> grp4
> {% endif %}

My hunch is that this produce a new line at the end.
You can check with
- debug: var=group

So when using YAML multi line you need to control the newlines.
|- will remove the last newline and {%- will remove any newline produced
by the YAML multiple lines

- name: Set Group
set_fact:
group: |-
{% if c == 1 %}
grp1
{%- elif c ==2 %}
grp3
{%- else %}
grp4
{%- endif %}


> Im getting below error.
>
> [WARNING]: **Could not match supplied host pattern, ignoring: grp1**

It's probably a newline at the end.


--
Kai Stian Olstad

Ansar Sahit

unread,
Jun 16, 2019, 10:50:48 PM6/16/19
to ansible...@googlegroups.com
HI Kai ,

I tried giving if in a single line and still get same error.

- name : set fact variable
  set_fact:
       group: "{% if c == 1 %}grp1{% elif c == 2 %}grp2{% else %}grp3{% endif %}"

Same error as mentioned above


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/61028d51315a2b48d7a178bc0b79aff7%40olstad.com.

Ansar Sahit

unread,
Jun 17, 2019, 9:44:38 AM6/17/19
to ansible...@googlegroups.com
I noticed 

Host "{{ hostvars.localhost.group}}" 
Isn't working.

What's the best way to choose host groups on which my playbook should run dynamically kindly help.


On Jun 17, 2019 8:20 AM, "Ansar Sahit" <sahit...@gmail.com> wrote:
HI Kai ,

I tried giving if in a single line and still get same error.

- name : set fact variable
  set_fact:
       group: "{% if c == 1 %}grp1{% elif c == 2 %}grp2{% else %}grp3{% endif %}"

Same error as mentioned above

Ansar Sahit

unread,
Jun 18, 2019, 2:18:20 PM6/18/19
to ansible...@googlegroups.com
Hi Rigler ,

Another doubt,

I'm trying to use this "if else block" with "set" inside a variable jinja2 file used in roles templating.

Cat varfile.yml.j2

----
        {% if c == 1 %}
        {% set group = 'grp1' %}
        {% elif c == 2 %}
        {% set group == 'grp3' %}
        {% else %}
        {% group == 'grp4' %}
        {% endif %}

Groupname : {{ group }}
Var: "test" 

This groupname should get populated in the variable file.

How can I achieve this?

To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+unsubscribe@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAFbiokdBbOsnz5nVCMbX-9ouTTL4K8jL2WNoQKz-F-MBYZq0fg%40mail.gmail.com.

Kai Stian Olstad

unread,
Jun 18, 2019, 2:25:36 PM6/18/19
to ansible...@googlegroups.com
On 18.06.2019 20:18, Ansar Sahit wrote:
> Another doubt,
>
> I'm trying to use this "if else block" with "set" inside a variable jinja2
> file used in roles templating.
>
> Cat varfile.yml.j2
>
> ----
> {% if c == 1 %}
> {% set group = 'grp1' %}
> {% elif c == 2 %}
> {% set group == 'grp3' %}
> {% else %}
> {% group == 'grp4' %}
> {% endif %}
>
> Groupname : {{ group }}
> Var: "test"
>
> This groupname should get populated in the variable file.
>
> How can I achieve this?

You can't use set like this, that's why we have given you another way of doing this.

--
Kai Stian Olstad
Reply all
Reply to author
Forward
0 new messages