# Set variable to some value if undefined

**URL:** <https://forum.ansible.com/t/set-variable-to-some-value-if-undefined/30316>\
**Category:** Ansible Project\
**Created:** [May 13, 2019, 1:25pm UTC](https://forum.ansible.com/t/set-variable-to-some-value-if-undefined/30316 "2019-05-13T13:25:36Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tuyen\_Nguyen](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tuyen\_Nguyen](https://forum.ansible.com/u/Tuyen_Nguyen)\
**Post date:** [May 13, 2019, 1:25pm UTC](https://forum.ansible.com/t/set-variable-to-some-value-if-undefined/30316/1 "2019-05-13T13:25:36Z")

</div>

Hi

I have a playbook whereby I pass variables in such as install\_office == ‘y’, and I have tasks where it would only include\_tasks for a task file to install Office (for example) when install\_office == ‘y’

eg.

tasks:

- name: Install Office  
include\_tasks: installoffice.yml  
when: install\_office == ‘y’

And when I did not want Office installed, I needed to explicitly define a variable to install\_office == ‘n’ (or anything else other than y would work as well), else Ansible tells me that install\_office variable is not defined.

How can I set it so that if I do not define the variable, it will default to ‘n’? Right now I am using include\_vars: to include a file defining each of the variables, and for each variable, I set it as ‘y’ or ‘no’ based on whether I want to include the task to my main playbook or not. It would be more simpler if I can just define the ones I want to include as ‘y’ and let everything else that my playbook expects but is not defined to be set to ‘n’.

Please let me know what I can do? Thanks!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [May 13, 2019, 1:49pm UTC](https://forum.ansible.com/t/set-variable-to-some-value-if-undefined/30316/2 "2019-05-13T13:49:01Z")

</div>

That is what the `default` filter is for:

when: install\_office|default('n') == 'y'

---

<div class="post-metadata">

**Author:** ![Tuyen\_Nguyen](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tuyen\_Nguyen](https://forum.ansible.com/u/Tuyen_Nguyen)\
**Post date:** [May 14, 2019, 2:52pm UTC](https://forum.ansible.com/t/set-variable-to-some-value-if-undefined/30316/3 "2019-05-14T14:52:49Z")

</div>

Hi

Thanks. I will try this. I think this should work.

---

<div class="post-metadata">

**Author:** ![Tuyen\_Nguyen](https://avatars.discourse-cdn.com/v4/letter/t/3ab097/32.png) [@Tuyen\_Nguyen](https://forum.ansible.com/u/Tuyen_Nguyen)\
**Post date:** [May 24, 2019, 5:21pm UTC](https://forum.ansible.com/t/set-variable-to-some-value-if-undefined/30316/4 "2019-05-24T17:21:44Z")

</div>

Thank you! This worked perfectly!!
