How to Install Ansible on Ubuntu 22.04

Published on March 26, 2023 · Updated on March 24, 2023
How to Install Ansible on Ubuntu 22.04

Ansible is an open-source automation tool used for configuring, managing, and deploying systems. It allows for simple, repeatable, and scalable infrastructure management. Compared to other automation tools such as Chef and Puppet, Ansible is simpler to use and requires less code, making it easier to learn and use.

In this tutorial, we’ll show you how to install Ansible on Ubuntu 22.04 using two methods: using apt-get and using Python3 virtual environments. We’ll also cover how to copy SSH keys to your system, test the Ansible installation, and deploy a demo.

Installation

Using apt-get

The first method to install Ansible on Ubuntu 22.04 is by using the apt-get package manager. This is the simplest and quickest method to get started with Ansible. Here’s how:

  1. Open a terminal and update the package index:
sudo apt update
  1. Install Ansible:
sudo apt install ansible
  1. Verify the installation by checking the version of Ansible:
ansible --version

Using Python3 Virtual Environments (Preferred Method)

The second method to install Ansible on Ubuntu 22.04 is by using Python3 virtual environments. This method is preferred because it allows you to manage multiple versions of Ansible and other Python packages side-by-side.

  1. Install Python3 and pip:
sudo apt update
sudo apt install python3 python3-pip
  1. Install virtualenv:
sudo pip3 install virtualenv
  1. Create a virtual environment:
sudo pip3 install virtualenv

This creates a virtual environment named ansible-env using Python3.

  1. Activate the virutal environment:
source ansible-env/bin/activate
  1. Install Ansible in the new created virtual environment:
pip3 install ansible
  1. Verify the installation by checking the version of Ansible with:
ansible --version

Copy SSH Keys to System

In order to authenticate to the remote systems managed by Ansible, it is recommended to use SSH keys instead of passwords.

If you do not have an existing SSH key pair, you can generate one using the following command:

ssh-keygen

Follow the prompts and leave the passphrase empty if you don’t want to enter it every time you use the key.

Next, you will need to copy the public key to the remote systems you want to manage. You can do this manually or using Ansible.

Manually

You can copy the public key to the remote systems manually by running the following command on your local machine:

ssh-copy-id user@server

Replace user with your username and server with the IP address or hostname of your remote server.

Using Ansible

To copy the public key to the remote systems using Ansible, you can create a playbook like the following:

- name: Copy SSH key to remote hosts
  hosts: all
  tasks:
    - name: Add public key to authorized keys
      authorized_key:
        user: ansible
        key: "{{ lookup('file', '~/.ssh/id_rsa.pub') }}"

This playbook will copy the public key from the current user’s ~/.ssh/id_rsa.pub file to the remote hosts, and add it to the authorized_keys file for the ansible user.

Test Ansible Installation

To test your Ansible installation, create a file named hosts with the IP address or hostname of your remote server:

[web]
your-server-ip-or-hostname

Then, create a playbook named ping.yml:

- name: Ping the web server
  hosts: web
  tasks:
    - name: Ping the server
      ping:

Run the playbook with the following command:

ansible-playbook -i hosts ping.yml

If everything is set up correctly, you should see output similar to the following:

PLAY [Ping the web server] **************************************************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************************************************
ok: [your-server-ip-or-hostname]

TASK [Ping the server] *****************************************************************************************************************************************
ok: [your-server-ip-or-hostname]

PLAY RECAP *****************************************************************************************************************************************************
your-server-ip-or-hostname : ok=2    changed

Deploy a demo

To deploy a simple demo application using Ansible, you can create a playbook like the following:

- name: Deploy demo application
  hosts: all
  become: yes
  tasks:
    - name: Install nginx
      apt:
        name: nginx
        state: latest
    - name: Copy index.html
      copy:
        src: index.html
        dest: /var/www/html/index.html
      notify:
        - Restart nginx
  handlers:
    - name: Restart nginx
      systemd:
        name: nginx
        state: restarted

This playbook will install the nginx package, copy a simple index.html file to the web server root directory, and restart the nginx service.

You can save this playbook to a file, e.g. demo.yml, and run it using the following command:

ansible-playbook demo.yml

This will run the playbook on all hosts in your inventory.

Conclusion

In this article, we have shown you how to install Ansible on Ubuntu 22.04, as well as how to copy SSH keys to the remote hosts, test your Ansible installation, and deploy a simple demo application using Ansible. By following these steps, you should now have a working Ansible installation that you can use to manage your infrastructure.

Load Comments