Ansible Best Practice.

I am currently developing a Playbook consisting of a number of roles via include statements as show below:

hosts: testing
sudo: yes
roles:

  • timezone
  • ntp
  • mariadb
  • nfs-server

I would appreciate some advice of where to put all the varables for the server eg Database , NFS exports, Timezone etc Would they be added as vars: in the main playbook ? As vars under a host_vars directory ?

Thanks Jason

The short answer: it depends.

The long answer: How is your network / inventory is laid out? Where the value of the variable will be unique for each server, or a single managed machine is a one-off for that variable, you’d be best to store it in host_vars/hostname, or perhaps in the inventory file as “hostname var=value”. Where you have a group of machines in your inventory that will all share the same value, create a group_vars/groupname file with that variable. If you need to set a unique value for everything, when a role is used in a specific play in a playbook, you can set the var as in a -vars section within the play. It can also be done on the command line with --extra-vars if it’s a one-off that should be applied to every managed machine that will be touched.

Originally, I was containing my vars within the roles, but after realizing that those vars override everything else, instead, I only populate the default/main.yml with vars inside the roles, just so things don’t error, and define everything else in my main group_vars.

So, in my setup, when I have vars that are global, I put them in group_vars/all/*, then label them if there is more than one option.

e.g.
#file: group_vars/all/nfs_servers

`