Ansible - Inventory
/etc/ansible/hosts | default inventory location |
-i |
specify inventory |
You can specify the location of your inventor file in your config file:
ansible.cfg
inventory=/opt/inv/hosts
Specify multiple inventory locations:
ansible-playbook test.yml -i /opt/inv/dev -i /opt/inv/prod
Basic Inventory Files
- Both INI and YAML formats can be used for Ansible inventory files.
- Host specific variables can be set in the config files.
Default groups:
- all - all hosts
- ungrouped - all hosts without a group
Here is an example of a INI formatted config file:
/etc/ansible/hostsmail.example.com [webservers] foo.example.com bar.example.com www[01:50].example.com [dbservers] one.example.com two.example.com host1 http_port=80 maxRequestsPerChild=808 badwolf.example.com:5309 # custom ssh ports (openssh, not paramiko) localhost ansible_connection=local
Here is an example of a YAML formatted config file:
/etc/ansible/hostsungrouped: hosts: mail.example.com: webservers: hosts: foo.example.com: bar.example.com: dbservers: hosts: one.example.com: two.example.com: three.example.com: host1: http_port: 80 maxRequestsPerChild: 808
Variables and Groups
- Sub groups can be created.
- Variables can be created on a group level.
INI format:
[web]
host1
host2
[web:vars]
port=8080
x=50
[db:children]
mysql
postgresql
[db:vars]
datadir="/data"
port=1234
[mysql]
test1
test2
[postgresql]
test3
test4
YAML format:
web:
hosts:
host1:
host2:
vars:
port: 8080
x: 50
db:
children:
mysql:
test1:
test2:
postgresql:
test3:
test4:
vars:
datadir: "/data"
port: 1234
Var Files
- Host and group variables can be defined in separate files.
- These files need to be in YAML format ( .yaml, .yml, .json )
- They are relative to the path of the inventory file
- Hosts and groups will have files or dirs named after them to contain their vars.
Group vars example paths:
/etc/ansible/group_vars/ | dir for group variables |
/etc/ansible/group_vars/all | all |
/etc/ansible/group_vars/web.yaml | var file for web group |
/etc/ansible/group_vars/db | dir for db group |
/etc/ansible/group_vars/db/data.yaml | var file for db group |
/etc/ansible/group_vars/db/control.yaml | var file for db group (another one) |
Host var example paths:
/etc/ansible/host_vars/ | dir for host variables |
/etc/ansible/host_vars/host1.yaml | vars for host1 |
/etc/ansible/host_vars/host2.yaml | vars for host2 |
/etc/ansible/host_vars/test1/ | dir for host test1 |
/etc/ansible/host_vars/test1/data1.yaml | file for host test1 |
/etc/ansible/host_vars/test1/data2.yaml | file for host test1 |
Dynamic Inventory
Using group_by for dynamic groups:
---
- name: talk to all hosts just so we can learn about them
hosts: all
tasks:
- name: Classify hosts depending on their OS distribution
group_by:
key: os_{{ ansible_facts['distribution'] }}
Now just on the CentOS hosts…
- hosts: os_CentOS
gather_facts: False
tasks:
- # tasks that only happen on CentOS go in this play
Variables file to include:
group_vars/os_CentOS.yml--- asdf: 42