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

Installing software or running commands on one machine takes somewhere around four to five mins, and if we are asked to perform same activity on multiple servers or machines , Do you how much time would it take? It would take tons of hours to do that . Does it sound OK to you to spend so much of time ?

To Solve the above problem , we have ansible which is configuration and automation tool used for deploying multiple applications on multiple systems easily it could be CLOUD, Services, orchestration etc.

Table of Content

  • What is Ansible ?
  • Ansible main concepts
  • Prerequisites
  • How to Install Ansible on Ubuntu 18.04 LTS
  • How to setup ansible configuration files and run First ansible playbook?
  • Conclusion

What is Ansible ?

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

Ansible Main Concepts

  • Control Node: Node from where you run ansible playbooks/commands. You can have multiple Control nodes but not windows. Must have Python Installed.
  • Managed Nodes: Servers/Network devices where you manage ansible also know as hosts.
  • Inventory: List of managed Nodes.
  • Collections: It includes all of above playbooks , modules, roles and plugins. Install and manage collections through Ansible Galaxy.
  • Modules: These are code or say libraries that Ansible needs to execute. You can call or invoke modules in your playbooks.
  • Tasks: Unit of action is known as task . You can execute one task , more tasks and later you can use many tasks in playbook as well.
  • Playbook: Ordered list of tasks written in YAML Human readable language.

Prerequisites

  • Ubuntu machine preferably 18.04 version + , if you don’t have any machine you can create a ec2 instance on AWS account
  • Recommended to have 4GB RAM
  • At least 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

  • Update your already existing system packages.
sudo apt update
  • Install the packages which will be required for ansible to work
sudo apt install software-properties-common # This Package is used to work with PPA  
  • Add PPA ansible Repository in the system.
sudo apt-add-repository --yes --update ppa:ansible/ansible 
#  PPA is Personal Package Archive 
sudo apt install ansible
  • Now verify if installation was successful.

How to setup Ansible configuration files and run First ansible playbook?

  1. Playbooks are used to deploy complex applications
  2. Playbooks offers reusable and simple configuration management
  3. Playbooks offers multi machine deployment
  4. Playbook offers help when we need to perform certain tasks multiple times.
  5. Playbooks are written in YAML format
  6. Playbooks executes in sequential order.
  7. Playbooks has different tasks to execute.

Lets run our first Ansible Playbook to install Apache latest service

  • 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, else please look at  working-with-ssh-connectivity
10.111.4.18          # Either we can use IP address or 

aserver.domain.com   # Either we can use Hostname or 

[webserver]           # Group a single server or multiple servers
10.111.4.18 
  • Create a folder under home directory
cd ~
mkdir ansibledemo
  • Create a apache.yml file inside the same directory
- 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

Conclusion

This tutorial is prepared with lots of efforts and with complete practical demonstration. In this tutorial we demonstrated what is ansible and ansible playbooks with practical’s. This would give you a good start if you are new to ansible. I would request you to please share the word with others and go through tutorial f you find it useful.

Please share with your Friends and Groups

2 comments

  1. I like the helpful info you provide in your articles.

    I will bookmark your weblog and check again
    here regularly. I am quite sure I’ll learn many new
    stuff right here! Best of luck for the next!

    Liked by 1 person

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 )

Google photo

You are commenting using your Google 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