# Variables based on conditions

**URL:** <https://forum.ansible.com/t/variables-based-on-conditions/33810>\
**Category:** Ansible Project\
**Created:** [September 15, 2020, 8:20am UTC](https://forum.ansible.com/t/variables-based-on-conditions/33810 "2020-09-15T08:20:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Yantram\_Cloud](https://avatars.discourse-cdn.com/v4/letter/y/c0e974/32.png) [@Yantram\_Cloud](https://forum.ansible.com/u/Yantram_Cloud)\
**Post date:** [September 15, 2020, 8:20am UTC](https://forum.ansible.com/t/variables-based-on-conditions/33810/1 "2020-09-15T08:20:01Z")

</div>

is there a way i can include only those variables which are defined in a task.  
for example: in below task, i want to include disk\_encryption\_key only when its passed from the user. else i want to exclude.  
basically i know we can include tasks based on the condition, looking for something similar for variables as well.

- name: google disk configurations  
gcp\_compute\_disk:  
name: “{{ gcp\_compute\_disk\_name }}”  
description: “{{ gcp\_compute\_disk\_name }}”  
size\_gb: “{{ disk\_size }}”  
source\_image: “{{ source\_image }}”  
disk\_encryption\_key: “{{ disk\_encryption\_key }}”  
licenses: “{{ licenses }}”  
labels: “{{ labels }}”  
physical\_block\_size\_bytes: “{{ physical\_block\_size\_bytes }}”  
zone: “{{ zone }}”  
project: “{{ gcp\_project }}”  
auth\_kind: “{{ gcp\_cred\_kind }}”  
service\_account\_file: “{{ gcp\_cred\_file }}”  
scopes:
- [https://www.googleapis.com/auth/compute](https://www.googleapis.com/auth/compute)

# state: “{{ gcp\_compute\_disk\_lifecycle\_event }}”

register: disk  
tags:

- always
- gcp\_compute\_disk\_info

Thanks in advance  
Yantram

---

<div class="post-metadata">

**Author:** ![Akasurde](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/akasurde/32/230_2.png) [@Akasurde](https://forum.ansible.com/u/Akasurde)\
**Post date:** [September 15, 2020, 8:22am UTC](https://forum.ansible.com/t/variables-based-on-conditions/33810/2 "2020-09-15T08:22:41Z")

</div>

Read about defaulting values - [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html#defaulting-undefined-variables](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#defaulting-undefined-variables)  
and omit - [https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_filters.html#omitting-parameters](https://docs.ansible.com/ansible/latest/user_guide/playbooks_filters.html#omitting-parameters)

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [September 15, 2020, 8:40am UTC](https://forum.ansible.com/t/variables-based-on-conditions/33810/3 "2020-09-15T08:40:03Z")

</div>

> is there a way i can include only those variables which are defined in a task.  
> for example: in below task, i want to include disk\_encryption\_key only when its passed from the user. else i want to  
> exclude.  
> basically i know we can include tasks based on the condition, looking for something similar for variables as well.

Hello Yantram,

use:

disk\_encryption\_key: "{{ disk\_encryption\_key | default(omit) }}"

That will prevent passing disk\_encryption\_key to the gcp\_compute\_disk module  
when the value of that variable is undefined.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Yantram\_Cloud](https://avatars.discourse-cdn.com/v4/letter/y/c0e974/32.png) [@Yantram\_Cloud](https://forum.ansible.com/u/Yantram_Cloud)\
**Post date:** [September 15, 2020, 10:07am UTC](https://forum.ansible.com/t/variables-based-on-conditions/33810/4 "2020-09-15T10:07:45Z")

</div>

Thanks for pointing me in the right direction. This works.  
Also I would have liked to define default values as dictionaries and lists, not sure if we can achieve this in current implementation.  
I will investigate this a little further.
