Share on Social Media

Learn how to install Ansible on Rocky Linux 8 with this comprehensive guide. Follow our step-by-step instructions to set up Ansible, configure your environment, and automate your IT infrastructure efficiently. #centlinux #linux #ansible

What is Ansible Automation Platform?

Ansible is an open-source automation tool that simplifies the process of IT automation, configuration management, and application deployment. It enables you to automate repetitive tasks, manage configurations, and orchestrate complex workflows across multiple servers or network devices.

Here are some key features and aspects of Ansible:

  1. Agentless: Ansible operates in an agentless manner, meaning it doesn’t require any agents or daemons to be installed on the managed hosts. It communicates with remote machines through SSH, making it lightweight and easy to set up.
  2. Infrastructure as Code (IaC): Ansible allows you to define infrastructure configurations, application deployments, and workflows as code using YAML syntax. This enables version control, collaboration, and repeatability of infrastructure changes.
  3. Playbooks and Roles: Ansible uses playbooks, which are YAML files containing a series of tasks to be executed on remote hosts. Playbooks can be organized into roles, allowing for modular and reusable configurations.
  4. Idempotent Execution: Ansible ensures idempotent execution, meaning running the same playbook multiple times will result in the same desired state, regardless of the initial state of the system. This helps maintain consistency and predictability in infrastructure management.
  5. Extensibility: Ansible is highly extensible, with support for a wide range of modules and plugins. You can extend its functionality by writing custom modules or using community-contributed modules to integrate with various systems and services.
  6. Orchestration: Ansible can orchestrate complex workflows across multiple hosts, including tasks such as rolling updates, blue-green deployments, and multi-tier application deployments.
  7. Integration: Ansible integrates seamlessly with other tools and technologies commonly used in the DevOps ecosystem, such as version control systems (e.g., Git), continuous integration/continuous deployment (CI/CD) pipelines, cloud platforms, and monitoring systems.

Overall, Ansible provides a simple, yet powerful way to automate IT operations, streamline configuration management, and accelerate application deployments, making it a popular choice for DevOps teams and system administrators.

Read Also: How to install Ansible on Rocky Linux 9

Ansible vs Terraform

Ansible and Terraform are both powerful tools used in DevOps and infrastructure automation, but they serve different purposes and have distinct features:

Ansible

  • Description: Ansible is a configuration management and automation tool that focuses on task execution and configuration management across multiple servers.
  • Strengths:
    • Agentless: Ansible doesn’t require any agents to be installed on remote systems, making it easy to set up and use.
    • Simplicity: Ansible uses YAML syntax and is easy to learn, making it accessible to a wide range of users.
    • Configuration Management: Ansible excels at managing configurations, deploying software, and orchestrating complex tasks across servers.
  • Use Cases: Ansible is well-suited for tasks such as configuration management, application deployment, and ad-hoc task automation.

Terraform

  • Description: Terraform is an infrastructure as code (IaC) tool that focuses on provisioning and managing infrastructure resources using declarative configuration files.
  • Strengths:
    • Infrastructure as Code: Terraform allows you to define infrastructure resources as code, enabling versioning, collaboration, and repeatability.
    • Multi-Cloud Support: Terraform supports multiple cloud providers and infrastructure platforms, allowing you to manage resources across different environments.
    • State Management: Terraform maintains a state file that tracks the current state of your infrastructure, making it easier to manage and update resources.
  • Use Cases: Terraform is ideal for provisioning and managing cloud infrastructure, creating immutable infrastructure, and managing complex environments.

Comparison

  • Focus: Ansible focuses on configuration management and automation of tasks, while Terraform focuses on infrastructure provisioning and management.
  • Approach: Ansible follows a procedural approach, where tasks are executed sequentially, while Terraform follows a declarative approach, where you define the desired state of your infrastructure.
  • Complexity: Ansible is simpler to learn and use, making it suitable for a wide range of tasks and users. Terraform, while more complex, offers powerful features for managing infrastructure at scale and across multiple providers.
  • Use Cases: Ansible is often used for managing configurations, deploying applications, and automating routine tasks. Terraform is typically used for provisioning cloud resources, managing infrastructure, and creating repeatable infrastructure deployments.

In summary, while both Ansible and Terraform are valuable tools in the DevOps toolkit, they serve different purposes and are often used together to achieve comprehensive automation and infrastructure management.

Recommended Online Training: Ultimate Ansible Bootcamp by School of Devops

1271900 62ef 10show?id=oLRJ54lcVEg&offerid=1074652.1271900&bids=1074652

Environment Specification

We are using a minimal Rocky Linux 8 virtual machine with following specifications.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 2 GB
  • Storage – 20 GB
  • Operating System – Rocky Linux 8.5 (Green Obsidian)
  • Hostname – ansible-01.centlinux.com
  • IP Address – 192.168.116.129 /24

Set Hostname of Linux Server

