Ansible can work with one or multiple systems in your infra at the same time. In order to work with multiple servers at the same time Ansible needs to establish connection to those server. This is done using SSH for Linux and PowerShell for Windows. This is what makes ansible agentless, meaning you don't need to install any additional software on the target machine to be able to work with ansible.
Ansible uses an inventory file to keep track hosts which are part of your infrastructure, and how to reach them for running commands and playbooks.
The inventory file contains information about the hosts you are going to manage with the Ansible. You can have one to several hundred servers in your inventory file. These hosts can be organized into groups and subgroups. The inventory file is also often used to set variables that will be valid only for specific hosts or groups, in order to be used within playbooks and templates.
When you install Ansible the default location for the inventory file is /etc/ansible/hosts. Ansible also gives you the flexibility to create a custom inventory file at your preferred location on your control node. The most common formats are INI and YAML.
A basic INI /etc/ansible/hosts might look like this -
The headings in brackets are group names, which are used in classifying hosts and deciding what hosts you are controlling at what times and for what purpose. You can have multiple groups defined in a single Inventory file.
There are multiple inventory parameters like -
ansible_host- Specifies FQDN or IP address of the server
ansible_connection - Define how ansible connect to target server (ssh/winrm/localhost)
ansible_port - Define which port to connect to ( default port for SSH - 22 )
ansible_user - Define the user to make remote connection( Default is root for linux)
ansible_ssh_pass - Define the SSH password for Linux
The Ansible inventory file name can be anything you want as long as you reference it in the ansible.cfg file.
You can have multiple inventories and specify to use each inventory at the playbook runtime, so you basically have a hosts file per project.
Ansible Inventory File Best Practices -
- Keep your hosts file name simple.
- Put comments in the file wherever necessary to ensure someone else can understand your inventory.
- Use children to address multiple groups at once.
- Do no put passwords in your inventory file.
Now let's quickly test ping connectivity from Ansible server to the target server.
1. From you Ansible server, first try to SSH to the target server and confirm the key fingerprint to add the target server to the list of known hosts.
2. In the next step create a Inventory file. In my case I already have a folder named test-project and I'm going to create a inventory file inside this folder
3. Inventory file will have one entry for target host i.e. target1. We can add multiple targets, but for now I've added only one target. Then you'd need to specify the target host IP using ansible_host attribute and then specify the password which is require to connect to the target server using the attribute ansible_ssh_pass. Save the Inventory file.
4. Now lets test the ping connectivity using the command ansible target1 -m ping inventory.txt
As you see it returned a pong message. This confirms our ansible server can successfully communicates with the target server.
With this let's wrap up this post here.
Thank you for reading!
*** Explore | Share | Grow ***
Comments