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

**URL:** <https://forum.ansible.com/t/ansible-syntax-to-use-if-else-and-set/30470>\
**Category:** Ansible Project\
**Created:** [June 2, 2019, 4:49pm UTC](https://forum.ansible.com/t/ansible-syntax-to-use-if-else-and-set/30470 "2019-06-02T16:49:28Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![sahitansar](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sahitansar](https://forum.ansible.com/u/sahitansar)\
**Post date:** [June 2, 2019, 4:49pm UTC](https://forum.ansible.com/t/ansible-syntax-to-use-if-else-and-set/30470/1 "2019-06-02T16:49:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![S\_C\_Rigler](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@S\_C\_Rigler](https://forum.ansible.com/u/S_C_Rigler)\
**Post date:** [June 3, 2019, 6:51am UTC](https://forum.ansible.com/t/ansible-syntax-to-use-if-else-and-set/30470/2 "2019-06-03T06:51:48Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [June 13, 2019, 11:12pm UTC](https://forum.ansible.com/t/ansible-syntax-to-use-if-else-and-set/30470/3 "2019-06-13T23:12:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sahitansar](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sahitansar](https://forum.ansible.com/u/sahitansar)\
**Post date:** [June 15, 2019, 5:14pm UTC](https://forum.ansible.com/t/ansible-syntax-to-use-if-else-and-set/30470/4 "2019-06-15T17:14:47Z")

</div>

Hi Brian,

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 15, 2019, 5:25pm UTC](https://forum.ansible.com/t/ansible-syntax-to-use-if-else-and-set/30470/5 "2019-06-15T17:25:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Karl\_Auer](https://avatars.discourse-cdn.com/v4/letter/k/cc9497/32.png) [@Karl\_Auer](https://forum.ansible.com/u/Karl_Auer)\
**Post date:** [June 15, 2019, 5:39pm UTC](https://forum.ansible.com/t/ansible-syntax-to-use-if-else-and-set/30470/6 "2019-06-15T17:39:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sahitansar](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sahitansar](https://forum.ansible.com/u/sahitansar)\
**Post date:** [June 16, 2019, 11:21am UTC](https://forum.ansible.com/t/ansible-syntax-to-use-if-else-and-set/30470/7 "2019-06-16T11:21:17Z")

</div>

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.

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

```

```auto

```

```auto
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. 

```

```auto
Kindly help

```

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 16, 2019, 12:06pm UTC](https://forum.ansible.com/t/ansible-syntax-to-use-if-else-and-set/30470/8 "2019-06-16T12:06:03Z")

</div>

> Hi All,
> 
> Thankyou for the response, I used Rigler's set\_fact to determine the  
> hostgroup like below
> 
> - name: Set Group  
> &nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;group: |  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% if c == 1 %}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grp1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% elif c ==2 %}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grp3  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% else %}  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grp4  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% endif %}

My hunch is that this produce a new line at the end.  
You can check with  
&nbsp;&nbsp;&nbsp;- 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  
&nbsp;&nbsp;&nbsp;set\_fact:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;group: |-  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{% if c == 1 %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grp1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{%- elif c ==2 %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grp3  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{%- else %}  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;grp4  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{%- endif %}

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

It's probably a newline at the end.

---

<div class="post-metadata">

**Author:** ![sahitansar](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sahitansar](https://forum.ansible.com/u/sahitansar)\
**Post date:** [June 17, 2019, 2:50am UTC](https://forum.ansible.com/t/ansible-syntax-to-use-if-else-and-set/30470/9 "2019-06-17T02:50:25Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![sahitansar](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sahitansar](https://forum.ansible.com/u/sahitansar)\
**Post date:** [June 17, 2019, 1:44pm UTC](https://forum.ansible.com/t/ansible-syntax-to-use-if-else-and-set/30470/10 "2019-06-17T13:44:21Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![sahitansar](https://avatars.discourse-cdn.com/v4/letter/s/eb9ed0/32.png) [@sahitansar](https://forum.ansible.com/u/sahitansar)\
**Post date:** [June 18, 2019, 6:18pm UTC](https://forum.ansible.com/t/ansible-syntax-to-use-if-else-and-set/30470/11 "2019-06-18T18:18:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [June 18, 2019, 6:25pm UTC](https://forum.ansible.com/t/ansible-syntax-to-use-if-else-and-set/30470/12 "2019-06-18T18:25:20Z")

</div>

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