By using a ssh client, connect with your Rocky Linux 8 server as root user.

Execute the following command on Linux bash prompt to set a proper hostname for your Ansible machine.

# hostnamectl set-hostname ansible-01.centlinux.com

Update Linux Software Packages

Build cache for yum repositories.

# dnf makecache
Rocky Linux 8 - AppStream                       550 kB/s | 9.5 MB     00:17
Rocky Linux 8 - BaseOS                          482 kB/s | 5.6 MB     00:11
Rocky Linux 8 - Extras                          3.4 kB/s |  12 kB     00:03
Metadata cache created.

Update your Linux software packages by using dnf command.

# dnf update -y

Check the Linux Kernel and Operating System versions.

# uname -r
4.18.0-348.12.2.el8_5.x86_64

# cat /etc/rocky-release
Rocky Linux release 8.5 (Green Obsidian)

Install EPEL Yum Repository

Ansible requires some packages from EPEL yum repository. Therefore, you have to install it before moving forward.

# dnf install -y epel-release

Build cache for newly installed yum repositories.

# dnf makecache
Rocky Linux 8 - AppStream                       2.1 kB/s | 4.8 kB     00:02
Rocky Linux 8 - BaseOS                          2.5 kB/s | 4.3 kB     00:01
Rocky Linux 8 - Extras                          4.0 kB/s |  12 kB     00:02
Extra Packages for Enterprise Linux 8 - x86_64  465 kB/s |  11 MB     00:24
Extra Packages for Enterprise Linux Modular 8 - 256 kB/s | 979 kB     00:03
Metadata cache created.

Install Python3 on Rocky Linux 8

Ansible is written in Python programming language. Therefore to install Ansible on Rocky Linux, you are also required to add Python3 language support along with pip3 (Python Package Manager) and required plugins.

# dnf install -y python3 python3-pip

After successful installation of Python3 software, check the version of python3 command.

# python3 -V
Python 3.6.8

The installed version of pip3 may not be the latest one, Therefore, you should upgrade it by using Python Package Manager.

# pip3 install --upgrade pip

Install Ansible on Rocky Linux 8

You can now install Ansible on Rocky Linux from EPEL yum repository by executing the dnf command.

# dnf install -y ansible

After successful installation, verify the version of ansible command that was being installed in previous step.

# ansible --version
ansible 2.9.27
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.6.8 (default, Nov  9 2021, 14:44:26) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3)]

Ansible Command Examples

Generate the SSH Key for your user on Ansible machine.

# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:ac0BBY4e3updr1ZWgBo3h1uvOGzupr1gPPg3FYJtT3k root@ansible-01.centlinux.com
The key's randomart image is:
+---[RSA 3072]----+
|        oo.o     |
|       o..= +    |
|      o .*.= +   |
|     o oo+=.+ E  |
|      o Soo= =   |
|       =  = *    |
|      o =o.=     |
|     . + =*.     |
|      . +*=+.    |
+----[SHA256]-----+

Copy the generated SSH Key to the target Linux machine by using the ssh-copy-id command.

# ssh-copy-id root@192.168.116.128
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.116.128 (192.168.116.128)' can't be established.
ECDSA key fingerprint is SHA256:P6+LBv2gk76hgvcQq7j5aaHHh48/I/f50xTD/GhWqBc.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.116.128's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'root@192.168.116.128'"
and check to make sure that only the key(s) you wanted were added.

Create an Inventory file for Ansible and add an entry for your target Linux machine as follows.

# echo "192.168.116.128 ansible_ssh_user=root" >> hosts

Ping the target Linux machine using ansible command to check connectivity.

# ansible  -i hosts 192.168.116.128 -m ping
192.168.116.128 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/libexec/platform-python"
    },
    "changed": false,
    "ping": "pong"
}

Execute the following command to read a log file from the target Linux machine.

# ansible  -i hosts 192.168.116.128 -m command -a "tail /var/log/firewalld"
192.168.116.128 | CHANGED | rc=0 >>
2022-02-20 20:12:06 WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future release. Please consider disabling it now.
2022-02-25 21:21:42 WARNING: AllowZoneDrifting is enabled. This is considered an insecure configuration option. It will be removed in a future release. Please consider disabling it now.

This is Adhoc command execution mode of Ansible. You can execute any Linux command on the target machine by using above syntax.

To practice Ansible commands and develop a solid foundation for this DevOps tool, we recommend that you should read Ansible for DevOps: Server and configuration management for humans by Jeff Geerling.

Final Thoughts

Installing Ansible on Rocky Linux 8 opens the door to streamlined IT automation and enhanced productivity. With our guide, you now have the foundation to automate your infrastructure and workflows effectively. If you require further assistance or customized configurations, I offer professional services to help you harness the full potential of Ansible.

Visit my Fiverr profile to explore my services and get expert assistance with your Ansible installation and configuration. Let’s simplify your IT operations and boost efficiency together!

Leave a Reply