# Conditionally using a task parameter if a variable is/is not defined.

**URL:** <https://forum.ansible.com/t/conditionally-using-a-task-parameter-if-a-variable-is-is-not-defined/15731>\
**Category:** Ansible Project\
**Created:** [July 22, 2014, 12:32am UTC](https://forum.ansible.com/t/conditionally-using-a-task-parameter-if-a-variable-is-is-not-defined/15731 "2014-07-22T00:32:41Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Steven\_Ringo](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@Steven\_Ringo](https://forum.ansible.com/u/Steven_Ringo)\
**Post date:** [July 22, 2014, 12:32am UTC](https://forum.ansible.com/t/conditionally-using-a-task-parameter-if-a-variable-is-is-not-defined/15731/1 "2014-07-22T00:32:41Z")

</div>

Hi,

I am running the `ec2` module to provision an instance, with variables defined in a `vars` file, and the action in a role playbook:

```auto
- name: instance provisioning
  local_action:
    module: ec2
    region: "{{ region }}"
    key_name: "{{ key }}"
    id: "{{ idempotency_token }}"
    ...truncated...

```

For the `ec2` module, `id` is an optional client token that if defined makes that operation idempotent.

If the `id` parameter is present in the action, it expects there to be a value for it.

I would like to have it that if `idempotency_token` is not defined or declared it does not pass in the `id` as parameter in the playbook.

I tried using `null` as a default but Amazon sees this as a valid token.

Is there a way to combine passing in an attribute conditionally, e.g. something like:

```auto
- name: instance provisioning
  local_action:
    module: ec2
    region: "{{ region }}"
    key_name: "{{ key }}"
    parameter:
      name: id
      value: "{{ idempotency_token }}"
      when: idempotency_token is defined

```

Thanks everyone.

Steve

---

<div class="post-metadata">

**Author:** ![Christian\_Thiemann](https://avatars.discourse-cdn.com/v4/letter/c/f04885/32.png) [@Christian\_Thiemann](https://forum.ansible.com/u/Christian_Thiemann)\
**Post date:** [July 22, 2014, 10:26am UTC](https://forum.ansible.com/t/conditionally-using-a-task-parameter-if-a-variable-is-is-not-defined/15731/2 "2014-07-22T10:26:06Z")

</div>

Not super pretty, but something like this should work:

- local\_action: ec2 region=“{{ region }}” key\_name=“{{ key }}” {{ ‘id="’ + idempotency\_token + ‘"’ if idempotency\_token is defined else ‘’ }}

The idea is that all YAML values are run through Jinja and you can use the key-value syntax for a task, with a string that contains only the key-value pairs you want.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 22, 2014, 12:13pm UTC](https://forum.ansible.com/t/conditionally-using-a-task-parameter-if-a-variable-is-is-not-defined/15731/3 "2014-07-22T12:13:28Z")

</div>

That might anger the playbook gods 🙂

It might be better to just have a “when: foo” and “when: not foo”. Somewhat cleaner. It’s not intended that YAML is used for much more than basic variable substitution inside of playbooks, like {{ x }}. You can easily push ansible into ugliness when you do not. 1.6.7 will actually yell at you if you do the above suggestion with variable additions, because it’s designed to detect some variable insertion issues, that can be security related when using untrusted variables.

To the original question, using exact\_count instead would be /still/ cleaner and what I would suggest instead. Just specify how many of each tag you want running.

---

<div class="post-metadata">

**Author:** ![Steven\_Ringo](https://avatars.discourse-cdn.com/v4/letter/s/bc79bd/32.png) [@Steven\_Ringo](https://forum.ansible.com/u/Steven_Ringo)\
**Post date:** [July 22, 2014, 9:16pm UTC](https://forum.ansible.com/t/conditionally-using-a-task-parameter-if-a-variable-is-is-not-defined/15731/4 "2014-07-22T21:16:34Z")

</div>

I am new to Ansible and I really appreciate having the project leader respond 🙂

Do you mind posting an example on how `exact_count` would work for the idempotency\_token? I am a bit lost on this one.

Thanks heaps

Steve

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [July 23, 2014, 1:25pm UTC](https://forum.ansible.com/t/conditionally-using-a-task-parameter-if-a-variable-is-is-not-defined/15731/5 "2014-07-23T13:25:22Z")

</div>

It doesn’t need it, actually.

Exact count works by making sure there are X number of instances running with the tag specified by “count\_tag”.

Thus if count\_tag is “webservers” and you have 5 running and exact count in 200, it will add 195 webservers.

Sources, for those interested, live here: [https://github.com/ansible/ansible/blob/devel/library/cloud/ec2](https://github.com/ansible/ansible/blob/devel/library/cloud/ec2)

---

<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:** [June 12, 2015, 9:07am UTC](https://forum.ansible.com/t/conditionally-using-a-task-parameter-if-a-variable-is-is-not-defined/15731/6 "2015-06-12T09:07:29Z")

</div>

Hi,

I’ve a similar issue and I would be interested if there exists now a solution to omit task parameters based on variable definitions. In my special example I’ve a role to provision EC2 instances. For some instances I would like to allocate private IP addresses and for some I wouldn’t. Any other idea how I can solve this issue without duplication roles?

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:** [June 12, 2015, 9:42am UTC](https://forum.ansible.com/t/conditionally-using-a-task-parameter-if-a-variable-is-is-not-defined/15731/7 "2015-06-12T09:42:27Z")

</div>

I’ve found the solution: [https://docs.ansible.com/playbooks\_filters.html#omitting-undefined-variables-and-parameters](https://docs.ansible.com/playbooks_filters.html#omitting-undefined-variables-and-parameters)
