Ansible Lab – Testing Connectivity
In the previous post, we set up an Ansible server and a CentOS 7 node to begin our automation journey. Now that we have our Ansible server and a test node, we can start testing connectivity and running commands on the Ansible server.
Creating an Inventory File
Ansible requires an inventory file, a list of nodes that it needs to manage. Let’s create a simple inventory file and a folder to store it in.
Enter the following commands:
“`
mkdir -p ~/ansible/inventory
touch ~/ansible/inventory/hosts
“`
The `mkdir` command creates a new directory called `ansible/inventory`, and the `touch` command creates a new file called `hosts` in that directory.
Creating a Basic Inventory File
Let’s create a basic inventory file with one host, our test node `snransl01.sonar.lan`. Open the `hosts` file in your favorite text editor:
“`
nano ~/ansible/inventory/hosts
“`
Add the following line to the file:
“`
[snransl01]
localhost ansible_user=root ansible_password=
“`
Replace `
Specifying the Inventory File
When we use the Ansible CLI, we need to specify the inventory file. Let’s try running our first Ansible command:
“`
ansible -i ~/ansible/inventory/hosts snransl01 -m touch /home/root/test_file
“`
The `-i` flag specifies the inventory file, and `snransl01` is the host we want to execute the command on. The `-m` flag specifies the module we want to use (in this case, `touch`). The `/home/root/test_file` is the path where Ansible will create a test file.
Host Key Checking is Enabled
If you try to run the previous command, you’ll encounter an error stating that Host Key Checking is enabled and SSH Error. This is because Ansible needs to add the host’s SSH fingerprint to its known_hosts file.
Adding the Host SSH Fingerprint
We can add the host’s SSH fingerprint to the known_hosts file in two ways:
1. Accept the fingerprint when prompted during an SSH connection.
2. Use the `ansible-doc` command to obtain the fingerprint and store it in the known_hosts file.
Let’s use the second method. Enter the following command:
“`
ansible-doc -t ssh snransl01 | awk ‘/ssh/ {‘print $3’} | tr -d ‘n’ > ~/ansible/inventory/known_hosts
“`
This command obtains the SSH fingerprint for `snransl01` and stores it in the known_hosts file.
Executing a Command on the Test Node
Now that we have added the host SSH fingerprint to the known_hosts file, let’s test executing a command on the test node. We’ll use the `touch` module to create a file on the node:
“`
ansible -i ~/ansible/inventory/hosts snransl01 -m touch /home/root/test_file2
“`
If everything is set up correctly, execution of this command will result in a file being created on the test node. You’ll 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!
Conclusion
In this post, we tested connectivity between the Ansible server and our test node, added the host SSH fingerprint to the known_hosts file, and executed our first Ansible command. We demonstrated how to create an inventory file, specify the inventory file when using the Ansible CLI, and how to add the host SSH fingerprint to the known_hosts file.
Stay tuned for the next post where we’ll explore more advanced features of Ansible, such as group management and idempotency.