Install Ansible on Rocky Linux 8 - CentLinux

Latest

Monday, 28 February 2022

Install Ansible on Rocky Linux 8

In this guide, you will learn about installation of Ansible on Rocky Linux 8 or other RPM based Linux distros.

 

Table of Contents:

     

    What is Ansible:

    Ansible is an open-source software provisioning, configuration management, and application-deployment tool enabling infrastructure as code. It runs on many Unix-like systems, and can configure both Unix-like systems as well as Microsoft Windows. It includes its own declarative language to describe system configuration.

    Ansible was written by Michael DeHaan and acquired by Red Hat in 2015.

    Ansible is agentless, temporarily connecting remotely via SSH or Windows Remote Management (allowing remote PowerShell execution) to do its tasks. (Source: Wikipedia)

    Ansible is included as part of the Fedora distribution of Linux, owned by Red Hat, and is also available for Red Hat Enterprise Linux, CentOS, openSUSE, SUSE Linux Enterprise, Debian, Ubuntu, Scientific Linux, and Oracle Linux via Extra Packages for Enterprise Linux (EPEL), as well as for other operating systems.

    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 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 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:

    Ansible is written in Python programming language, therefore you are required Python3 language support along with pip3 (Python Package Manager) 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:

    You can now install Ansible 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 Usage Example:

    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.

     

    Conclusion:

    In this article, you have learned how to install Ansible on Rocky Linux 8 or other RPM based Linux distros.

    No comments:

    Post a Comment