Atlantic.Net Blog

How to Install Ansible on Oracle Linux 8

Ansible is a free and open-source automation platform used by system administrators to install, configure and provision multiple systems at once. Nowadays IT environments are very complex and often need to scale extremely quickly. Automation makes system administrators’ and developers’ jobs easier, and allows them to focus attention 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.

In this post, we will show you how to install Ansible on Oracle Linux 8.

Prerequisites

  • Server running Oracle Linux 8 on the Atlantic.Net Cloud Platform. This will be your Manager Node
  • A second server running Oracle Linux 8 on the Atlantic.Net Cloud Platform. This will be your Worker Node
  • A root password configured on your server

Step 1 – Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server. Create a new server, choosing Oracle Linux 8 as the operating system with at least 2GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged in to your server, run the following command to update your base system with the latest available packages.

dnf update -y

Step 2 – Set up an SSH Key-based Authentication

Before starting, you will need to 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 [email protected]
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 [email protected]

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 '[email protected]<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 [email protected]

Also Read

How to Setup SSH Public and Private Key in Linux

Step 3 – Install Ansible on Oracle Linux 8

Now, you will need to install the Ansible package on the Ansible control node. By default, the Ansible package is not available in the Oracle Linux 8 default repo, so you will need to 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 4 – Configure Ansible Hosts

Next, you will need to edit the Ansible hosts configuration file and define the remote Linux server that 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 disable 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 5 – 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 6 – 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

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

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G3.2GB Cloud VPS Server 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