Create hosts file
In this How To I will show you how to create a hosts file for your ansible server. Ansible is all about it’s file structure. Much like Linux, Ansible performs it’s tasks based on files. These files are set in a specific folder structure, to run playbooks on specific hosts. These hosts are determined by the “hosts file”. The hosts file will at minimum contain a hostname or IP address of a target device Ansible want’s to manage. In this demo I will be using VSCODE and will have ansible version 2.9.2 running on my Ubuntu machine. I will also show you how to add a host to your /etc/hosts file on your Ubuntu machine. This file will do DNS resolution for your hosts that Ansible will manage. This is essential because Ansible uses SSH to connect to the managed host. SSH also must be installed on your machine. It also goes without saying that your machine must be able to PING the host Ansible will manage.
Instructions
In this demonstration i will use a 7200 series router in GNS3 connected to my Ubuntu machine through a GNS3 NAT cloud appliance.
First obtain the IP address and hostname of the device you wish to manage. In this example I am using the Cisco CLI to obtain the information needed.
Document the hostname and IP address of the host you want to manage. We are going to create the hosts file on our Ansible Server terminal using VIM.
Leave the documentation up. We’ll need the info for the next step. Open up the /etc/hosts using your editor of choice. In this file we will put the hostname and IP address. This file will enable your machine to do a name resolution for the hostname and IP address you input. You will need this record in the /etc/hosts file to SSH via hostname.
And that is it! Let’s check to see if Ansible can make an SSH connection to the host. Input this code on your terminal! ***NOTE*** If your ansible.cfg file is not configured this will not work. Click this link to learn how to setup your ansible.cfg file! ansible.cfg
This command is an “ansible ad-hoc” command. This will command will SSH to the cisco device.
If successful the terminal should return this output.
Related articles
If you want to reference an Ansible repo check out my git repo’s here! ansible
Last updated