Context based variable substitution in Ansible 2.0

35 views
Skip to first unread message

Christian Kaps

unread,
Mar 29, 2016, 10:40:46 AM3/29/16
to Ansible Project
I have a role which contains tasks that are executed based on a variable set in a playbook. In my case I have a provisioning playbook and a configuration playbook. Both share a role named `ebs_single`. In this role I've tasks which should be executed only in the provisioning step and some other tasks which should be executed in the configuration step. 

- name: Some provisioning task
...
with_items: "{{ ec2.instances }}"
register: ec2_volumes
when: ebs_single_provisioning

- name: Some configuration task
...
when: ebs_single_configuration

Prior to 2.0 this wasn't an issue, because I've used a bare variable in `with_items`:
with_items: ec2.instances

But now if I try to fix the deprecation warning, I get an error that the variable `ec2` is not found.

I'm not sure if this is an issue or if it's by design. But how can I use such a concept with Ansible 2.0?

I've tried to default the variable if it's not set. But this doesn't work. I think also this would be the wrong way to fix this issue, because in the provisioning step an error should be thrown if the variable doesn't exists.
with_items: "{{ ec2.instances|default(omit) }}"

Any suggestions?

Best regards,
Christian

Christian Kaps

unread,
Mar 29, 2016, 10:56:41 AM3/29/16
to Ansible Project
Sorry, I forgot to mention that it fails in the configuration step, because the e2c variable is only set in the provisioning step.

Anyway, I got it working with the include statement.
- include: provisioning.yml
when: ebs_single_provisioning

- include: configuration.yml
when: ebs_single_configuration

Best regards,
Christian

Brian Coca

unread,
Mar 29, 2016, 1:18:51 PM3/29/16
to ansible...@googlegroups.com
Return an empty list and the task will be skipped, the `when` executes for EACH item, this 'worked' in 1.9 as the error was ignored and task skipped, not because the `when` was evaluated. 

You can remove the `when` on the second task, for the `with_` do this:

`with_items: "{{ (ec2|default({})).instances|default([]) }}"`


----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages