I have a large Ansible playbook where Docker images are built when running it. I am using an increasing number as the tag to version them. Currently, I have to specify this in e
Ansible has a default all
group that, funnily enough, contains all the hosts in the inventory file.
As such you can do like with any host groups and provide group_vars for the host group.
As shown in the previous link these can be defined directly in the inventory file or they can be contained in a separate file named after the group in a group_vars
directory at the same directory level as the inventory file.
An example directory structure might then look something like:
-ansible
|--inventory
| |--group_vars
| | |--all
| | |--dev
| | |--test
| | |--prod
| | |--webservers
| | |--databases
| |--dev
| |--test
| |--prod
|--roles
...
Your dev inventory file might then look something like:
[dev:children]
webservers
databases
[webservers]
web1.dev
web2.dev
[databases]
database-master.dev
database-slave.dev
All of these hosts will now pick up any host specific config (that could be defined either in line or, just like with group_vars can be put into a host_vars folder) and also config for the specific groups they are in such as webservers
and then the groups they also inherit from such as dev
but also, by default, all
.
This can then be used to configure things in a coarser way than per host.
Things such as NTP servers may want to be defined in all, while DNS servers may want to be defined at the environment level (if your network is segmented into dev, test and production they may need different DNS servers setting in /etc/resolv.conf
) while different types of servers may have different configurations around things such as lists of packages to be installed. Finally, some things may need to be host specific such as setting the MySQL server id in a replication group.
If, instead, you only want to define playbook global settings rather than across the inventory (and so could be accessed by other playbooks) then you simply need a vars
block in your play definition like so:
- hosts: webservers
vars:
http_port: 80
tasks:
- name: Task1 to be ran against all the webservers
...
As mentioned before, you can always use the all
group here too:
- hosts: all
vars:
ntp_pool:
- ntp1.domain
- ntp2.domain
tasks:
- name: Task1 to be ran against all the servers
...
In general though, I would strongly recommend using roles to structure what things are ran against certain hosts and then using the inventory to explain what servers are what type and then use a group_vars dir at the inventory level to contain all the variables for those groups of hosts. Doing things this way will help you keep things in sensible places and allow you to easily reuse your code base.