Ansible/Separate Playbooks by Role
From charlesreid1
Contents
Separating Playbooks by Role
For Ansible playbooks involving multiple roles, top level playbooks should be separated by role.
Here is the simplest possible top-level playbook, which includes two sub-playbooks:
- import_playbook: webservers.yml - import_playbook: dbservers.yml
These files are both at the top level, and
Using Separate Playbooks
Now that you have multiple roles split across playbooks, you can either configure your whole infrastructure, by running site.yml with ansible-playbook, or you can configure particular pieces of infrastructure, for example by only running dbservers.yml.
To limit which playbooks are run, either run the whole site-wide playbook with the --limit flag, or run with the sub-playbook:
ansible-playbook site.yml --limit webservers ansible-playbook webservers.yml
Further Examples of Using Separate Playbooks
If you further separate your tasks (things to do) and handlers (things to do that are triggered by other things being done) by role, you can start to do lots of different things.
To reconfigure all of the infrastructure:
ansible-playbook -i production site.yml
To reconfigure NTP only:
ansible-playbook -i production site.yml --tags ntp
To reconfigure webservers only, use the webservers sub-playbook:
ansible-playbook -i production webservers.yml
To reconfigure webservers in Boston only, use the webservers sub-playbook plus the limit tag for Boston:
ansible-playbook -i production webservers.yml --limit boston
Or, to only reconfigure a certain range of servers in Boston:
ansible-playbook -i production webservers.yml --limit boston[0:9] ansible-playbook -i production webservers.yml --limit boston[10:19]
Basic ad-hoc stuff:
ansible boston -i production -m ping ansible boston -i production -m command -a '/sbin/reboot'
Useful Commands
Other useful commands:
# confirm what task names would be run if I ran this command and said "just ntp tasks" ansible-playbook -i production webservers.yml --tags ntp --list-tasks # confirm what hostnames might be communicated with if I said "limit to boston" ansible-playbook -i production webservers.yml --limit boston --list-hosts
Flags