Open In App

How to Install rpm Package in Linux Using Ansible ?

Last Updated : 16 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Ansible, a strong computing automation tool, expands its capabilities beyond configuration management to package management, including the installation of RPM packages on Linux frameworks.RPM (Red Hat Package Manager) is a package management system executives framework utilized by different Linux distributions, including Red Hat Enterprise Linux (RHEL), CentOS, and Fedora. Ansible improves on the method involved with managing RPM packages across multiple hosts by providing a declarative and idempotent approach. Ansible abstracts away the complexities of RPM package management, allowing clients to focus on defining the desired state of their frameworks as opposed to the perplexing details of package installation.

While deploying applications, managing libraries, or guaranteeing framework consistency, Ansible smoothes out the establishment lifecycle making it effective repeatable, and adaptable.

In this guide, we will explore how to use Ansible to install RPM packages on Linux systems, empowering administrators to maintain framework trustworthiness and effectiveness easily.

What is Ansible?

Ansible is an Orchestration, and configuration management tool (installation and deployment). Ansible is a suite of software tools that enables configuring systems, deploying software, and orchestrating advanced workflows to support application deployment, system updates, and more. It is an automation tool that can be used to manage large groups of computer systems.

  • we can manage and configure other servers called slave servers from a single server called the Master server.
  • when we install Ansible in the master server a file is created called Hosts also known as the inventory path inside this inventory path we give details of slave servers such as IP address, key pair path, and hostnames. with this, we create a bridge between slave and master to manage configuration.

Architecture of Ansible

They are two working methods in ansible

  1. Ad-hoc
  2. Playbook

What Are Ansible Ad-hoc Commands ?

An ansible Ad-hoc command uses the /usr/bin/ansible command-line tool to automate a single task on one or more managed nodes/slave servers these commands are quick and easy but not reusable.

Ad-hoc command are perfect for tasks you repeat rarely.for example you want to drive off all the machines in your working environment for pongal vacation you could execute quick one-line command in ansible without composing a playbook. This ad-hoc command can be written as

ansible [pattern] -m(module) command -a(argument) "parameters"

What Is Ansible Playbook?

At its core, Ansible uses a simple YAML syntax called “playbooks” to describe automation tasks and configurations. completely different way to use ansible than in ad-hoc task execution mode and are particularly powerful. These playbooks define a series of steps or “plays” in that play we divide the plays with tasks. Ansible executes on target hosts, allowing users to define the desired state of their infrastructure in a declarative manner.

Example For Playbook

- name: Install and start Nginx
hosts: web_servers
become: yes
tasks:
- name: Install Nginx package
yum:
name: nginx
state: present
- name: Ensure Nginx service is running
service:
name: nginx
state: started
enabled: yes

Key Terminologies Of Ansible

  • Agentless: By the inventory path or hosts file we shall make the ansible agentless. Ansible operates over SSH, making it agentless and eliminating the need to install any additional software on target hosts. This simply deployment and reduces overhead.
  • Idempotent: Ansible ensures that the system’s state matches the desired state defined in the playbook, regardless of the system’s current state. This makes automation tasks predictable and repeatable.
  • Extensible: Ansibles’ modular architecture allows users to extend its functionality through custom modules and plugins, enabling integration with a wide range of systems, tools, and services.
  • Infrastructure as Code (IaC): With this Infrastructure as a code IaC feature we can create any cloud environment. Ansible enables Infrastructure as Code practices by allowing users to define infrastructure configurations in human-readable YAML files. This approach promotes version control, collaboration, and repeatability.

How to Install rpm package In Linux using Ansible: A step by step guide

Here, we are going to install rpm package in Linux using ansible in aws EC2 instance.

Step 1 : Create an AWS account and navigate to EC2 and select launch instance.

