Install Ansible on Ubuntu Server to automate Linux server deployments
How many Linux machines do you manage on your network or cloud-hosted platform? These days, that number is probably growing pretty quickly, especially considering that businesses depend not only on Linux for regular services, but also containerized and cloud-native deployments.
So yes, these Linux machines are probably growing exponentially every week or every month. This means that you have more and more machines to manage, which can be time consuming. Since your day is already busy, you don’t need to log into each machine and run commands manually.
With that in mind, what do you do? One solution is to turn to a centralized configuration management tool, like Red Hat’s Ansible. One of the best things about Ansible is that it uses SSH and YAML files to handle the heavy lifting remotely. This means you don’t have to worry about installing agents on the servers you need to manage, because everything is managed through the controller.
I’ll walk you through installing and configuring Ansible on Ubuntu Server, then show you how to use the platform to run your first Ansible playbook.
What you will need
I will demonstrate this setup entirely with Ubuntu Server installations, specifically Ubuntu Server 22.04. You can use Ansible with other operating systems, but since Ubuntu is my benchmark, that’s what I choose to use. Additionally, Ansible is incredibly easy to install on Ubuntu.
That said, are you ready to get to work? I thought so.
Installing Ansible
Connect to your Ubuntu Server 22.04 instance and install Ansible with the command:
sudo apt-get install ansible -y
The above command will also retrieve all the dependencies needed to get ansible up and running. There is, however, another piece of software that we will be installing, which is sshpass. SSHpass is a non-interactive ssh password provider, so you can configure your remote server inventory with passwords for easier use of Ansible.
To install sshpass, run the command:
sudo apt-get install sshpass -y
And that’s all the software you need to install.
Creating your inventory file
I will show you how to create an inventory with a single server. You can add as many servers as you need to this file, just be sure to divide them into categories (such as web, dev, database, etc.), so you have more control over the configuration.
First, create a new directory to host the inventory file with the command:
sudo mkdir /etc/ansible
Next, create your inventory file with:
sudo nano /etc/ansible/hosts
This is where things take a turn for the specific. Ansible requires you to configure your hosts in a very particular way. I will create an entry called testServer for a machine at IP address 192.168.1.13, the user socket and an ssh password of [email protected] This inventory file would look like this:
[testServer]192.168.1.13 [testServer:vars]ansible_user=jack ansible_ssh_pass= [email protected] [all:vars]ansible_python_interpreter=/usr/bin/python3 ansible_sudo_pass: SUDO_PASSWORD
I>[testServer] /I>I>192.168.1.13/I> I>[testServer:vars] /I>I>ansible_user=jack /I>I>ansible_ssh_pass= Th3N3w$you@ck/I> I>[all:vars] /I>I>ansible_python_interpreter=/usr/trash can/python3 /I>I>ansible_sudo_pass: SUDO_PASSWORD/I> |
The bottom section defines variables for all servers and tells Ansible to use Python3 instead of the default Python. Be sure to insert your user’s sudo password there.
Impressive.
Let’s test our inventory. To do this, issue the command:
ansible all -m ping
The output of the above command should look like this:
192.168.1.13 | SUCCESS => { “modified”: false, “ping”: “pong” }
192.168.1.13 | HIT => { “amended”: fake, “ping”: “sponge” } |
The SUCCESS output is what you are looking for. You see that… you are golden.
Create and run a playbook
Now let’s create our first Ansible playbook. We are going to create a simple playbook that will install a full LAMP stack on our Ubuntu servers. It is very important that you indent this playbook perfectly, because it is a YAML file and it will fail if the indentation is not correct.
Create the new playbook with the command:
nano lampstack.yaml
In this file, paste the following:
#Install LAMP stack on Ubuntu Server – hosts: testServer tasks: – name: install lamp stack become: yes apt: pkg: – apache2 – mysql-server – php – php-mysql-status: present update_cache: yes – name: start apache service become: yes become_user: jack service: name: apache2 status: started enabled: yes – name: start service mysql become: yes become_user: jack service: name: mysql status: started enabled: yes
1 2 3 4 5 6 seven 8 9 ten 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
#Install LAMP stack on Ubuntu server – hosts: testServer Tasks: – Last name: install lamp stack become: yes apt: pack: – apache2 – mysql–waiter – php – php–mysql State: present update_cache: yes – Last name: beginning apache service become: yes become_user: jack service: Last name: apache2 State: started activated: yes – Last name: beginning mysql service become: yes become_user: jack service: Last name: mysql State: started activated: yes |
Save and close the file. No, you’ll want to change jack above to a user with sudo privileges on your server.
Once your playbook has been created, you can now run it with the command:
ansible-playbook lampstack.yaml --user=jack --extra-vars ansible_sudo_pass="[email protected]"
Again, you’ll want to switch jacks and [email protected] with a user and password that actually works on your server.
As the playbook runs, you should see output like this:
TO PLAY [testServer] ************************************************** ************************************************* ******** ***************************************** ******* TASK [Gathering Facts] ************************************************** ************************************************* ******** ***************************************** ******* Okay : [192.168.1.13]TASK [install lamp stack] ************************************************** ************************************************* ******** ***************************************** ******* Okay : [192.168.1.13]TASK [start apache service] ************************************************** ************************************************* ******** ***************************************** ******* Okay : [192.168.1.13]TASK [start mysql service] ************************************************** ************************************************* ******** ***************************************** ******* Okay : [192.168.1.13]READ THE SUMMARY *********************************************** *************************************************** ************************************************** ********* * 192.168.1.13: ok=4 modified=0 unreachable=0 failed=0 skipped=0 saved=0 skipped=0
TO PLAY [testServer] ************************************************** ************************************************* ******** ***************************************** ******* TASK [Gathering Facts] ************************************************** ************************************************* ******** ***************************************** ******* Okay: [192.168.1.13] TASK [install lamp stack] ************************************************** ************************************************* ******** ***************************************** ******* Okay: [192.168.1.13] TASK [start apache service] ************************************************** ************************************************* ******** ***************************************** ******* Okay: [192.168.1.13] TASK [start mysql service] ************************************************** ************************************************* ******** ***************************************** ******* Okay: [192.168.1.13] TO PLAY TO SUM UP *************************************************** ************************************************* ******** ***************************************** ****** 192.168.1.13 : Okay=4 amended=0 inaccessible=0 lack=0 jumped up=0 Safe=0 ignored=0 |
The playbook should run (this will probably take a while), and when it’s done, you can point a web browser to the server’s IP address in your inventory to see the Apache homepage on the remote server.
And that’s all there is to installing Ansible and using it to manage remote servers. For more information on Ansible Playbooks, be sure to check out the official documentation.
Feature image: GetVectorLogo.com.
Comments are closed.