How to Install and Configure Ansible on CentOS

There are many configuration management tools available to developers that can facilitate the management of remote servers, but Ansible stands apart for many reasons. 

Want to know what makes Ansible different from other configuration management tools? Well, it is an agentless tool, which means there is no requirement to set up agents on every node you operate. It is a tool that automates cloud provisioning, configuration management, deployment of application, orchestration, and many other vital IT requirements.

In this article on Ansible installation, we'll be discussing the following topics:

  • What Is Ansible?
  • How to Install Ansible?
  • Conclusion

Now, before we jump into learning Ansible installation, let’s start by looking at exactly what Ansible is. 

What is Ansible?

Ansible is a configuration and orchestration management tool where applications are deployed automatically in a variety of environments.

Post Graduate Program in DevOps

Designed in collaboration with Caltech CTMEExplore Course
Post Graduate Program in DevOps

How to Install Ansible?

While installing Ansible, it's essential to have two machines. Let's name our first machine, as ‘Server’ that will act as our managed node, and the second machine named ‘Node’ that will act as the controller node. 

Now, let's get started on this and understand the Ansible installation process in detail.

Step 1: Update Your Control Node

Before installing any new software, it is important to ensure that your existing operating version is up-to-date. Enter the command mentioned below to start your task.

yum update

Step 2: Install EPEL Repository

Moving on, install the EPEL repository on the system.

yum install epel-release

EPEL provides easy access to install commonly used packages on CentOS.

Step 3: Install Ansible

The next step is to install the Ansible package from the EPEL repository.

yum install ansible

Step 4: Create a User for Ansible

Let's create a non-root user on both the nodes that will run our Ansible playbooks. In this demo, we will use "Simplilearn"' as the username (but any username can be added). Also, you should ensure that you use the same username on both the nodes (i.e., controller node and your managed node).

Add a user and set a password onto your Controller node.

useradd  simplilearn

passwd Simpli@123

Add the admin user, and set a password onto your Managed node.

useradd simplilearn

passwd Simpli@123

Step 5: Configure Our Admin User for SSH Access

Configure the admin user (i.e. Simplilearn user) so that it can access the Managed node without a password. Then, ensure that you set up an SSH key pair to the Simplilearn user.

Now, run the following command (in the control node) to generate an SSH key pair. 

ssh-keygen

Then, copy the public key and paste it to our Managed node with the command below.

ssh-copy-id node.kb.liquidweb.com

Step 6: Create an Inventory

An inventory list is created to identify your managed nodes. 

Log in to your control node as the admin user to connect the Managed node to the inventory.

vim /home/admin/inventory

Now, enter ‘i’ to get to the insert mode, and add the hostname of the Managed Node. node.kb.liquidweb.com

Now, save the file by typing ‘[ESC]:wq’.

Increase team productivity and improve business outcomes with the Ansible Foundation Training Course. Enroll now!

Step 7: Create an Ansible Playbook

Here, we will create a simple Ansible playbook by installing Nginx on the Managed Node.

First, log onto your Controller Node as the “Simplilearn” user and create a file with a descriptive name.

vim /home/simplilearn/install-nginx.yml

These playbooks are written in YAML language which is human-readable (as shown below).

Now, go to the insert mode and add the following text to your playbook. 

--

- hosts: AppServer

  become: yes

 

  tasks:

 

  - name: Installs nginx web server

 

    Apt: name: nginx state: started update_cache: true

 

    notify:

 

    - start nginx

 

  handlers:

 

  - name: start nginx

 

    service:

 

      name: nginx

 

      state: started

Let’s break the code into segments for a better understanding.

---

YAML provides multiple files to exist in one document file where each is separated by---

The YAML file defines a hierarchical structure with the containing elements such as hosts, tasks, and handlers.

In this playbook, we have a set of tasks, such as:

- hosts: AppServer

 

  become: yes

 

  tasks:

 

  - name: Installs nginx web server

 

    Apt: name: nginx state: started update_cache: true

 

    notify:

 

    - start nginx

Task performs all the major operations in the file.

Here, ‘notify’ consists of a list with one item, which is called "start nginx." Notify is not an internal command of Ansible but a reference to a handler that is responsible for performing a function when it is called by a task.

Handlers are the same as hosts and tasks, but they operate only when instructed by a task on the client system.

 handlers:

 

  - name: start nginx

 

    service:

 

      name: nginx

 

      state: started

The code above defines the "start nginx" handler. The handler will execute after the nginx web server is installed. We can save this playbook into a file called something like "nginx.yml".

Then type '[ESC]:wq' to save this and exit.

Note: Prioritize using only spaces and not tabs. Make sure to use consistent spacing for your YAML file to avoid errors.

Step 8: Run the Playbook

Our Ansible playbook is built. Now, to run the playbook, type the following command on the controller node:

ansible-playbook -i /home/admin/inventory /home/admin/install-nginx.yml

In the command above, we have added the inventory file with the "-i" option, followed by the playbook path.

Conclusion

This brings us to the conclusion of the ‘Ansible Installation’ article. Here, we learned what Ansible is and how to install the Ansible management tool. If you want to get an in-depth understanding of this popular automation tool, enroll for the Ansible Training Course. You can even check out the Post Graduate Program in DevOps designed in collaboration with Caltech CTME. The program prepares you for a career in DevOps, bridging the gap between software developers and operations teams. Our unique Blended Learning combines live online classes, interactive labs and more, giving you hands-on experience and job-ready skills.

Do you have any questions regarding this topic? Please feel free to put it in the comments section of this article, our experts will get back to you at the earliest.

About the Author

SimplilearnSimplilearn

Simplilearn is one of the world’s leading providers of online training for Digital Marketing, Cloud Computing, Project Management, Data Science, IT, Software Development, and many other emerging technologies.

View More
  • Disclaimer
  • PMP, PMI, PMBOK, CAPM, PgMP, PfMP, ACP, PBA, RMP, SP, and OPM3 are registered marks of the Project Management Institute, Inc.