Welcome to part 3 of my Introduction to Ansible blog post series! If you have not read part 1 and part 2 of the Introduction to Ansible series, you can find it here:
- Part 1: Here I cover what Ansible is and how to find your way around the Ansible documentation
- Part 2: Here I cover how to install Ansible on Ubuntu on DigitalOcean and some of the main configuration files that you need to keep in mind
In this part, we will focus on what Ansible Ad-hoc commands are and we will introduce a few basic commands so make sure to have your 3 servers up and running and be able to follow along!
To make things even better you can use my referral link to get a free $100 credit that you could use to deploy your virtual machines and test the guide yourself on a few DigitalOcean servers:
There are two ways of running Ansible commands:Checkout our latest product - the ultimate tailwindcss page creator 🚀
- Ad-hoc commands
The Ad-hoc commands are just like single bash command that we want to execute.
Playbooks on the other side are like scripts and can target different node groups.
To run Ansible Ad-hoc commands you can just use the ansible command and then specify a host and the command that you want to run.
For example, if you want to check the disk space on a specific remote server you could run:
ansible your_host_name_here -i hosts -a "df -h"
The output that you would get would look something like this:
You can find a link to the official Ansible ad-hoc documentation here:
You can also run modules. This is almost the same but we use -m flag rather than the -a flag for an action, for example:
ansible your_host_name_here -m setup
What the Ansible setup module does is it hooks up with the host and shows us all of the information that Ansible master has about the host.
As you can see this returns a lot of information that can be used in countless different ways.
Here's another Ad-hoc command example:
ansible your_host_name_here -m ping
This pings our host and that way we can test the connectivity to the host from the master server and make sure that it actually works.
We can also install some packages on our hosts using Ansible ad-hoc commands.
For example, let's say that we wanted to install Apache on our webserver, what we would need to do is just run the following command:
ansible your_host_name_here -b -m apt -a "name=apache2 state=latest"
Rundown of the command:
-b - means become which essentially tells Ansible that the command should be executed with sudo privileges
-m - specifies the module that we want to use
-a - specifies the flags, in our case we specify that we want to install the latest apache2 version
We could also start the service with the Ansible service module:
ansible host-name -b -m service -a "name=apache2 state=started"
Once you've read this post, make sure to test your knowledge with this Ansible Ad-hoc commands Quiz:
That's pretty much it for part 3! Now you know what Ansible ad-hoc commands and how to run them! You even installed Apache on your webserver using Ansible!
In part 4 we will cover the actual power of Ansible with the Ansible Playbooks!
Feel free to reach out to me in case you have any questions!