Note: This post presumes that you have completed the steps in the previous post Ansible Lab – Getting Started


Creating Our Inventory File

Now that we have our Ansible server and a Centos7 server node, we can run commands on the Ansible server to test connectivity. First things first, make sure you are logged into the Ansible server (snrans01.sonar.lan in my case) as root.

Ansible requires an inventory; a list of the nodes that it needs to manage. Let’s create a simple inventory file and a folder to store it in ( editor: we can make it more complicated later on! ). Enter the commands as shown below

mkdir /home/ansible
echo snransl01.sonar.lan >> /home/ansible/inventory
more /home/ansible/inventory

We now have our inventory file, albeit very basic. We can specify the inventory file when we use the Ansible cli. The commands will then target the listed entities, currently just the snransl01.sonar.lan server. We will add additonal servers later to demonstrate how we can groupthem into logical groups for Ansible to process.



Setting Up The Test Nodes SSH FingerPrint

We could go ahead and run our first Ansible command now. I tend to like to run the ping command to test Ansible out or use ‘touch’ to create a file in the target nodes /home folder. If you were to try this, you would be greeted with an error stating that the Host Key Checking is enabled.

SSH Error

We need to have the nodes ssh fingerprint added to our Ansible servers known_hosts file. This is simple to do. You can initiate an ssh connection from the Ansible server to the test node and when prompted accept the fingerprint. However, I prefer to do it a different way. Enter the command as shown below to obtain the fingerprint and store it in the known_hosts file.

mkdir ~/.ssh && ssh-keygen -H snransl01.sonar.lan >> ~/.ssh/known_hosts

The command will create a .ssh folder, obtain the test nodes SSH fingerprint and save it into the known_hosts file (which it will create).


Execute Our First Command

It is finally time to test out executing a command from the Ansible server to our test server. For this test, let’s create a file using touch. We are going to use the command as shown below

ansible snransl01.sonar.lan -i /home/ansible/inventory -u root -m command -a "touch /home/newfile" -k

What do the arguments mean?

snransl01.sonar.lanThe node listed in the inventory file that we want to issue the command to
-i /home/ansible/inventoryThe -i switch and following path/file are used to direct Ansible to the inventory file
-u root-u switch specifies the user to use on the target system when executing the command
-m command-m specified the Ansible module that we want to use. In this case I am using the command module so that I can issue a shell command on the target
-a "touch /home/newfile"-a is used to specify what command and arguments we want to pass to the command module
-kWe do not have the root password stored anywhere, therefore the -k switch is used to prompt us for it when the command is submitted to Ansible


Assuming that everything in the environment is configured correctly, execution of the command line above will result in a file being created on the test node. You will also see a message stating that the test node has been changed on the Ansible server. That was pretty simple wasn’t it?

Our first Ansible command creates a file on our test node








CIO at Sonar, Automation Practice Lead at Xtravirt and guitarist in The Waders. Loves IT, automation, programming, music

%d bloggers like this: