Ansible Inventory: teoría + ejercicios resueltos
Introducción
Guía de lectura sobre inventarios:
Resumen: el formato del inventario puede ser en INI o YAML principalmente, y dependiendo de si se usa uno u otro, la forma de definir variables, grupos de hosts, etc cambia. El inventario por defecto se encuentra en /etc/ansible/hosts
. Las variables que se pueden usar estan en Connecting to hosts: behavioral inventory parameters
The simplest inventory is a single file with a list of hosts and groups. The default location for this file is /etc/ansible/hosts
. You can specify a different inventory file at the command line using the -i <path>
option or in configuration using inventory
.
Guía para construir el archivo inventario: How to build your inventory
Even if you do not define any groups in your inventory file, Ansible creates two default groups: all
and ungrouped
. The all
group contains every host. The ungrouped
group contains all hosts that don’t have another group aside from all
. Every host will always belong to at least 2 groups (all
and ungrouped
or all
and some other group)
Example of inventory in YAML format:
leafs:
hosts:
leaf01:
ansible_host: 192.0.2.100
leaf02:
ansible_host: 192.0.2.110
spines:
hosts:
spine01:
ansible_host: 192.0.2.120
spine02:
ansible_host: 192.0.2.130
network:
children:
leafs:
spines:
webservers:
hosts:
webserver01:
ansible_host: 192.0.2.140
webserver02:
ansible_host: 192.0.2.150
datacenter:
children:
network:
webservers:
Example of inventory in INI format
[atlanta]
host1
host2
[atlanta:vars]
ntp_server=ntp.atlanta.example.com
proxy=proxy.atlanta.example.com
Ejercicios
- We have a sample inventory file with 3 servers listed. Add a fourth server by the name server4.company.com
# Sample Inventory File server1.company.com server2.company.com server3.company.com
Solución
# Sample Inventory File server1.company.com server2.company.com server3.company.com server4.company.com
-
We have added aliases named
web1
,web2
andweb3
for the first three servers. Update server4 to have an aliasdb1
# Sample Inventory File web1 ansible_host=server1.company.com web2 ansible_host=server2.company.com web3 ansible_host=server3.company.com server4.company.com
Solución:
# Sample Inventory File web1 ansible_host=server1.company.com web2 ansible_host=server2.company.com web3 ansible_host=server3.company.com db1 ansible_host=server4.company.com
-
The web servers are linux, but the db server is windows. Add additional parameters in each line to add
ansible_connection
,ansible_user
andpassword
. Use the below table for information about credentials.Alias Host Connection User Password web1 server1.company.com SSH root Password123! web2 server2.company.com SSH root Password123! web3 server3.company.com SSH root Password123! db1 server4.company.com Windows administrator Password123! Note: For linux use
ansible_ssh_pass
and for windows useansible_password
. Connector for windows iswinrm
# Sample Inventory File web1 ansible_host=server1.company.com web2 ansible_host=server2.company.com web3 ansible_host=server3.company.com db1 ansible_host=server4.company.com
Solución:
# Sample Inventory File # Web Servers web1 ansible_host=server1.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web2 ansible_host=server2.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web3 ansible_host=server3.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! # Database Servers db1 ansible_host=server4.company.com ansible_connection=winrm ansible_user=administrator ansible_password=Password123!
- We have created a group for web servers. Similarly create a group for database servers named
db_servers
and adddb1
server to it.
# Sample Inventory File # Web Servers web1 ansible_host=server1.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web2 ansible_host=server2.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web3 ansible_host=server3.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! # Database Servers db1 ansible_host=server4.company.com ansible_connection=winrm ansible_user=administrator ansible_password=Password123! [web_servers] web1 web2 web3
Solución:
# Sample Inventory File # Web Servers web1 ansible_host=server1.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web2 ansible_host=server2.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web3 ansible_host=server3.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! # Database Servers db1 ansible_host=server4.company.com ansible_connection=winrm ansible_user=administrator ansible_password=Password123! [web_servers] web1 web2 web3 [db_servers] db1
-
Let us now create a group of groups. Create a new group called
all_servers
and add the previously created groupsweb_servers
anddb_servers
to it.Note: Syntax:
[parent_group:children]
child_group1
child_group2# Sample Inventory File # Web Servers web1 ansible_host=server1.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web2 ansible_host=server2.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web3 ansible_host=server3.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! # Database Servers db1 ansible_host=server4.company.com ansible_connection=winrm ansible_user=administrator ansible_password=Password123! [web_servers] web1 web2 web3 [db_servers] db1
Solución:
# Sample Inventory File # Web Servers web1 ansible_host=server1.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web2 ansible_host=server2.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! web3 ansible_host=server3.company.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Password123! # Database Servers db1 ansible_host=server4.company.com ansible_connection=winrm ansible_user=administrator ansible_password=Password123! [web_servers] web1 web2 web3 [db_servers] db1 [all_servers:children] web_servers db_servers
-
Try and represent the data given in the below table in Ansible Inventory format
Server Alias Server Name OS User Password sql_db1 sql01.xyz.com Linux root Lin$Pass sql_db2 sql02.xyz.com Linux root Lin$Pass web_node1 web01.xyz.com Win administrator Win$Pass web_node2 web02.xyz.com Win administrator Win$Pass web_node3 web03.xyz.com Win administrator Win$Pass Group the servers together based on this table
Group Members db_nodes sql_db1, sql_db2 web_nodes web_node1, web_node2, web_node3 boston_nodes sql_db1, web_node1 dallas_nodes sql_db2, web_node2, web_node3 us_nodes boston_nodes, dallas_nodes # Sample Inventory File # Web Servers web_node1 ansible_host=web01.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass web_node2 ansible_host=web02.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass web_node3 ansible_host=web03.xyz.com ansible_connection=winrm ansible_user=administrator ansible_password=Win$Pass # DB Servers sql_db1 ansible_host=sql01.xyz.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Lin$Pass sql_db2 ansible_host=sql02.xyz.com ansible_connection=ssh ansible_user=root ansible_ssh_pass=Lin$Pass # Groups [db_nodes] sql_db1 sql_db2 [web_nodes] web_node1 web_node2 web_node3 [boston_nodes] sql_db1 web_node1 [dallas_nodes] sql_db2 web_node2 web_node3 [us_nodes:children] boston_nodes dallas_nodes