Ansible is an open-source automation technology that has become very popular in the past few years. Ansible and Ansible Tower (the commercial version) automate configuration management and application deployment for systems on a private cloud or traditional infrastructure.
Ansible can be used to manage both Windows and Linux environments. This blog post will introduce how Ansible connects to hosts, providing some basic information about what Ansible does as well as some of the features offered by Ansible.
Ansible communicates with hosts using the SSH protocol because it is available on all Linux, macOS, routers and switches by default. This means that Ansible can be used to manage systems that are not accessible from the machine where Ansible is running. Ansible can also be used to manage systems that are not accessible through the network. Ansible uses SSH keys to authenticate with hosts.
Ansible also requires a working installation of Python. The version of Python required by Ansible is typically included in most Linux distributions and macOS X versions. If you are running Windows, you will need to install Python separately. For more information, see the Ansible documentation on Windows support.
Once Ansible is installed, you can run it from the command line.
Ansible will automatically look for your Ansible inventory file in the current working directory. If you do not provide an inventory file, Ansible will use the default hosts file located at /etc/ansible/hosts.
The Ansible command-line tool has a number of options that can be used to control how Ansible connects to hosts and the modules that Ansible uses.
Below is an entry from my SSH config file for a Linux VM in Azure.
To connect to that machine, I will put an entry in my inventory file that looks like:
From here, connecting to the machine with Ansible and running a playbook is simple, as shown below, I’m using the –limit option to only run the playbook against the servers group in the inventory file.
ansible-playbook -i ./hosts ./playbooks/install-git.yml --limit servers
If you do not provide an inventory file, Ansible will use the default hosts file located at /etc/ansible/hosts. Ansible can be configured to automatically look for a custom or even multiple inventories by using the -i flag followed by the Ansible inventory file name.
The Ansible inventory file is a text file that contains a list of hosts grouped into sections. The format for the Ansible inventory file is as follows:
hosts = host_list
The group_name is optional and can be used to organize hosts into groups. The host_list is a comma-separated list of hosts or host aliases. For example:
The Ansible inventory file shown above contains two groups, webservers and dbservers. The hosts in the webservers group are 192.168.0.20 and 192.168.0.21, while the hosts in the dbservers group are 172.16.0.50. Ansible will use this inventory file to connect to the hosts.