Launch An Instance With Configuration:

  • AMI– amazon Linux 2
  • instance type- t2.micro
  • Security group- allow SSH(22),HTTP(80),HTTPS(443) traffic from anywhere
  • Configure storage – 8gb with root volume type gp2
  • Connect this instance with any CLI terminal by using SSH
ssh -i  "keypair-pem file" ec2-user@<instance-public-ip address>compute-1.amazonaws.com

Launch Instance And Connect To It

Step 2: Installing Ansible

  • install Ansible with amazon-linux-extras and the version of ansible is 2.9.23.
sudo amazon-linux-extras install ansible2

Installing Ansible

Step 3: Hosts File Configuration

  • After installing and setup the ansible a hosts file is created called inventory path in /etc/ansible path
  • This file holds information for the hosts/and host groups you will configure
  • we need to configure this file called hosts
sudo vi /etc/ansible/hosts

hosts file configuration

  • Now,configure this inventory hosts file with slave server’s details such as server name,ansible_host,ansible_user, ansible_ssh_private_key_file path
  • for this create one group name as [localhost] you can configure this group name as you wish.
  • below this group localhost we add the slave server details.
  • the keypair pem file should be only with read permissions to root.
[localhost]
slave ansible_host=172.31.45.95 ansible_user=ec2-user ansible_ssh_private_key_file path=/home/ec2-user/11.pem

Configuring Inventory File

Step 4: Check the connection to the slave server

  • now we need to check the connection to this slave server wit the ping module
ansible all -m ping

Providing Permission To Slave Server

Step 5: Create a file for playbook

  • to write a playbook we need to create file for this playbook
sudo vi playbook.yml

Creating A Playbook

  • in this file write the playbook
  • You need to make sure that you follow standard YAML syntax guidelines when manipulating Ansible configuration files, otherwise you are likely to experience syntax errors. A link to YAML Syntax on Ansible’s website is included in the resources section of this article.
---
- name: Install RPM package using Ansible
hosts: localhost
become: yes
tasks:
- name: Install RPM using Yum
yum:
name: "rpm"
state: present

playbook for rpm package installing

  • you can check the playbook syntax error
ansible-playbook <playbook_filename> --syntax-check

Step 6: Run or execute the playbook

  • execute the playbook file playbook.yml which has yaml script.
ansible-playbook playbook.yml

Execution Of playbook

  • playbook is successfully executed.

Step 7: Ensure the rpm package installation

  • we need to make sure the rpm package is successfully installed or not
  • we can check the rpm installation and its version from this ansible by using ad-hoc command.
ansible localhost -m -a "rpm --version"

ensuring rpm package installation

Installation Of rpm Packages In Linux Using Ansible – FAQs

What is Ansible?

Ansible is a Orchestration, configuration management tool (installation and deployment). Ansible is a suite of software tools that enables to configure systems,deploy software and orchestrate advanced workflows to support application deployment,system updates and more……it is automation tool that can be used to mange large group of computer systems.

we can manage and configure of other servers called slave servers from a single server called Master server.

Where can we configure the hosts file or Inventory Path?

we can configure the hosts file.Before we create a basic configuration, I want to take a moment to explain the Ansible file/folder structure. You’ll note that if you list the files/folders in /etc/ansible that you’re presented with the following.

/etc/ansible/hosts
sudo vi /etc/ansible/hosts

Is Ansible Agent-based or Agentless?

Ansible is primarily agentless, meaning that it does not require any software to be installed on target hosts. Instead, Ansible uses SSH (or other connection methods) to communicate with remote hosts and execute tasks. This agentless architecture simplifies deployment and reduces overhead.

How many working methods in Ansible?

There are two working methods in ansible

1.Ad-hoc: An ansible Ad-hoc command uses the /usr/bin/ansible command-line tool to automate a single task on one or more managed nodes/slave servers these commands are quick and easy but not reusable.

2.playbook: Ansible uses a simple YAML syntax called “playbooks” to describe automation tasks and configurations.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads