Learning DevOps
上QQ阅读APP看书,第一时间看更新

Configuring hosts in the inventory

As we have seen, the entire Ansible configuration is in the ansible.cfg file. However, this configuration is generic and applies to all Ansible executions as well as connectivity to hosts.

However, when using Ansible to configure VMs from different environments or roles with different permissions, it is important to have different connectivity configurations, such as different admin users and SSL keys per environment. For this reason, it is possible to override the default Ansible configuration in the inventory file by configuring specific parameters per host defined in this inventory.

The main configuration parameters that can be overridden are as follows:

  • ansible_user: This is the user who connects to the remote host.
  • ansible_port: It is possible to change the default value of the SSH port.
  • ansible_host: This is an alias for the host.
  • ansible_connection: This is the type of connection to the remote host and can be Paramiko, SSH, or local.
  • ansible_private_key_file: This is the private key used to connect to the remote host.
The complete list of parameters is available in the following documentation:  https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html#list-of-behavioral-inventory-parameters.

Here is an example of an inventory in which we configure the connection of hosts:

[webserver]
webserver1 ansible_host=192.10.20.31 ansible_port=2222
webserver2 ansible_host=192.10.20.31 ansible_port=2222

[database]
database1 ansible_host=192.20.34.20 ansible_user=databaseuser
database2 ansible_host=192.20.34.21 ansible_user=databaseuser

[dev]
webserver1
database1

[qa]
webserver2
database2

The following can be seen in this inventory example:

  • The connection information has been specified beside each host.
  • The alias implementation (such as webserver1 and webserver2) is used in another group (such as the qa group in this example).

Having completed the implementation of an Ansible inventory, we will now look at how to test this inventory.