How to Install Ansible and Run ansible-playbooks on Ubuntu 18.04 LTS

Installing software or running commands on one machine is ok, but what if you have dozens of servers to manage? To deploy software or configuration file changes on dozens of servers, consider using Ansible.

Ansible is a configuration and automation tool used to easily deploy multiple applications on multiple systems; it could be CLOUD, Services, orchestration, etc.

In this tutorial, you will learn step by step how to Install Ansible and Run Ansible playbooks on Ubuntu machines. Let’s get started.

Join 48 other followers

Table of Content

  1. What is Ansible ?
  2. Prerequisites
  3. How to Install Ansible on Ubuntu 18.04 LTS
  4. What is Ansible playbook and Ansible playbook examples!
  5. Conclusion

What is Ansible ?

Ansible is an automation tool used for deploying applications and systems easily; it could be CLOUD, Services, orchestration, etc. Ansible uses YAML Language to build playbooks to deploy or configure the required change. Ansible is an agentless automation tool that manages machines over the SSH protocol by default. Once installed, Ansible does not add a database, and there will be no daemons to start or keep running.

Related: The Ultimate Ansible tutorial with Ansible Playbook Examples

Prerequisites

  • Ubuntu machine preferably 18.04 plus version. If you don’t have any machine you can create an AWS EC2 instance on AWS account with recommended 4GB RAM and atleast 5GB of drive space

You may incur a small charge for creating an EC2 instance on Amazon Managed Web Service.

How to Install Ansible on Ubuntu 18.04 LTS

Let’s kick off this tutorial by installing Ansible on ubuntu. Installing Ansible on the ubuntu machine is a straightforward task. This tutorial will install Ansible from the Personal Package Archive repository (PPA).

  • Login to Ubuntu machine using your favorite SSH client.
  • First, update your existing list of packages by running the below command
sudo apt update
  • Install below prerequisites softwares so that apt uses packages over https protocol. The apt transport software allow your machine to connect with external respositories to connect over HTTPS or HTTP over TLS.
sudo apt install software-properties-common # This Package is used to work with PPA  
  • Add PPA ansible Repository in the system. This repository contains the Ansible software package.
sudo apt-add-repository --yes --update ppa:ansible/ansible 
#  PPA is Personal Package Archive 
Adding PPA ansible Repository in the system
Adding PPA ansible Repository in the system
  • Next install the Ansible Package using the below command.
sudo apt install ansible
  • Now verify if Ansible installation is successful.
ansible --version
Verifying the Ansible installation
Verifying the Ansible installation

What is Ansible playbook and Ansible playbook examples!

Ansible playbooks are used to deploy complex applications, offer reusable and simple configuration management, offer multi-machine deployments, and perform multiple tasks multiple times. Ansible playbooks are written in YAML format containing multiple tasks and executed in sequential order.

Now that you have successfully installed Ansible on the ubuntu machine let’s learn the basics of Ansible Playbook and how to build and run Ansible Playbooks using the Ansible tool. Let’s learn how to declare the Ansible playbook to install Apache on the remote node.

Basic inventory that is remote machines can be configured inside /etc/ansible/hosts. Make sure Ansible controller host node can ssh into worker node that is remote machine.

Related: working-with-ssh-connectivity

  • Create a folder under home directory named ansibledemo.
cd ~
mkdir ansibledemo
  • Create a apache.yml file inside the same directory. This Plaubook will install Apache service on the remote nodes.
- name: Installing Apache service on all my_app_servers  # Define the process
  hosts: webserver                                          # Define the host or group
  remote_user: ubuntu                                    # Remote_user is ubuntu

 
  become: true
  tasks:                                                 # Define the tasks
  - name: Install the Latest Apache
    apt:
      name: apache2
      state: latest

  • Before you run your first ansible-playbook you can verify the syntax , check tasks and details of playbook.
ansible-playbook apache.yml --syntax-check
Checking the syntax of Ansible Playbook
Checking the syntax of Ansible Playbook
ansible-lint apache.yml   # To check detailed 
Checking the syntax of Ansible Playbook in detail
Checking the syntax of Ansible Playbook in detail
  • Now run the ansible-playbook command
ansible-playbook apache.yml --check
Executing the Ansible Playbook
Executing the Ansible Playbook
  • Now, verify in remote host if apache2 is has been installed successfully and if service is running by running the service command.
service apache2 status
Verifying the Apache service on ubuntu machine
Verifying the Apache service on ubuntu machine
  • You can also verify if apache is running using Ip address of the sever followed by port 80.
Verifying the Apache service on Browser
Verifying the Apache service on Browser

Conclusion

In this article, you learned Ansible, how to install Ansible on an Ubuntu machine, and how to declare Ansible Playbooks and run Ansible playbooks.

Now that you have gained a handful of Knowledge on Ansible, what do you plan to deploy using it?

Advertisement

4 thoughts on “How to Install Ansible and Run ansible-playbooks on Ubuntu 18.04 LTS

  1. Pingback: The Ultimate Ansible tutorial with Ansible Playbook Examples | Automateinfra

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s