top of page

Install Ansible on Ubuntu Server to Automate Linux Server Deployments

How many Linux machines are you managing on your network or your cloud-hosted platform? These days, that number is probably growing fairly quickly, especially given how much businesses not only depend on Linux for regular services, but containerized and cloud-native deployments.

So, yeah, those Linux machines are probably growing exponentially by the week or month. That means you have more and more machines to manage, which can be rather time-consuming. Given how busy your day already is, you don’t need to have to log into every single machine and run commands manually.

With that in mind, what do you do? One solution is to turn to a centralized configuration management tool, such as Red Hat‘s Ansible. One of the best things about Ansible, is that is makes use of SSH and YAML files to handle the heavy lifting of remote work. That means you don’t have to bother with installing agents on the servers you need to manage because it’s all handled via the controller.

I’m going to walk you through the installation and configuration of Ansible on Ubuntu Server and then show you how to use the platform to run your first Ansible playbook.

What You’ll Need

I’m going to demonstrate this set up entirely with Ubuntu Server installations, specifically Ubuntu Server 22.04. You can use Ansible with other operating systems, but since Ubuntu is my go-to, that’s what I choose to use. On top of this, Ansible is incredibly easy to install on Ubuntu.

With that said, are you ready to get down to business? I thought so.

Installing Ansible

Log into your Ubuntu Server 22.04 instance and install Ansible with the command:

sudo apt-get install ansible -y

The above command will also pick up all the necessary dependencies to get ansible up and running. There is, however, one more piece of software we’re going to install, which is sshpass. SSHpass is a noninteractive ssh password provider, so you can configure your remote server inventory with passwords for easier Ansible usage.

To install sshpass, issue the command:

sudo apt-get install sshpass -y

And that’s all the software you need to install.

Creating Your Inventory File

I’m going to demonstrate creating an inventory with a single server. You can add as many servers as you need to this file, just make sure to break them down into categories (such as web, dev, database, etc.), so you can have more control over the configuration.

First, create a new directory to house 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 that you configure your hosts in a very particular way. I’m going to create an entry called testServer for a machine at IP address, the user jack, and an ssh password of Th3N3w$t@ck. That inventory file would look like this:

ansible_ssh_pass= Th3N3w$t@ck
ansible_sudo_pass: SUDO_PASSWORD

The bottom section sets variables for all severs and instructs Ansible to use Python3 instead of the default Python. Make sure to insert your user’s sudo password there.


Let’s test our inventory. To do that, issue the command:

ansible all -m ping

The output of the above command should look something like this: | SUCCESS => {
    "changed": false,
    "ping": "pong"

The SUCCESS output is what you’re looking for. You see that…you’re golden.

Create and Run a Playbook

Let’s now create our first Ansible playbook. We’re 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, as it’s a YAML file and will fail if the indention isn’t correct.

Create the new playbook with the command:

nano lampstack.yaml

In that file, paste the following:

#Install LAMP Stack On Ubuntu Server
- hosts: testServer
  - name: install lamp stack
    become: yes
        - apache2
        - mysql-server
        - php
        - php-mysql
      state: present
      update_cache: yes
  - name: start apache service
    become: yes
    become_user: jack
      name: apache2
      state: started
      enabled: yes
  - name: start mysql service
    become: yes
           become_user: jack
      name: mysql
      state: started
      enabled: yes

Save and close the file. Do not, you’ll want to change jack in the above to a user with sudo privileges on your server.

With your playbook created, you can now run it with the command:

ansible-playbook lampstack.yaml  --user=jack --extra-vars ansible_sudo_pass="Th3N3w$t@ck"

Again, you’ll want to change jack and Th3N3w$t@ck with a user and password that actually works on your server.

As the playbook runs, you should see output like this:

PLAY [testServer] *****************************************************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************************************************************
ok: []
TASK [install lamp stack] *****************************************************************************************************************************************************
ok: []
TASK [start apache service] *****************************************************************************************************************************************************
ok: []
TASK [start mysql service] *****************************************************************************************************************************************************
ok: []
PLAY RECAP ***************************************************************************************************************************************************** : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=

The playbook should run (it’ll probably take some time to complete) and, once it completes, you can point a web browser to the IP address of the server in your inventory to see the Apache welcome page 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, make sure to check out the official documentation.

16 views0 comments


Obtuvo 0 de 5 estrellas.
Aún no hay calificaciones

Agrega una calificación
Stationary photo

Be the first to know

Subscribe to our newsletter to receive news and updates.

Thanks for submitting!

Follow us
bottom of page