# Variable defaults

**URL:** <https://forum.ansible.com/t/variable-defaults/14304>\
**Category:** Ansible Project\
**Created:** [January 9, 2014, 8:02pm UTC](https://forum.ansible.com/t/variable-defaults/14304 "2014-01-09T20:02:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stan\_Lemon](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@Stan\_Lemon](https://forum.ansible.com/u/Stan_Lemon)\
**Post date:** [January 9, 2014, 8:02pm UTC](https://forum.ansible.com/t/variable-defaults/14304/1 "2014-01-09T20:02:58Z")

</div>

Is there a way to default a variable inside of a tasks file? Use case is something like this… I have a list of tasks that I include in some play books, each of which I am currently defining the vars in. For a couple of those vars they’re there more for flexibility that actually necessity, and I am wondering if there is a way with jinja to maybe default them… something like {{varname|“def\_value”}} or something that could be done in the task itself when that variable has not been defined.

Thanks for your help,  
Stan

---

<div class="post-metadata">

**Author:** ![James\_Cammarata1](https://avatars.discourse-cdn.com/v4/letter/j/d26b3c/32.png) [@James\_Cammarata1](https://forum.ansible.com/u/James_Cammarata1)\
**Post date:** [January 9, 2014, 8:12pm UTC](https://forum.ansible.com/t/variable-defaults/14304/2 "2014-01-09T20:12:24Z")

</div>

If you’re talking about roles, default variables should be put in the defaults/main.yml file.

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [January 9, 2014, 8:13pm UTC](https://forum.ansible.com/t/variable-defaults/14304/3 "2014-01-09T20:13:53Z")

</div>

​Almost there:

{{ varname | default('default\_value') }}

​

---

<div class="post-metadata">

**Author:** ![Stan\_Lemon](https://avatars.discourse-cdn.com/v4/letter/s/7cd45c/32.png) [@Stan\_Lemon](https://forum.ansible.com/u/Stan_Lemon)\
**Post date:** [January 10, 2014, 1:58am UTC](https://forum.ansible.com/t/variable-defaults/14304/4 "2014-01-10T01:58:12Z")

</div>

Awesome, that’s exactly what I was in need of - thank you!
