Some Concepts of Ansible

Posted By : Ankit Gupta | 09-Dec-2020

What Is Ansible?

Ansible is an open source IT configuration management, deployment, and orchestration tool. It is a real boon for DevOps teams as they can define their infrastructure in a simple and declarative form of code.

Most people compare Ansible to Chef or Puppet, as they are similar. Although these tools help in automaton and provision infrastructure, Ansible has a slight edge over others due to some additional features.

What Are The Benefits Of Using Ansible?

Ansible Is Agentless

In Ansible, managing remote systems does not require any agent installation. As a result, there are fewer maintenance overhead and performance issues. Instead, Ansible uses the push-based approach to leveraging existing SSH connections to run tasks on the remote managed host. Unlike Ansible, Chef or Puppet requires Agent installation on the hosts to be managed. That agent is responsible for pulling changes from the control host using its own channel.

Ansible Is Written In Python

Ansible is created in Python, which eases the installation and running of Ansible in any Linux distribution and only a bit more difficult on OS X. Since Python is a popular programming language, you can easily learn it online if you are not already familiar with it. Or, you’ll easily be able to find a developer with Python experience to help you out.

Ansible Is Easy To Learn

The fact that a new user can get up to speed and run Ansible tasks in a matter of minutes, thanks to clear and easy-to-follow documentation, is one of the most appealing features of Ansible. Troubleshooting in Ansible is additionally very easy for beginners and therefore the incontrovertible fact that all tasks are idempotent reduces the danger of creating an error.

Deploy Infrastructure In Record Time

 

Ansible could dispatch tasks to multiple remote managed hosts in parallel. This means you can execute Ansible tasks on a second managed host without waiting for them to complete on the first to reduce provision time and deploy your infrastructure faster than ever.

Step 1: Install Ansible On Your Control Machine

To take your first steps with Ansible you initially got to install it on your control machine. This is the machine you’ll use to dispatch tasks. For most people, this will be your desktop machine at home or your laptop, but you can also use one VPS as a control host to connect to other VPSs.

Installing Ansible on Ubuntu 16.04

Use standard package managers like apt/yum or Python’s pip command to install Ansible. To install it using standard package manager in Ubuntu, add its repository information apt-add-repository. In the next step, you need to update the system and install Ansible using apt-get.

$ sudo apt-get install software-properties-common

$ sudo apt-add-repository ppa:ansible/ansible

$ sudo apt-get update

$ sudo apt-get install ansible

 

Installing Ansible on CentOS 7

While installing Ansible in CentOS and RHEL you would like to enable the EPEL repository first before proceeding with installation of Ansible. Once you enabled the EPEL repository, install Ansible using yum.

$ cd /tmp

$ wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm

$ sudo rpm -ivh epel-release-7-9.noarch.rpm

$ sudo yum update

$ sudo yum install ansible

 

Also Read: Things about Ansible everybody should know

 

Ansible inventory files

The Ansible inventory file lists which which hosts will receive commands from the control host. The inventory can list individual hosts, or group them under categories you create.

The default location for the inventory file is /etc/ansible/hosts, but it’s also possible to change the location of the inventory file by uncommenting the inventory parameter in /etc/ansible/ansible.cfg

A typical inventory file can list the managed host either by IP address or by domain names. It is also possible to list one managed host in more than one group. Here’s an example of listing two hosts under the webservers and dbservers categories.

[webservers]

123.45.67.89

SUBDOMAIN.DOMAIN.TLD


 

[dbservers]

123.45.67.89 SUBDOMAIN.DOMAIN.TLD

To test if all the hosts are discoverable by the inventory file, use the following ad-hoc command.

$ ansible all --list-hosts

  hosts (2):

    123.45.67.89

    SUBDOMAIN.DOMAIN.TLD

 

You can also list the hosts by group name:

$ ansible dbservers --list-hosts

hosts (2):

    123.45.67.89

    SUBDOMAIN.DOMAIN.TLD

 

Ad-hoc commands in Ansible are those that perform a single command across one or many hosts. They don’t use tasks but allow you to try tons of things quite easily without building out playbooks (more on those within the second part of this guide)

To find out if all the hosts are up and running use the subsequent ad-hoc command that uses the ping module of Ansible. The -u switch specifies which user Ansible will connect to via SSH—change it according to the non-root user you created earlier.

$ ansible all -m ping -u USER

 

123.45.67.89 | SUCCESS => {

    "changed": false,

    "ping": "pong"

}

...

...

 

The "changed": false within the above JSON result tells us that the ping Ansible task didn’t change anything on the remote server.

Rather than specifying all the hosts as within the above command, you can also ping a group of hosts. Specify the group name in place of ‘all’ with the following command:

$ ansible webservers -m ping -u USER

 

Ansible modules

Modules are the discrete units of code that can be used from the terminal or in a playbook task. It simplifies Ansible tasks by installing software, copying files, using templates, and so on.

Modules use the available context to determine what actions if any needed to bring the managed host to the desired state and are idempotent, that means if you run the same task, again and again, the state of the machine will not change.

To find the list of available modules, use the following command:

$ ansible-doc -l

 

To install Nginx on an Ubuntu/Debian host using an ad-hoc command in Ansible:

$ ansible webservers -b --become-user=root -m shell -a 'apt -y install nginx' -u USER

 

172.104.160.8 | SUCCESS | rc=0 >>

Reading package lists...

Building dependency tree...

 

The following flags were used with the above command:

  • -b: Tell ansible to become another user to run the command
  • --become-user=root: Run the command as a root user
  • -m: Declares which module is used in the command
  • -a: Declares which arguments are passed to the module

 

We are seasoned DevOps solutions and service providers with vast experience in providing full-scale DevOps solutions for varied business requirements. Our team of DevOps professionals formulates effective strategies to strengthen your enterprise IT infrastructure and enhance operational efficiency. Our 360-degree DevOps solutions and services accelerate the software development lifecycle and ensure faster delivery with continuous deployment. For project related queries, reach us out at [email protected]

About Author

Author Image
Ankit Gupta

Ankit is a Redhat Certified System Administrator and Redhat Certified Engineer. He is interested in learning new DevOps tools . He likes Linux, DevOps , Automation & Cloud Computing. He always try to complete the assigned tasks within in the given time.

Request for Proposal

Name is required

Comment is required

Sending message..