L’inventaire contient une liste de serveurs
# inventory
server1.company.com
server2.company.com
Ces serveurs peuvent ensuite être utilisés dans le playbook via la directive hosts
# playbook.yml
- hosts: server1.company.com
Si on a beaucoup d’hôtes qui suivent un même motif, on peut les déclarer comme suit:
# inventory
www[01:50].example.com
Pour chaque serveur, on peut spécifier un alias
Note: ansible_ssh_host
est déprécié en faveur de ansible_host
# inventory
web1 ansible_host=server1.company.com
db ansible_host=server2.company.com
mail ansible_host=server3.company.com
web2 ansible_host=server4.company.com
Et utiliser cet alias dans le playbook
# playbook.yml
- hosts: web1
On peut créer des groupes de serveurs, avec une section [GROUP_NAME]
# inventory
[web_servers]
web1 server1.company.com
web2 server2.company.com
[mail_servers]
mail server3.company.com
[db_servers]
db server4.company.com
ou
# inventory
web1 server1.company.com
web2 server2.company.com
mail server3.company.com
db server4.company.com
[web_servers]
web1
web2
[mail_servers]
mail
[db_servers]
db
Et utiliser le groupe dans le playbook pour cibler plusieurs serveurs
# playbook.yml
- hosts: web_servers
Pour créer un groupe de groupes, on utilise la syntaxe [GROUP_NAME:children]
# inventory
...
[all_servers:children]
web_servers
db_servers
# playbook.yml
- hosts: all_servers
all
est un mot-clé qui désigne l’ensemble des serveurs définis dans l’inventaire — sans qu’il soit nécessaire de créer un groupe / groupe de groupes
# playbook.yml
- hosts: all
Ou on peut désigner plusieurs hôtes en les séparant par des virgules
# playbook.yml
- hosts: server1,server2,server3
localhost
peut être utilisé pour exécuter des actions sur le serveur où est installé Ansible
# inventory
localhost ansible_connection=local
ou
# playbook.yml
---
- name: 'Exec cmd on localhost'
hosts: localhost
connection: local
tasks:
- name: 'Display content of /etc/resolv.conf'
command: cat /etc/resolv.conf
On peut également définir la variable d’environement ANSIBLE_INVENTORY
$ export ANSIBLE_INVENTORY=/tmp/inventory
$ ansible-playbook playbook.yml
On peut utiliser un (ou plusieurs) inventory, autre que celui par défaut, en utilisant l’option -i
ansible-playbook playbook.yml -i staging -i production