VOOZH about

URL: https://thenewstack.io/install-ansible-on-ubuntu-server-to-automate-linux-server-deployments/

⇱ Install Ansible on Ubuntu Server to Automate Linux Server Deployments - The New Stack


TNS
SUBSCRIBE
Join our community of software engineering leaders and aspirational developers. Always stay in-the-know by getting the most important news and exclusive content delivered fresh to your inbox to learn more about at-scale software development.
REQUIRED
It seems that you've previously unsubscribed from our newsletter in the past. Click the button below to open the re-subscribe form in a new tab. When you're done, simply close that tab and continue with this form to complete your subscription.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.
Welcome and thank you for joining The New Stack community!
Please answer a few simple questions to help us deliver the news and resources you are interested in.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Great to meet you!
Tell us a bit about your job so we can cover the topics you find most relevant.
REQUIRED
REQUIRED
REQUIRED
REQUIRED
REQUIRED
Welcome!

We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.

What’s next?

Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.

Follow TNS on your favorite social media networks.

Become a TNS follower on LinkedIn.

Check out the latest featured and trending stories while you wait for your first TNS newsletter.

PREV
1 of 2
NEXT
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Thanks for your opinion! Subscribe below to get the final results, published exclusively in our TNS Update newsletter:
NEW! Try Stackie AI
From clobbered drafts to real-time sync
Apr 14th 2026 10:00am, by David Moore
TypeScript 6.0 RC arrives as a bridge to a faster future
Mar 14th 2026 9:00am, by Darryl K. Taft
Mastra empowers web devs to build AI agents in TypeScript
Jan 28th 2026 11:00am, by Loraine Lawson
2022-07-09 06:00:40
Install Ansible on Ubuntu Server to Automate Linux Server Deployments
DevOps / Linux

Install Ansible on Ubuntu Server to Automate Linux Server Deployments

Do you have a growing number of Linux servers to take care of? One solution is to turn to a centralized configuration management tool, such as Red Hat's Ansible. Here's how to get started.
Jul 9th, 2022 6:00am by Jack Wallen
👁 Featued image for: Install Ansible on Ubuntu Server to Automate Linux Server Deployments
Feature image: GetVectorLogo.com.

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 192.168.1.13, the user jack, and an ssh password of Th3N3w$t@ck. That inventory file would look like this:

[testServer]
192.168.1.13

[testServer:vars]
ansible_user=jack
ansible_ssh_pass= Th3N3w$t@ck

[all:vars]
ansible_python_interpreter=/usr/bin/python3
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.

Awesome.

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:

192.168.1.13 | 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
  tasks:
  - name: install lamp stack
    become: yes
    apt:
      pkg:
        - apache2
        - mysql-server
        - php
        - php-mysql
      state: present
      update_cache: yes

  - name: start apache service
    become: yes
    become_user: jack
    service:
      name: apache2
      state: started
      enabled: yes

  - name: start mysql service
    become: yes
           become_user: jack
    service:
      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: [192.168.1.13]

TASK [install lamp stack] *****************************************************************************************************************************************************
ok: [192.168.1.13]

TASK [start apache service] *****************************************************************************************************************************************************
ok: [192.168.1.13]

TASK [start mysql service] *****************************************************************************************************************************************************
ok: [192.168.1.13]

PLAY RECAP *****************************************************************************************************************************************************
192.168.1.13 : ok=4 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

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.

TRENDING STORIES
Jack Wallen is what happens when a Gen Xer mind-melds with present-day snark. Jack is a seeker of truth and a writer of words with a quantum mechanical pencil and a disjointed beat of sound and soul. Although he resides...
Read more from Jack Wallen
SHARE THIS STORY
TRENDING STORIES
Red Hat is a sponsor of The New Stack.
SHARE THIS STORY
TRENDING STORIES
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.
The New Stack does not sell your information or share it with unaffiliated third parties. By continuing, you agree to our Terms of Use and Privacy Policy.