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

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.

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

vars:
   group: ' {{ c ==1|ternary('gr1', c==2|ternary('grp3', 'grp4')) }}'

Hi Brian,

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.

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.

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

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.

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

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.

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

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