# Problem assigning value to new variable on conditional definition of 2 other variables

**URL:** <https://forum.ansible.com/t/problem-assigning-value-to-new-variable-on-conditional-definition-of-2-other-variables/13428>\
**Category:** Ansible Project\
**Created:** [September 6, 2013, 6:34pm UTC](https://forum.ansible.com/t/problem-assigning-value-to-new-variable-on-conditional-definition-of-2-other-variables/13428 "2013-09-06T18:34:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Konstantin\_Root](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@Konstantin\_Root](https://forum.ansible.com/u/Konstantin_Root)\
**Post date:** [September 6, 2013, 6:34pm UTC](https://forum.ansible.com/t/problem-assigning-value-to-new-variable-on-conditional-definition-of-2-other-variables/13428/1 "2013-09-06T18:34:40Z")

</div>

Hi,

I’m having trouble getting simple construction to work - I want to assign to new variable value of var1 or var2 depending if they are defined or not.  
And in case both of them are defined then var2 should win.

I created basic script that should do this, but problem is that its not working, since variable is registered even when task is skipped due to “when” condition.  
Anyone have alternative solution for this task?

- command: echo {{ serverId }}  
register: serverId\_final  
when: serverId is defined

- command: echo {{ serverid\_map[inventory\_hostname\_short] }}  
register: serverId\_final  
when: serverid\_map is defined

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 7, 2013, 12:41pm UTC](https://forum.ansible.com/t/problem-assigning-value-to-new-variable-on-conditional-definition-of-2-other-variables/13428/2 "2013-09-07T12:41:47Z")

</div>

it is important in ansible that variables are registered to all hosts in a host loop, so no, it doesn’t work like this. The conditional is evaluated per host, so it records None for things that are skipped during a register statement.

I should say though that your use case seems a little complex, and it seems like you are trying to do something a little programmy, so can we step back and talk about the use case and what you are trying to model?

What is “serverid\_map” and “serverid\_final” ?

However I should point out this should work:

- command: echo {% if serverid\_map is defined %}{{ serverid\_map[inventory\_hostname\_short] }}{% else %}{{ serverid }}{% endif }  
register: serverId\_final

Given though, it’s definitely a ‘smell’ of sorts, so that’s why I’m asking a bit about the use case.

---

<div class="post-metadata">

**Author:** ![Konstantin\_Root](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@Konstantin\_Root](https://forum.ansible.com/u/Konstantin_Root)\
**Post date:** [September 9, 2013, 4:28pm UTC](https://forum.ansible.com/t/problem-assigning-value-to-new-variable-on-conditional-definition-of-2-other-variables/13428/3 "2013-09-09T16:28:16Z")

</div>

Michael,

Thank you for the solution.

My use case is following:

- We assign ID to each host that is present in ansible hosts file (that ID is later used in our deployment scripts)
- For sections that have many hosts and follow certain pattern we use patterns to specify hosts (to avoid having 100+ entries in ansible hosts file).  
In such case we could not specify ID directly in ansible hosts - for that purpose we have additional include file that contains hashmap/dictionary to specify IDs.  
Something like  
serverid\_map:

host1: ID1

- So we needed mechanism to handle both cases and populate 1 variable that is used later in different scripts

---

<div class="post-metadata">

**Author:** ![Konstantin\_Root](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@Konstantin\_Root](https://forum.ansible.com/u/Konstantin_Root)\
**Post date:** [September 11, 2013, 9:44am UTC](https://forum.ansible.com/t/problem-assigning-value-to-new-variable-on-conditional-definition-of-2-other-variables/13428/4 "2013-09-11T09:44:48Z")

</div>

Any specific version of Ansible i need to use to make {% %} sections work?  
Provided code does not work with version 1.2.3.

---

<div class="post-metadata">

**Author:** ![Konstantin\_Root](https://avatars.discourse-cdn.com/v4/letter/k/a9adbd/32.png) [@Konstantin\_Root](https://forum.ansible.com/u/Konstantin_Root)\
**Post date:** [September 11, 2013, 10:18am UTC](https://forum.ansible.com/t/problem-assigning-value-to-new-variable-on-conditional-definition-of-2-other-variables/13428/5 "2013-09-11T10:18:00Z")

</div>

Got it working - final % was missing and if check should be modified to handle all cases.  
So it should look like this:

- command: echo {% if serverid\_map[inventory\_hostname\_short] is defined %}{{ serverid\_map[inventory\_hostname\_short] }}{% else %}{{ serverid }}{% endif %}

---

<div class="post-metadata">

**Author:** ![kahlil.hodgson](https://avatars.discourse-cdn.com/v4/letter/k/b9bd4f/32.png) [@kahlil.hodgson](https://forum.ansible.com/u/kahlil.hodgson)\
**Post date:** [September 11, 2013, 4:45pm UTC](https://forum.ansible.com/t/problem-assigning-value-to-new-variable-on-conditional-definition-of-2-other-variables/13428/6 "2013-09-11T16:45:55Z")

</div>

No judgement on your approach or model, but a jinja2 if-expression  
([http://jinja.pocoo.org/docs/templates/#if-expression](http://jinja.pocoo.org/docs/templates/#if-expression)) might be  
clearer

- command: echo {{ serverid\_map[inventory\_hostname\_short] if  
serverid\_map[inventory\_hostname\_short] is defined else serverid }}

K

Kahlil (Kal) Hodgson GPG: C9A02289  
Head of Technology (m) +61 (0) 4 2573 0382  
DealMax Pty Ltd (w) +61 (0) 3 9008 5281

Suite 1415  
401 Docklands Drive  
Docklands VIC 3008 Australia

"All parts should go together without forcing. You must remember that  
the parts you are reassembling were disassembled by you. Therefore,  
if you can't get them together again, there must be a reason. By all  
means, do not use a hammer." -- IBM maintenance manual, 1925

---

<div class="post-metadata">

**Author:** ![Brian\_Coca1](https://avatars.discourse-cdn.com/v4/letter/b/278dde/32.png) [@Brian\_Coca1](https://forum.ansible.com/u/Brian_Coca1)\
**Post date:** [September 11, 2013, 5:28pm UTC](https://forum.ansible.com/t/problem-assigning-value-to-new-variable-on-conditional-definition-of-2-other-variables/13428/7 "2013-09-11T17:28:10Z")

</div>

I would use the default filter:

{{ serverid\_map[inventory\_hostname\_short] |default(serverid)}}
