Atlantic.Net Blog

How to Install and Configure Ansible on Fedora

Ansible is an open-source automation tool sponsored by Red Hat. It is written in Python and makes it easier to configure systems and deploy software on multiple servers from the central server. Ansible is cross-platform and primarily intended for IT professionals for application deployment, server updates, configuration management, and many day-to-day tasks.

In this post, we will show you how to install Ansible on Fedora.

Step 1 – Install Ansible

By default, the Ansible package is included in the Fedora default repo. You can install it easily using the following command.

dnf install ansible -y

Once Ansible is installed, you can verify the Ansible version using the following command.

ansible --version

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.9/site-packages/ansible
  executable location = /usr/bin/ansible
  python version = 3.9.5 (default, May 14 2021, 00:00:00) [GCC 11.1.1 20210428 (Red Hat 11.1.1-1)]

Step 2 – Setting Up SSH Key-Based Authentication

Next, you will need to set up an SSH key-based authentication between the Ansible host and a remote host. First, create an SSH key pair on the Ansible host with the following command.

ssh-keygen -t rsa

You will see the generated key in 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:aPKqkVGXq7wmohJ/nCCZon730+x5Wrxeo+3K091Sr4o root@fedora
The key's randomart image is:
+---[RSA 3072]----+
|                 |
|       .         |
|    . o          |
|   . . o         |
| o. . + S        |
|* .+ =    .     .|
|o+ooo.. o  o.o..o|
|+ oo=+ . o++=.o.o|
|=oo=+ ..o+E*=+.o |
+----[SHA256]-----+

Next, copy the generated SSH key to the remote host with the following command.

ssh-copy-id root@remote-host-ip

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.200.200 (192.168.200.200)' can't be established.
ED25519 key fingerprint is SHA256:qBNfJ6Vud/6TCt9bHOa1JwouYDrVuU5g/JhfAxfu3FM.
This key is not known by any other names
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]'"
and check to make sure that only the key(s) you wanted were added.

Step 3 – Configure Ansible Inventory

Next, you will need to edit the Ansible inventory file and define your remote host IP address and SSH username.

nano /etc/ansible/hosts

Add the following configuration at the end of the file.

[fedora]
server1 ansible_host=remote-host-ip ansible_user=root

[all:vars]
ansible_python_interpreter=/usr/bin/python3

Save and close the file, then verify your added configuration using the following command.

ansible-inventory --list -y

You will see the following output.

all:
  children:
    fedora:
      hosts:
        server1:
          ansible_host: 192.168.200.200
          ansible_user: root
    ungrouped: {}

ansible --list-hosts all
  hosts (1):
    server1

Step 4 – Use Ansible to Manage Remote Host

At this point, Ansible is installed and configured to manage the remote host. Now, let’s get our hands dirty with Ansible.

First, check the connectivity between the Ansible host and the remote host using the following command.

ansible -m ping all

If the connection is successful, you will see the following output.

server1 | SUCCESS => {
    "changed": false,
    "ping": "pong"
}

To verify the operating system version of the remote host, run the following command.

ansible -m shell -a "cat /etc/fedora-release" fedora

After the successful command execution, you will see the following output.

server1 | CHANGED | rc=0 >>
Fedora release 34 (Thirty Four)

To install Nginx on the remote host, run the following command.

ansible -m shell -a "dnf install nginx" fedora

To verify the Nginx installation, run the following command.

ansible -m shell -a "nginx -v" fedora

Output:

server1 | CHANGED | rc=0 >>
nginx version: nginx/1.20.2

To check the uptime of the remote host, run the following command.

ansible -m shell -a "uptime" fedora

Output:

server1 | CHANGED | rc=0 >>
 04:15:35 up  3:53,  2 users,  load average: 0.24, 0.14, 0.05

If you want to check the disk space of the remote host, run the following command.

ansible -m shell -a "df -h" fedora

Output:

server1 | CHANGED | rc=0 >>
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        3.9G     0  3.9G   0% /dev
tmpfs           3.9G  124K  3.9G   1% /dev/shm
tmpfs           1.6G  664K  1.6G   1% /run
/dev/sda1       160G  4.0G  157G   3% /
tmpfs           3.9G  168K  3.9G   1% /tmp
tmpfs           796M     0  796M   0% /run/user/0

Conclusion

In this tutorial, we showed you how to install Ansible on Fedora. Then, we explained how to configure Ansible to manage remote hosts with hands-on examples. You can now use Ansible to manage and monitor multiple servers from the central place. You can try Ansible to manage multiple servers on dedicated server 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