Ansible variables in when

Hi Everyone.

Currently I’m working in a script that should be capable of install software in different distros.
But my problem is that I want to simplify my role conditions putting them in a file.

For example:
My common vars file would be like this:

ubuntu Versions
is_1004: “‘$ansible_distribution_version’ == ‘10.04’”
is_1010: “‘$ansible_distribution_version’ == ‘10.10’”
is_1104: “‘$ansible_distribution_version’ == ‘11.04’”
is_1110: “‘$ansible_distribution_version’ == ‘11.10’”
is_1204: “‘$ansible_distribution_version’ == ‘12.04’”
is_1210: “‘$ansible_distribution_version’ == ‘12.10’”
is_1304: “‘$ansible_distribution_version’ == ‘13.04’”

is_two_selected: “$is_1004 or $is_1010”

And my roles selection file:

roles:

  • { role: Ubuntu_List, when: $is_two_selected}

But it keeps giving me this error:

TASK: [Adding source.] ********************************************************
fatal: [192.168.1.112] => Conditional expression must evaluate to True or False: {% if ‘6.2’ == ‘10.04’ or ‘6.2’ == ‘10.10’ %} True {% else %} False {% endif %}

FATAL: all hosts have already failed – aborting

Does ansible support variable sutitution? Or I’m doing something wrong?
If I put the plain string in the when condition ($ansible_distribution_version’ == ‘10.04’ or $ansible_distribution_version’ == ‘10.10’) the script executes normally.

Thanks in advance.

Yes, of course Ansible supports variable subsitution, in fact, you can see what it expanded to when it gave you the error in building the conditional.

Now, of course in Ansible 1.2, it’s preferred that when you use “when” you don’t do any of that old-style pre-1.2 templating, and use “{{ variable }}” vs "{{ $var }}, but based on what you have gottten back there may be something else up that I need to chase.

I will take a look shortly.

Hi Mario,

Can you try:

roles:

  • { role: Ubuntu_List, when: is_two_selected}

  • Benno

Hi Benno.

It works. Well… almost.

It doesn’t give any error, but is letting pass a non Ubuntu machine.
So I suppose the condition is not being passed to the role.

Is there a way to see the conditions that are being applied?

Sorry I ommited my code in the answer.
The new version:

is_1004: “ansible_distribution_version == ‘10.04’”
is_1010: “'ansible_distribution_version == ‘10.10’”
is_1104: “'ansible_distribution_version == ‘11.04’”
is_1110: “'ansible_distribution_version == ‘11.10’”
is_1204: “'ansible_distribution_version == ‘12.04’”
is_1210: “'ansible_distribution_version == ‘12.10’”
is_1304: “'ansible_distribution_version == ‘13.04’”

is_two_selected: “is_1004 or is_1010”

roles:

  • { role: Ubuntu_List, when: is_two_selected}

But still doesn’t work.

Yeah, haven’t had a chance to look at this yet.

Please make sure there is a github ticket filed for this and I can investigate.

Thanks, I already filled it.