Friday, September 11, 2020

Ansible - Configuration File and Inventory

Introduction

In this second post from the ansible series, we are going to check and configure the ansible configuration file and also the inventory file. The configuration file is for ansible parameters whereas the inventory file consists of hosts or the ansible client machines.

Ansible Configuration File

Ansible looks for configuration in four locations as listed below. 

  • ANSIBLE_CONFIG (environment variable)
  • ansible.cfg (current directory)
  • ~/.ansible.cfg (home directory)
  • /etc/ansible/ansible.cfg

If ansible configuration is found in the environment variable other configurations will be ignored. The process is similar for all configurations in other locations downwards.

For this series we will be using the second method. 

The working directory:
 [devops@anscontrol ansible]$ pwd  
 /home/devops/ansible  
The configuration file:
 [devops@anscontrol ansible]$ cat ansible.cfg  
 [defaults]  
 inventory = /home/devops/ansible/hosts  
 remote_user = devops  
   
 [privilege_escalation]  
 become = True  
Two sections are listed in the ansible.cfg file. First is the defaults section, where the inventory file and remote user on the client machines are listed. Second is the privilege_escalation section, where we allow the remote user to run commands as a privileged user.

Inventory File

In the ansible.cfg file we have listed the inventory file and named it hosts. So, we have to create a file named hosts with a list of all the ansible clients.

The contents of the hosts file:
 [devops@anscontrol ansible]$ cat hosts  
 [jenkins]  
 192.168.200.11  
   
 [tomcat]  
 192.168.200.12  
   
 [ansible-control]  
 192.168.200.13  
   
 [docker]  
 192.168.200.14  
   
 [kubemaster]  
 192.168.200.15  
   
 [kubenodes]  
 192.168.200.16  
 192.168.200.17  
   
 [nagios]  
 192.168.200.18  
   
 [ansible-clients]  
 192.168.200.19  
 192.168.200.20  
 192.168.200.21  

Listing Hosts

We can finally test our configuration by listing the hosts or ansible clients.
 [devops@anscontrol ansible]$ ansible kubemaster --list-hosts  
  hosts (1):  
   192.168.200.15  
   
 [devops@anscontrol ansible]$ ansible ansible-clients --list-hosts  
  hosts (3):  
   192.168.200.19  
   192.168.200.20  
   192.168.200.21  

In the next post we will run some ad hoc commands.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.