# Variable definition best practice

**URL:** <https://forum.ansible.com/t/variable-definition-best-practice/21785>\
**Category:** Ansible Project\
**Created:** [June 15, 2016, 3:42pm UTC](https://forum.ansible.com/t/variable-definition-best-practice/21785 "2016-06-15T15:42:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mpampis\_Mpigkonias](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mpampis\_Mpigkonias](https://forum.ansible.com/u/Mpampis_Mpigkonias)\
**Post date:** [June 15, 2016, 3:42pm UTC](https://forum.ansible.com/t/variable-definition-best-practice/21785/1 "2016-06-15T15:42:39Z")

</div>

Hello,

I use some fact variables in my playbook in my tasks code (I’ve been using roles). I know that they don’t need to be defined in the vars part, but is it a good practice to do so?

I have something like:

> hostname: “{{ ansible\_hostname }}”  
> year: “{{ ansible\_date\_time.year }}”  
> month: “{{ ansible\_date\_time.month }}”  
> day: “{{ ansible\_date\_time.day }}”  
> date: “{{ ansible\_date\_time.date }}”  
> dest\_file: “/tlp\_adm/recoverydata/linux/{{ year }}/{{ month }}/{{ day }}/{{ hostname }}.recoverydata.{{ date }}”

in vars.

while the tasks code does something like this:

> - name: Ensure destination path exists  
> file: path=/tlp\_adm/recoverydata/linux/{{ year }}/{{ month }}/{{ day }} state=directory
> - name: Output recovery log  
> local\_action: template src=tlp\_recoveryData.j2 dest=“{{ dest\_file }}”

I think that defining variables like this on top makes the code a little bit more readable, but I don’t know if it makes sense to put everything in variables, like for example the source path or the facts (?).

What is your opinion? What are the best practices on this?

---

<div class="post-metadata">

**Author:** ![Jason\_Bornhoft](https://avatars.discourse-cdn.com/v4/letter/j/b487fb/32.png) [@Jason\_Bornhoft](https://forum.ansible.com/u/Jason_Bornhoft)\
**Post date:** [June 17, 2016, 4:18pm UTC](https://forum.ansible.com/t/variable-definition-best-practice/21785/2 "2016-06-17T16:18:04Z")

</div>

I’ve never set vars inside the playbook but I’ve seen it done. I’m not really a fan of that structure because different applications/environments might use the same role and having the vars set within the playbooks can cause a lot of problems or unintended consequences.

Without know your entire infrastructure it’s difficult to answer but I specify variables in three separate locations

1. /defaults/main.yml

2. /group\_vars

3. /host\_vars

Ansible is quite flexible with its handling of variables and allows them to be specified in many different locations.

---

<div class="post-metadata">

**Author:** ![Mpampis\_Mpigkonias](https://avatars.discourse-cdn.com/v4/letter/m/e495f1/32.png) [@Mpampis\_Mpigkonias](https://forum.ansible.com/u/Mpampis_Mpigkonias)\
**Post date:** [June 23, 2016, 3:18pm UTC](https://forum.ansible.com/t/variable-definition-best-practice/21785/3 "2016-06-23T15:18:45Z")

</div>

Thanks for the reply Jason. Maybe I should explain it better.

The part

> hostname: “{{ ansible\_hostname }}”  
> year: “{{ ansible\_date\_time.year }}”  
> month: “{{ ansible\_date\_time.month }}”  
> day: “{{ ansible\_date\_time.day }}”  
> date: “{{ ansible\_date\_time.date }}”  
> dest\_file: “/tlp\_adm/recoverydata/linux/{{ year }}/{{ month }}/{{ day }}/{{ hostname }}.recoverydata.{{ date }}”

is already in my /vars/main.yml

while the part

> - name: Ensure destination path exists  
> file: path=/tlp\_adm/recoverydata/linux/{{ year }}/{{ month }}/{{ day }} state=directory
> - name: Output recovery log  
> local\_action: template src=tlp\_recoveryData.j2 dest=“{{ dest\_file }}”

is already in /tasks/main.yml

My question is, if it is a good practice for these variables to be defined in general, because in my case you can clearly omit them and use them directly (as facts) in the tasks code.
