Ansbile Inventory
- Inventory is list of machines (hosts)
- /etc/ansible/hots default Inventory path
An Inventory can defined
- hosts: Individual severs
10.10.1.0
10.20.2.0
- groups: collections of hosts (webservers and dbservers)
[dev]
10.0.1.0
10.0.2.0
10.0.3.0
10.0.4.0
[webservers]
10.2.1.0
10.3.2.0
10.4.3.0
10.56.4.0
- variables: setting attachment for hosts or groups (port, users, password, app version …etc)
- nested Groups: groups of groups
[production]
backend
webservers
[backend]
10.0.1.0
10.0.2.0
10.0.3.0
10.0.4.0
[webservers]
10.2.1.0
10.3.2.0
10.4.3.0
10.56.4.0
Validate connection ansible all -i Inventory_file -m ping
Types of Inventory
- Static inventory:
- a fixed file (INI or yaml ) that can be edit by you
- best for small and stable enviroment onprem
- Dynamic inventory
- could api to fetch nodes ip address (run scrpit and generate inventory file)
- best for cloud and autoscalling enviroment
- Inventory Directory
- A Directory conatianing multiple files/scrpits combine together (both Static and Dynamic)
- best for cloud and on-prem mixed enviroments
- INLINE/ ad-hoc inventory
- host pass Directly from cmd line
- best for Quick test
Static Inventory:
- plan text add all hosts and groups
Ex: INI format
backend-api.ini.com
10.55.55.6 anasible_user=ansible ansible_password=ansible@123
[backend]
10.0.1.0
10.0.2.0 ansible_port=23
10.0.3.0 ansible_pasword=ansible@123
10.0.4.0
[webservers]
10.2.1.0
10.3.2.0
10.4.3.0
10.56.4.0
[webservers:vars]
anasible_user=ubuntu
ansible_password=ansible@123
[all:vars]
ansible_port=22
same in yaml format
all:
vars:
ansible_port: 22
hosts:
backend-api.ini.com:
ansible_host: 10.55.55.6
ansible_user: ansible
ansible_password: "ansible@123"
children:
backend:
hosts:
10.0.1.0:
10.0.2.0:
ansible_port: 23
10.0.3.0:
ansible_password: "ansible@123"
10.0.4.0:
webservers:
hosts:
10.2.1.0:
10.3.2.0:
10.4.3.0:
10.56.4.0:
vars:
ansible_user: ubuntu
ansible_password: "ansible@123"
Host range patter
[webservers]
web[01:50].amazon.in # web01-web50
web01.amazon.in
web02.amazon.in
web03.amazon.in
...
web50.amazon.in
[dbservers]
10.0.20.[05:155] # 10.0.20.05 --> 10.0.20.155
10.0.20.05
10.0.20.06
10.0.20.07
10.0.20.155
Common Inventory (Connection) Variables
| Variable |
Purpose |
Example |
ansible_host |
Real IP/DNS to connect to (alias differs from address) |
ansible_host=10.0.1.15 |
ansible_port |
SSH port |
ansible_port=2222 |
ansible_user |
Remote login user |
ansible_user=ubuntu |
ansible_password |
SSH password (prefer Vault) |
ansible_password=secret |
ansible_ssh_private_key_file |
SSH key path |
~/.ssh/id_rsa |
ansible_connection |
Connection type |
ssh, local, winrm, docker |
ansible_become |
Use privilege escalation |
true |
ansible_become_user |
User to become |
root |
ansible_python_interpreter |
Python path on the target |
/usr/bin/python3 |
Task:
- create Static inventory and validate above methods like host, groups, all, variables…etc