Linux

How to Install and Configure Ansible on CentOS

How to Install and Configure Ansible on CentOS

Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. Its agentless architecture and easy-to-understand playbooks make it a favorite among system administrators. In this guide, we will go through the step-by-step process of installing and configuring Ansible on CentOS.

Prerequisites
Before we begin, ensure that you have the following:

A CentOS system (CentOS 7 or 8).
Sudo or root access.
An active internet connection for installing packages.
It’s also a good idea to update your system before beginning the installation:

sudo yum update -y

This command ensures all packages and repositories are up to date.

Step 1: Install EPEL Repository
Ansible is not available in the default CentOS repositories, but it is available in the EPEL (Extra Packages for Enterprise Linux) repository. To install EPEL, run the following command:

sudo yum install epel-release -y

Once EPEL is installed, you can proceed to install Ansible.

Step 2: Install Ansible
With the EPEL repository enabled, installing Ansible is straightforward. Use the following command to install Ansible:

sudo yum install ansible -y

This will download and install all the necessary dependencies for Ansible to run on your CentOS system.

Step 3: Verify the Installation
Once the installation is complete, you can verify that Ansible was installed correctly by checking its version:

ansible –version

The output should display the installed Ansible version and some additional information about your system. This confirms that Ansible is ready to use.

Step 4: Configure Ansible Inventory
Ansible uses an inventory file to know which systems to manage. By default, the inventory file is located at /etc/ansible/hosts. You can use this file to list the IP addresses or hostnames of the machines you want Ansible to manage.

To edit the inventory file, use a text editor like nano:

sudo nano /etc/ansible/hosts

Add your hosts under different groups. For example:

[webservers]
192.168.1.10
192.168.1.11

[dbservers]
192.168.1.12

In this example, the web servers and database servers are listed under separate groups. You can group your servers based on their roles, which will make it easier to manage them with Ansible playbooks.

Step 5: Test Connectivity with Managed Hosts
Before proceeding to more complex configurations, it’s important to ensure that Ansible can communicate with your managed hosts. Ansible uses SSH to connect to remote servers, so you’ll need to have SSH access set up.

To test connectivity, use the following Ansible ping module:

ansible all -m ping

If everything is configured correctly, you should see a response from each server in your inventory:

192.168.1.10 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
192.168.1.11 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}
192.168.1.12 | SUCCESS => {
“changed”: false,
“ping”: “pong”
}

This confirms that Ansible can communicate with your servers.

Step 6: Configuring Ansible Playbooks
Ansible uses playbooks written in YAML to automate tasks. Playbooks are a set of instructions that define how Ansible should manage or configure your servers.

To create a simple playbook, first create a new file:

nano my-playbook.yml

Inside this file, you can define a basic playbook that installs Apache on your web servers:


– hosts: webservers
become: yes
tasks:
– name: Install Apache
yum:
name: httpd
state: present
– name: Start Apache service
service:
name: httpd
state: started

In this playbook, Ansible will install Apache (httpd) on all servers listed under the webservers group in your inventory file. It will then ensure that the Apache service is started.

Once your playbook is ready, run it using the following command:

ansible-playbook my-playbook.yml

This command will execute the tasks in the playbook on the specified hosts. You should see Ansible performing each step, and once it finishes, Apache should be installed and running on your web servers.

Step 7: Configuring SSH Keys for Passwordless Access
To streamline your Ansible operations, you can configure passwordless SSH access. This will prevent Ansible from prompting you for a password every time it connects to a remote host.

First, generate an SSH key pair on your control machine (the machine where Ansible is installed):

ssh-keygen

You’ll be prompted to choose a file location for the key and an optional passphrase. Once the key pair is generated, copy the public key to the remote hosts:

ssh-copy-id [email protected]

Repeat this for each server in your inventory. Afterward, Ansible will be able to connect to these servers without prompting for a password.

Step 8: Automating Tasks with Ansible
With Ansible now fully installed and configured, you can begin automating various system administration tasks. Whether you need to update packages, deploy applications, or configure services, Ansible can simplify the process. Here are a few examples of what you can do with Ansible:

Software installation: Use Ansible to install software packages across multiple servers simultaneously.
Service management: Start, stop, or restart services on remote servers.
File management: Push configuration files or templates to servers and ensure they are up-to-date.
To run these tasks, create additional playbooks with the appropriate instructions, and run them with the ansible-playbook command.

By following these steps, you now have a fully operational Ansible setup on CentOS. From here, you can explore more advanced features of Ansible, such as role-based playbooks, vault for encrypted data, and dynamic inventories.

Ansible Github 

Ansible Documentation 

If you want, you can also read our article about Multi-PXE.

How to Set Up a Multi-PXE Installation Server

How to Install Cobbler on Alpine Linux Latest

You can also rent a server from our site and try packages such as cobbler, multi-PXE and Ansible in a reliable environment 🙂

Bulgaria Location Virtual Dedicated Server

 

 

 

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button