Getting started with AnsibleAnsible is a Red Hat owned tool for automating system administration tasks. It is typically used in environments where an administrator wants to perform the same task, such as deploying security updates, on many computers without logging into each computer manually. Unlike many automation tools, Ansible does not require any special software to be installed on each client machine. Each client just needs the OpenSSH service to be installed on the clients and all the work and configuration is handled by one central server.
There are a lot of reasons for working with Ansible and this guide is meant to get you up and running quickly. If you're like me, I have a terrible habit of not reading the fine manual. To quote the Simpsons character Renier Wolfcastle, "I was elected to lead not to read". To follow along with this tutorial here are the basics you will need:
Enough RAM and disk space for your virtual machines.
Virtual apps such as VirtualBox, VMware, KVM or whatever your preference.
Minimum number of required VMs are two, one as the primary where all the scripts/playbooks are kept and a secondary machine that will receive the changes pushed from the primary machine.
For this example I am using VirtualBox and Rocky Linux as my operating system. You can set up Ansible with almost any operating systems you want. As my reasons for doing things in this example are due to my job, which will require me to update various Red Hat servers, I am sticking with a Red Hat clone.
Before you do anything, you need to setup an environment to use Ansible. In VirtualBox I created a primary machine named Bullwinkle and four secondary machines named Rocky One, Rocky Two, etc. Naming is not important, you can name it whatever you want. Machine One, Bruins One, etc. Again, you only need one secondary machine, for this example I created four.
Note: I did not install desktops on any of these VMs, they were strictly command line only because it is rare that you will find servers with desktops. Also, without desktops the VMs have a much smaller resource footprint.
The quickest way to create multiple VMs: create the first one in VirtualBox and once you have created the VM and updated the operating system, you can clone the first one to as many machines as you want, just be sure to rename them with unique names. Once you have the VMs in place, you will have to make sure each one has a unique IP address because the first thing you will notice is they all have the same IP address. I used the following tutorial from our friends at Dedoimedo.
At this point, I stopped here in the instructions because the next steps included generating an RSA secure key and pushing it to the secondary machines. I struggled through this section without any success.
Note: The creation of secure keys is a best practice and should not be overlooked, especially if you plan to execute this in production! I was working in a controlled environment at home and I skipped this part, but the work around is provided in the video below in the next paragraph.
The hosts file should have been placed in the roles directory so if you are seeing this outside of the directory just move it to the correct location. The hosts file is where you will list your managed secondary machines. You will need to update this file to create a reference point for your playbook which will follow shortly. The default hosts file has three distinct examples (Ex 1: ungrouped hosts, Ex 2: web servers, Ex 3: db servers. As my example didn't fit the last two examples, I chose the first group and labeled it "ansible_clients" The sample of what this looks like is below. I have added two of my secondary machines as noted in the lines "#rocky one machine" and "#rocky two machine" and under each commented line you see the following:
#rocky one machine 10.0.2.15 ansible_ssh_user=root ansible_ssh_pass=rockyone #rocky two machine 10.0.2.5 ansible_ssh_user=root ansible_ssh_pass=rockytwo
Now that the hosts file has been updated we'll create a playbook. A playbook is what will push down commands to your secondary machines. The playbooks are written in YAML and is very easy to write but spacing is important so note that in the screenshot. I wanted to create a playbook that updates my secondary machines with the latest updates and also to install the EPEL repository. Using the vi editor I created the following playbook: server_updates_all_playbook.yml. It makes sense to name the playbook with what you want to run. In this case as I want to push updates to all machines, the name server_updates_all makes sense to me. Remember when you save the file, be sure the file is a dot yml file (server_updates_all_playbook.yml) The example of this playbook is below:
Instructions for installing updates through Ansible (full image size: 8kB, resolution: 597x477 pixels)
Now that your file is in place, you always want to test it to make sure it will run properly so you will need to run the following command using my playbook as the file to check:
Now its ready to run. From the command line put in the following command:
The screenshot below shows a successful execution of the playbook. Please keep in mind the last line of output is important because if the playbook had failed you would get output letting you know where it failed.
Progress and status report from Ansible (full image size: 11kB, resolution: 796x594 pixels)