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

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

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.

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

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

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 %}

No judgement on your approach or model, but a jinja2 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

I would use the default filter:

{{ serverid_map[inventory_hostname_short] |default(serverid)}}