Automate with Ansible: Part 2
Let’s ping some VMs with Ansible, bruh!
Previous
Using Virtualbox VMs
In this portion I’ll be using VMs I have setup on my local machine inside virtualbox to see if we can connect with Ansible.
If you already have virtualbox installed and need to make sure your VMs are able to talk to each other and/or your local machine, then check this out:
The only other thing that you will need for now is the ability to SSH into your VMs — See here:
We know there are a couple ways to SSH into your servers you can SSH in using just a password or you can ssh in using key-based authentication
.
Ansible recommends key-based authentication
and I do too, however I don’t want this tutorial to be a non-starter to where now you have to do all of that before you can start ansible so I’ll cover how to use a password for your servers and then in another Automate with Ansible section, I’ll show you how to create a task to set-up key-based authentication.
Create a directory called: ansible
mkdir ansible && cd ansible
Inside there make another directory called: inventory
mkdir inventory && cd inventory
Add a file called: hosts (don’t worry about an extension — it’s basically an .ini file)
touch hosts
You can use an IDE if you want, or nano, or whatever.
Open the file and give it a grouping label like this: [name]
. The name can be whatever you want, but it’s a grouping section for your servers. You can call it [servers]
, [ubuntu]
, or [farty]
— it’s really up to you.
For this example, since I’m using virtualbox, I’ll call it [vbox]
.
Under that, add the IP addresses of your VMs. Your hosts file should look something like this:
[vbox]
10.0.0.81
10.0.0.198
10.0.0.231
cd
back to the ansible directory and run the following command:
ansible -i ./inventory/hosts vbox -m ping --user morty --ask-pass
This command utilizes the hosts file, you then pass in the group name (vbox
), then we are using the module ping
, then the user (morty
, or whoever) on the server(s), and finally--ask-pass
which will prompt for a password that you type in.
Run the command.
If you get an error that asks you to install the sshpass
program, you’ll need to do that on your local machine (or whatever machine you are working from).
sudo apt install -y sshpass
Run the ansible command again:
ansible -i ./inventory/hosts vbox -m ping --user morty --ask-pass
You should see a response similar to this:
Next we will look at playbooks that will have specific tasks for ansible to perform on your VMs.