This Ansible Playbooks Starter Kit is a collection of ideas and best practices in organizing and using Ansible. I used Ansible for several projects in many companies and stumbled across different approaches to organize all the mess. This is the essence of my learnings so I won't forget it.
This repo only includes two roles for demonstration purposes. I maintain a repository with my essential
roles in another repository. (That can be includes in ansible.cfg.)
You need a working Python installation and pip installed.
pip install -r requirements.txtI add every host to at least one group in ansible_hosts. This can be cluster groups (e.g. consul-servers) or groups for a special purpose (e.g. streaming-servers).
Every role has a group too (e.g. role apache to install Apache on a server). So I can add hosts or groups to a group that belong to a specific role to execute the tasks of this role on this hosts.
Assume a group webservers with 4 hosts in ansible_hosts and a group workers:
[webservers]
web1 ansible_host=web1.servers.local primary_ipv4=192.168.1.1
web2 ansible_host=web2.servers.local primary_ipv4=192.168.1.2
web3 ansible_host=web3.servers.local primary_ipv4=192.168.1.3
web4 ansible_host=web4.servers.local primary_ipv4=192.168.1.4
[workers]
worker1 ansible_host=worker1.servers.local primary_ipv4=192.168.2.1(I use short names like web1 and explicitly set a long name with ansible_host. The variable primary_ipv4 is no official Ansible variable like ansible_host. But it's often a good choice to see the IP address of a host at frist sight and I use it in some roles.)
If I need to install Aapche on all webservers and worker hosts I add them to the apache group:
[apache:children]
webservers
workersIn site.yaml every role gets an entry like this:
- name: Install Apache
hosts: apache
become: yes
gather_facts: yes
strategy: free
roles:
- { role: apache, tags: ['apache'] }
After that it's easy to work on a specific host or group:
ansible-playbook site.yaml --limit webserversOr execute a specific tag like apache:
ansible-playbook site.yaml --tags "apache"If you need to set variables for groups or hosts create a folder with the name of the group in group_vars or of the host in host_vars. Put YAML files with your settings in these folders.