Skip to main content

[How-To] Install Ansible on Ubuntu 24.04 LTS

Purpose

This doc will explain how to take a base Ubuntu 24 server and turn it into a hub for Ansible automation that can interact with Proxmox.

Prerequisites

List of prerequisites:

  • Sudo user
  • Ubuntu 24.04 LTS Server (VM)

Instructions

Step 1: Install Ansible on Ubuntu

Update the package list:

sudo apt update

Install Ansible:

sudo apt install -y ansible

Verify the Ansible installation:

ansible --version

Step 2: Install Required Ansible Collections and Modules for Proxmox

Install the community.general collection, which includes the proxmox modules:

ansible-galaxy collection install community.general

Add the Debian repository to your sources list:

echo "deb http://deb.debian.org/debian stable main" | sudo tee /etc/apt/sources.list.d/debian.list

Install the proxmoxer Python library, which the Proxmox API module depends on:

sudo apt update
sudo apt install -y python3-proxmoxer

Disable the Debian repository after installing to avoid pulling other packages from it:

sudo rm /etc/apt/sources.list.d/debian.list
sudo apt update

Verify proxmoxer is installed on the system:

python3 -c "import proxmoxer; print(proxmoxer.__version__)"

Step 3: Set Up API Access for Proxmox

Create a Proxmox API user specifically for Ansible access:

  1. Log into Proxmox and navigate to Datacenter > Permissions > Users.
  2. Add a new user (e.g., ansible_user) with API Token privileges.

Create an API token for the user:

  1. Go to API Tokens under the new user and create a token (e.g., ansible_token).
  2. Note the username@realm!tokenid and the token’s secret, as you'll need them in Ansible.

Assign the necessary permissions to the API user:

  1. Go to Datacenter > Permissions > Add > User Permission.
  2. Assign VM.Allocate, VM.Config.Disk, VM.Config.HWType, and VM.Console permissions (or others as needed).

Step 4: Configure Ansible Inventory for Proxmox

Create a directory for your Ansible project and configure the inventory:

cd /etc/ansible 
sudo nano inventory.yml

Add the Proxmox server to the inventory file:

---
proxmox:
  hosts:
    192.168.1.10:
  vars:
    ansible_user: root
    ansible_password: ********
    ansible_ssh_common_args: '-o StrictHostKeyChecking=no'
    ansible_python_interpreter: auto_silent

Replace IP address under hosts with your proxmox ve host IP and ansible_password with your proxmox ve root password. For better security use API token and ansbile vault.

Step 5: Create Ansible Playbook for Ping Test to Proxmox

Create a playbook file:

sudo nano test-ping.yml

Add the following content to the playbook to test ping to proxmox ve node:

---
- name: Test connectivity to all hosts
  hosts: all
  gather_facts: no
  tasks:
    - name: Ping test
      ansible.builtin.ping:

Run the playbook with the following command:

ansible-playbook test-ping.yml

If everything works, you should see output like this:

user@ansible-server:/etc/ansible$ ansible-playbook test-ping.yml

PLAY [Test connectivity to all hosts] **************************************************************************************

TASK [Ping test] ***********************************************************************************************************
ok: [192.168.1.10]

PLAY RECAP *****************************************************************************************************************
192.168.1.10                 : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

This confirms you can ping the proxmox ve host via Ansible playbooks from your Ansible server.

Step 6: Create Ansible Playbook for Connection Test to Proxmox

Create a playbook file:

sudo nano test-connection.yml

Add the following content to the playbook to test the connection to proxmox ve node:

---
- name: Proxmox example task
  hosts: proxmox
  tasks:
    - name: Example Proxmox task
      ansible.builtin.debug:

Run the playbook with the following command:

ansible-playbook test-connection.yml

If everything works, you should see output like this:

user@ansible-server:/etc/ansible$ ansible-playbook test-connection.yml

PLAY [Proxmox example task] ************************************************************************************************

TASK [Gathering Facts] *****************************************************************************************************
ok: [192.168.1.10]

TASK [Example Proxmox task] ************************************************************************************************
ok: [192.168.1.10] => {
    "msg": "Hello world!"
}

PLAY RECAP *****************************************************************************************************************
192.168.1.10                 : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

This confirms you can connect and authenticate to the proxmox ve host via Ansible playbooks from your Ansible server.

At this point, you have a fully working Ansible server built and configured with proxmox ve nodes for automation. Start creating playbooks for your automation.