Atlantic.Net Blog

How to Install Ansible on Oracle Linux 8

Ansible is a free and open-source automation platform administrators use to install, configure, and provision multiple systems simultaneously. Nowadays, IT environments are very complex and often need to scale extremely quickly. Automation makes system administrators’ and developers’ jobs more accessible, allowing them to focus on other tasks that add value to an organization. Ansible is very simple to set up and use; you don’t need any coding skills to use Ansible.

This post will show you how to install Ansible on Oracle Linux 8.

Note: This procedure will use 2 servers for demonstration purposes

Step 1 – Set up an SSH Key-based Authentication

Before starting, you must set up an SSH password-less login between the Ansible control node and the manager node. You can do it by setting up an SSH key-based authentication.

First, generate a private and public key using the following command:

ssh-keygen -t rsa

You should see the following output:

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:0qOchzAC0Asm8WPlduL7ZxN6C5NUelScBo2nYVuqPMs root@oraclelinux8
The key's randomart image is:
+---[RSA 3072]----+
|oo  .   .=..     |
|+o.o    +.B      |
|+.+.+ ..oO       |
| o.+ o =+        |
|  . +.+.S        |
|   . *+B..       |
|    ..O+..       |
|     .E+=        |
|      .+.o       |
+----[SHA256]-----+

Next, copy the generated public key to the Ansible-managed nodes using the following command:

ssh-copy-id -i ~/.ssh/id_rsa.pub root@your-remote-host-ip

You should see the following output:

/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.1.10 (192.168.1.10)' can't be established.
ECDSA key fingerprint is SHA256:4K4sZbu1hLHzDGlwmWAJng6nDbxDqp6hnv65KDaOAn0.
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
[email protected]'s password: 

Number of key(s) added: 1

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

Now, run the following command to test the SSH password-less login:

ssh root@your-remote-host-ip

Also Read

How to Setup SSH Public and Private Key in Linux

Step 2 – Install Ansible on Oracle Linux 8

Now, you must install the Ansible package on the Ansible control node. By default, the Ansible package is unavailable in the Oracle Linux 8 default repo, so you must install it from the EPEL repo.

First, install the EPEL repo using the following command:

dnf install -y epel-release

Next, install the Ansible package using the following command:

dnf install ansible -y

Once Ansible is installed, verify the Ansible version with the following command:

ansible --version

You should see the following output:

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 10 2021, 06:50:23) [GCC 8.5.0 20210514 (Red Hat 8.5.0-3.0.2)]

Step 3 – Configure Ansible Hosts

Next, you must edit the Ansible hosts configuration file and define the remote Linux server you want to manage.

You can edit it with the following command:

nano /etc/ansible/hosts

Add the following lines:

[web]
server1 ansible_ssh_host=<manager-node-ip> ansible_ssh_port=22 ansible_ssh_user=root

[database]
dbserver1 ansible_ssh_host=<worker-node-ip> ansible_ssh_port=22 ansible_ssh_user=root

Save and close the file when you are finished.

Next, edit the Ansible configuration file and turn off the deprecation warnings and host key checking:

nano /etc/ansible/ansible.cfg

Add the following line below [defaults]:

deprecation_warnings=False
host_key_checking = False

Save and close the file when you are finished.

Step 4 – How to Use Ansible

At this point, Ansible is installed and configured. Now, you will need to check Ansible’s functionality using the Ansible ad-hoc commands:

First, check the connectivity of all managed nodes using the following command:

ansible -m ping all

If everything is fine, you should see the following output:

dbserver1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
server1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

If you only want to check the database server connectivity, run the following command:

ansible -m ping database

You will get the following output:

dbserver1 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}

To check the free memory on the web server node, run the following command:

ansible -m shell -a "free -m" web

You will get the following output:

server1 | CHANGED | rc=0 >>
              total        used        free      shared  buff/cache   available
Mem:           1817         621         210          21         985        1006
Swap:             0           0           0

To check the MySQL version on the database server node, run the following command:

ansible -m shell -a "mysqladmin --version" database

You should see the following output:

dbserver1 | CHANGED | rc=0 >>
mysqladmin  Ver 8.0.26 for Linux on x86_64 (Source distribution)

Also Read

How to Check CPU Usage and Utilization in Linux

Step 5 – Create Ansible Playbook to Install Packages on Managed Nodes

Ansible allows us to create a playbook to define all tasks that we want to perform on the managed nodes. First, create a directory to store your playbook.

mkdir project

Next, navigate to the created directory and create a YAML file using the following command:

cd project
nano app.yaml

Add the following configuration:

- name: Install Packages
  hosts:
    - web
    - database
  tasks:
  - name: Install php and nginx
    package:
      name:
        - php
        - httpd
      state: present

Save and close the file, then run the playbook using the following command:

ansible-playbook app.yaml

Once the Ansible playbook runs successfully, you will get the following output:

PLAY [Install Packages] **********************************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************************
ok: [server1]
ok: [dbserver1]

TASK [Install php and apache] ****************************************************************************************************************
ok: [server1]
changed: [dbserver1]

PLAY RECAP ***********************************************************************************************************************************
dbserver1                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
server1                    : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   

You can now verify the PHP version on the webserver node using the following command:

ansible -m shell -a "php -v" web

You will get the following output:

server1 | CHANGED | rc=0 >>
PHP 7.2.24 (cli) (built: Oct 22 2019 08:28:36) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies

Conclusion

This guide explained how to install Ansible on Oracle Linux 8. We also explained using Ansible ad-hoc commands and playbook to provision managed nodes. You can now use Ansible to efficiently provide and manage your entire IT infrastructure. Try it on VPS hosting from Atlantic.Net!

Get a $250 Credit and Access to Our Free Tier!

Free Tier includes:
G3.2GB Cloud VPS a Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year