# Context based variable substitution in Ansible 2.0

**URL:** <https://forum.ansible.com/t/context-based-variable-substitution-in-ansible-2-0/21007>\
**Category:** Ansible Project\
**Created:** [March 29, 2016, 2:40pm UTC](https://forum.ansible.com/t/context-based-variable-substitution-in-ansible-2-0/21007 "2016-03-29T14:40:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Christian\_Kaps](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@Christian\_Kaps](https://forum.ansible.com/u/Christian_Kaps)\
**Post date:** [March 29, 2016, 2:40pm UTC](https://forum.ansible.com/t/context-based-variable-substitution-in-ansible-2-0/21007/1 "2016-03-29T14:40:46Z")

</div>

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.

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

```

```auto

- 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`:

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

```auto
with_items: "{{ ec2.instances|default(omit) }}"

```

Any suggestions?

Best regards,  
Christian

---

<div class="post-metadata">

**Author:** ![Christian\_Kaps](https://avatars.discourse-cdn.com/v4/letter/c/839c29/32.png) [@Christian\_Kaps](https://forum.ansible.com/u/Christian_Kaps)\
**Post date:** [March 29, 2016, 2:56pm UTC](https://forum.ansible.com/t/context-based-variable-substitution-in-ansible-2-0/21007/2 "2016-03-29T14:56:41Z")

</div>

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.

```auto
- include: provisioning.yml
  when: ebs_single_provisioning

- include: configuration.yml
  when: ebs_single_configuration

```

Best regards,  
Christian

---

<div class="post-metadata">

**Author:** ![Brian\_Coca](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@Brian\_Coca](https://forum.ansible.com/u/Brian_Coca)\
**Post date:** [March 29, 2016, 5:18pm UTC](https://forum.ansible.com/t/context-based-variable-substitution-in-ansible-2-0/21007/3 "2016-03-29T17:18:41Z")

</div>

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([]) }}"`
