# best practices in vars organisation

**URL:** <https://forum.ansible.com/t/best-practices-in-vars-organisation/34269>\
**Category:** Ansible Project\
**Created:** [December 2, 2020, 1:30pm UTC](https://forum.ansible.com/t/best-practices-in-vars-organisation/34269 "2020-12-02T13:30:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Daniel\_Caillibaud](https://avatars.discourse-cdn.com/v4/letter/d/2acd7d/32.png) [@Daniel\_Caillibaud](https://forum.ansible.com/u/Daniel_Caillibaud)\
**Post date:** [December 2, 2020, 1:30pm UTC](https://forum.ansible.com/t/best-practices-in-vars-organisation/34269/1 "2020-12-02T13:30:38Z")

</div>

Hi,

I'm an ansible beginner, sorry if these topics have been already posted  
here.

I read a lot of pages on [https://docs.ansible.com/](https://docs.ansible.com/) (very complete and  
well done), had a look to ansible-examples (and also some github repo  
ansible-role-xxx), but I've still some question about best practices for  
vars organisation

I started with this structure  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_reuse\_roles.html#role-directory-structure](https://docs.ansible.com/ansible/latest/user_guide/playbooks_reuse_roles.html#role-directory-structure)

I want to describe my infrastructure into vars  
(a) vm list with their ip  
(b) a lot of default values (locales, ssh keys, timezone, etc.)

I read  
[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_variables.html#variable-precedence-where-should-i-put-a-variable](https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#variable-precedence-where-should-i-put-a-variable)  
[https://docs.ansible.com/ansible/latest/user\_guide/intro\_inventory.html#organizing-host-and-group-variables](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#organizing-host-and-group-variables)  
and :

(a) seems better in group\_vars/all.yml

(b) seems better in the right roles/xxx/vars.yml, but for the maintenance  
it's easier to put all in the same place (painfull to have some ssh keys in  
roles/common/vars.yml and others in roles/webservers/vars.yml, even if each  
are used only in their role)

Any advice ?

About override, I saw that it's often with a default value, sometimes  
changed in a specific case, but is it possible to add item to a default  
list (without replacing the list) ?

The use case could be for ssh keys, a list for all targets and some  
specifics keys to add for some targets.

I guess I can define for all the vars default\_ssh\_keys and ssh\_keys\_to\_add  
(set to by default), then override ssh\_keys\_to\_add for some targets then  
concat in task, but perhaps there is an easier way.

I imagined also just the default\_ssh\_keys defined for all, and then a task  
to export a ssh\_keys var, which would be the concat of default\_ssh\_keys and  
ssh\_keys\_extra if defined, with set\_fact

What is the better way to do this ?

And for the description, I'm not really sure about the best var structure,  
between

1) several dict, target as key on each dict

privates\_ipv4:  
&nbsp;&nbsp;host1: 192.168.1.1  
&nbsp;&nbsp;host2: 192.168.2.1  
&nbsp;&nbsp;foo: 192.168.1.4  
&nbsp;&nbsp;bar: 192.168.2.42

publics\_ipv4:  
&nbsp;&nbsp;foo: 1.2.3.4

parent\_hosts  
&nbsp;&nbsp;foo: host1  
&nbsp;&nbsp;bar: host2

or  
2) one dict, target as key

targets:  
&nbsp;&nbsp;host1:  
&nbsp;&nbsp;&nbsp;&nbsp;private\_ipv4: 192.168.1.1  
&nbsp;&nbsp;host2:  
&nbsp;&nbsp;&nbsp;&nbsp;private\_ipv4: 192.168.2.1  
&nbsp;&nbsp;foo:  
&nbsp;&nbsp;&nbsp;&nbsp;private\_ipv4: 192.168.1.4  
&nbsp;&nbsp;&nbsp;&nbsp;public\_ipv4: 1.2.3.4  
&nbsp;&nbsp;&nbsp;&nbsp;parent: host1  
&nbsp;&nbsp;bar:  
&nbsp;&nbsp;&nbsp;&nbsp;private\_ipv4: 192.168.2.42  
&nbsp;&nbsp;&nbsp;&nbsp;parent: host2

or  
3) one list of dicts, target in the "name" key

targets:  
&nbsp;&nbsp;- name: host1  
&nbsp;&nbsp;&nbsp;&nbsp;private\_ipv4: 192.168.1.1  
&nbsp;&nbsp;- name: host2  
&nbsp;&nbsp;&nbsp;&nbsp;private\_ipv4: 192.168.2.1  
&nbsp;&nbsp;- name: foo  
&nbsp;&nbsp;&nbsp;&nbsp;private\_ipv4: 192.168.1.4  
&nbsp;&nbsp;&nbsp;&nbsp;public\_ipv4: 1.2.3.4  
&nbsp;&nbsp;&nbsp;&nbsp;parent: host1  
&nbsp;&nbsp;- name: bar  
&nbsp;&nbsp;&nbsp;&nbsp;private\_ipv4: 192.168.2.42  
&nbsp;&nbsp;&nbsp;&nbsp;parent: host2

I started with 1), easier for loop & filtering in tasks and templates, but  
in the long term I'm wondering if others solutions are easier to maintain…

Perhaps that 2) allow to set groups on each target (better than repeating  
target list in host definition), with with dynamic inventory and  
constructed plugin, I'll see…

Any advice from experienced users will be appreciated 😉
