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.
Table of Content
- What is Ansible ?
- How to Install Ansible on Ubuntu 18.04 LTS
- What is Ansible playbook and Ansible playbook examples!
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.
- 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
- Next install the Ansible Package using the below command.
sudo apt install ansible
- Now verify if Ansible installation is successful.
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.
- 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
ansible-lint apache.yml # To check detailed
- Now run the ansible-playbook command
ansible-playbook apache.yml --check
- 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
- You can also verify if apache is running using Ip address of the sever followed by port 80.
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?