How to Install Ansible on Rocky Linux 9 Step by Step

Hello techies, in this guide, we will show you how to install ansible on rocky linux 9 step by step.

Ansible is a free and opensource automation tool, it is used to configure and manage remote Linux and windows systems over ssh protocol and WinRM protocol respectively. We don’t need to install any agent on remote linux systems as it connects ssh connection.

The system on which we install Ansible is known as control node and systems which are managed by ansible are known as managed hosts.

Prerequisites

  • Minimal Installed Rocky Linux 9 along with internet connectivity
  • Sudo User with admin rights on control and managed hosts
  • Network connectivity between Ansible Control node and managed hosts

Without any further delay, let’s deep dive into Ansible installation steps,

1 ) Install Ansible-Core from Appstream Repository

Ansible core is available in the default appstream package repository of Rocky Linux 9. So, we don’t need to enable EPEL repository anymore. Appstream repository maintains the latest and stable version of ansible.

Note: package with name ansible is not available anymore on rocky linux 9.

Run following command to view the available ansible-core package,

$ sudo dnf list ansible-core
Available Packages
ansible-core.x86_64                   2.13.3-1.el9             appstream
$

To install ansible-core, run following dnf command,

$ sudo dnf install ansible-core -y

dnf-install-ansible-core-rocky-linux9

Execute the beneath command to verify ansible version after its installation,

$ ansible --version

Output

Ansible-version-check-after-installation

2) Create Ansible.cfg and Inventory File

It is always recommended to have a separate ansible.cfg and inventory file for each project. Let’s first create project directory with name “demo-automation” using mkdir command.

$ mkdir demo-automation
$ cd demo-automation/

Create a ansible.cfg file with following content

$ vi ansible.cfg
[defaults]
inventory = ./inventory
host_key_checking = false
remote_user = sysops
ask_pass = False

[privilege_escalation]
become=true
become_method=sudo
become_user=root
become_ask_pass=False

save & close the file.

Now, create inventory file with beneath content,

$ vi inventory
[dev_web]
192.168.1.204

Save & exit the file.

Note: 192.168.1.204 is a managed host. It is an Ubuntu 22.04 system.

3) Prepare Managed host

First generate the ssh keys of your sysops user from ansible control node and share those keys to managed host so that password less authentication can be achieved. Run ssh-keygen command from ansible control node,

$ ssh-keygen

Hit enter couple time to accept the default values.

Copy the public key from ansible control node to manage host using ssh-copy-id command.

$ ssh-copy-id sysops@192.168.1.204

Output,

ssh-copy-id-from-control-node-to-managed-host

Now login to your managed host and create following file on it, so that it does not prompt for the password while executing sudo commands.

$ echo "sysops ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/sysops

4) Test Ansible Installation

To test ansible installation, let’s first verify the connectivity using ping module, run following command from “/home/sysops/demo-automation” folder,

$ cd /home/sysops/demo-automation
$ ansible dev_web -m ping

Ping-Pong-Test-Ansible-Rocky-Linux

Above output confirms that we able to connect remote managed host. Let’s create a sample playbook to install and start nginx service,

$ vi nginx.yaml
---
- name: Playbook to Install Nginx Web Server
  hosts: dev_web
  tasks:
  - name: Install nginx
    package:
      name: nginx
      state: present

  - name: Start nginx Service
    service:
      name: nginx
      state: started

save and close the file.

Run the playbook using following command,

$ ansible-playbook nginx.yaml

Output,

Run-Ansible-Playbook-RockyLinux9

Above Output shows that playbook has been executed successfully. Let’s verify using following ad-hoc commands,

$ ansible dev_web -m shell -a 'apt list --installed|grep nginx'
$ ansible dev_web -m shell -a 'systemctl status nginx'

Ansible-ad-hoc-command-to-verify-nginx-installation

That’s all from this guide, I hope you have found it useful. Kindly do post your queries and feedback in below comments section.

Leave a Comment

five × 4 =