Ansible has an installation guide ready which will guide you through any missing dependencies as well: https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html

There is also the development version on GitHub: https://github.com/ansible/ansible

Here are the basic guidelines, taken from the guide above:

  1. Locate where Python is installed using the following command:

    which python3
    
  2. Ensure that pip (part of Python) is installed by running this command:

    python3 -m pip -V
    

    If all is well, you should see something like the following:

    pip 21.0.1 from /usr/lib/python3.9/site-packages/pip (python 3.9)
    

    If so, pip is available, and you can move on to the next step.

    If you see an error like No module named pip, you’ll need to install pip under your chosen Python interpreter before proceeding. This may mean installing an additional OS package (for example, python3-pip), or installing the latest pip directly from the Python Packaging Authority by running the following:

    curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
    python3 get-pip.py --user
    
  3. Install Ansible for the current user using pip in your selected Python environment:

    python3 -m pip install --user ansible
    

You haven't mentioned your Ubuntu release installation, so I assume you can have something above 18.04, for which the above should work out fine.

For anything extra, refer to the guide and Ansible's official website, mentioned in the start of this answer.

Good luck.

Answer from Giorgos Saridakis on askubuntu.com
🌐
Ansible
docs.ansible.com › projects › ansible › latest › collections › ansible › builtin › apt_module.html
ansible.builtin.apt module – Manages apt-packages — Ansible Community Documentation
This module is part of ansible-core and included in all Ansible installations. In most cases, you can use the short module name apt even without specifying the collections keyword.
🌐
GitHub
github.com › Oefenweb › ansible-apt
GitHub - Oefenweb/ansible-apt: Ansible role to manage packages and up(date|grade)s in Debian-like systems · GitHub
Ansible role to manage packages and up(date|grade)s in Debian-like systems - Oefenweb/ansible-apt
Starred by 24 users
Forked by 18 users
Languages   Jinja 92.3% | Dockerfile 7.7%
Discussions

How to run apt update and upgrade via Ansible shell - Stack Overflow
I'm trying to use Ansible to run the following two commands: sudo apt-get update && sudo apt-get upgrade -y I know with ansible you can use: ansible all -m shell -u user -K -a "uptime" ... More on stackoverflow.com
🌐 stackoverflow.com
Does the `ansible.builtin.apt` module provide parameters that only downloads the debian packages and not install it?
From what I know using apt-get ... /var/cache/apt/archives directory source Based on the documentation there is no explicit mention of download parameter. Does someone have any information about this. I would like to avoid using command module for something that already has a module in ansible-co... More on forum.ansible.com
🌐 forum.ansible.com
5
0
May 29, 2024
APT and packages with interactive configuraton?
About halfway through the article the author installs Postfix via sudo apt-get install postfix -y, which opens an interactive configuration wizard before the package is installed. I have not been able to find anything the the documentation of ansible.builtin.apt for answering these interactive ... More on forum.ansible.com
🌐 forum.ansible.com
3
0
September 28, 2024
Ansible: Difference between the apt module and command: apt-get -y install X (module) - DevOps Stack Exchange
Consider these YAML tasks which are based on two different parts of some locally-executed Ansible playbook I read here: - name: Update the apt package-index cache i.e. apt-get update apt: update... More on devops.stackexchange.com
🌐 devops.stackexchange.com
December 9, 2018
Top answer
1 of 2
1

Ansible has an installation guide ready which will guide you through any missing dependencies as well: https://docs.ansible.com/ansible/latest/installation_guide/intro_installation.html

There is also the development version on GitHub: https://github.com/ansible/ansible

Here are the basic guidelines, taken from the guide above:

  1. Locate where Python is installed using the following command:

    which python3
    
  2. Ensure that pip (part of Python) is installed by running this command:

    python3 -m pip -V
    

    If all is well, you should see something like the following:

    pip 21.0.1 from /usr/lib/python3.9/site-packages/pip (python 3.9)
    

    If so, pip is available, and you can move on to the next step.

    If you see an error like No module named pip, you’ll need to install pip under your chosen Python interpreter before proceeding. This may mean installing an additional OS package (for example, python3-pip), or installing the latest pip directly from the Python Packaging Authority by running the following:

    curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
    python3 get-pip.py --user
    
  3. Install Ansible for the current user using pip in your selected Python environment:

    python3 -m pip install --user ansible
    

You haven't mentioned your Ubuntu release installation, so I assume you can have something above 18.04, for which the above should work out fine.

For anything extra, refer to the guide and Ansible's official website, mentioned in the start of this answer.

Good luck.

2 of 2
0

If you are looking to use a package manager to maintain/install Ansible, you will want to use the directions provided in the Ansible documentation. This requires you to install and enable the Ansible ppa repository.

$ sudo apt update
$ sudo apt install software-properties-common
$ sudo add-apt-repository --yes --update ppa:ansible/ansible
$ sudo apt install ansible

Source: https://docs.ansible.com/ansible/latest/installation_guide/installation_distros.html#installing-ansible-on-ubuntu

🌐
Ansible
docs.ansible.com › projects › ansible › latest › collections › ansible › builtin › apt_repository_module.html
ansible.builtin.apt_repository module – Add and remove APT repositories — Ansible Community Documentation
Add or remove an APT repositories in Ubuntu and Debian. The below requirements are needed on the host that executes this module. ... This module supports Debian Squeeze (version 6) as well as its successors and derivatives. ... Add and remove deb822 formatted repositories. - name: Add specified repository into sources list ansible.builtin.apt_repository: repo: deb http://archive.canonical.com/ubuntu hardy partner state: present - name: Add specified repository into sources list using specified filename ansible.builtin.apt_repository: repo: deb http://dl.google.com/linux/chrome/deb/ stable main
🌐
Spacelift
spacelift.io › blog › ansible-apt-module
Using Ansible apt Module to Manage Packages
The apt module allows you to run updates on all packages on the system. This command is similar to running the everyday sudo apt update on an Ubuntu server. On Ansible, the playbook commands will look like this:
Published   May 5, 2025
Find elsewhere
🌐
Jon Sprig
jon.sprig.gs › blog › post › 1525
Apt Updates with Ansible – A nice guy's view on life
January 6, 2020 - - hosts: all tasks: - name: Get stat of last run apt stat: path: /var/cache/apt/pkgcache.bin register: apt_run - name: "Apt update, Full-upgrade, autoremove, autoclean check" debug: msg: "Skipping apt-update, etc. actions as apt update was run today" when: "'%Y-%m-%d' | strftime(apt_run.stat.mtime) in ansible_date_time.date" - name: "Apt update, Full-upgrade, autoremove, autoclean" apt: upgrade: full update_cache: yes autoremove: yes autoclean: yes when: "'%Y-%m-%d' | strftime(apt_run.stat.mtime) not in ansible_date_time.date"
🌐
OneUptime
oneuptime.com › home › blog › how to install packages with the ansible apt module
How to Install Packages with the Ansible apt Module
February 21, 2026 - The apt module is one of the most frequently used modules in Ansible if you manage Debian or Ubuntu servers. It wraps the apt-get command and gives you a clean, idempotent way to install, remove, and update packages.
🌐
nixCraft
cyberciti.biz › nixcraft › howto › debian linux › ansible apt update all packages on ubuntu / debian linux
Ansible apt update all packages on Ubuntu / Debian Linux - nixCraft
November 21, 2023 - This page explains how to run apt/apt-get update and upgrade all packages via Ansible and reboot the machine if the need occurs.
🌐
Ansible
forum.ansible.com › get help
Does the `ansible.builtin.apt` module provide parameters that only downloads the debian packages and not install it? - Get Help - Ansible
May 29, 2024 - From what I know using apt-get upgrade -d only downloads the packages in the /var/cache/apt/archives directory source Based on the documentation there is no explicit mention of download parameter. Does someone have any …
🌐
Ansible
forum.ansible.com › get help
APT and packages with interactive configuraton? - Get Help - Ansible
September 28, 2024 - Hello, I am trying to set up Postfix on a Debian 12 web server according to this guide. About halfway through the article the author installs Postfix via sudo apt-get install postfix -y, which opens an interactive configuration wizard before the package is installed.
Top answer
1 of 2
1

 In other words, why not both be done with an apt module?

We can't say for certain, but likely it's just a mistake coming from ignorance. The latter command can be fully implemented using the Ansible apt module, and you should always prefer that over command; command exists as an escape for when there isn't a module for what you want to do.

2 of 2
1

TL;DR: Ansible modules like apt can be mostly implemented with command module, but you lose error checking. Using command is usually sign of a lack of experience with ansible.

To answer this question it is best to look at the code implementing the apt module in ansible. What you can see is that most of the features of the module are implemented by constructing an appropriate apt-get command and executing it, then handling error conditions and presenting them in ansible data structures. Sometimes depending on options the command can be quite complex. In two exceptions for upgrade, in case it is full-upgrade or safe-upgrade it would use aptitude command instead of apt-get.

Of course, the implementation of the module can change going forward, but you should be able to rely on it doing essentially the same thing. On the other hand, if apt-get changes in some way or returns new unhandled type of errors, you will need to change your ansible code in case you use command module directly. While if you use apt module, it is likely the maintainer of the module will implement the new error checking for you.

So in general it is better to use apt to command: apt-get unless the module won't do something that you absolutely need to do and there is no other way around it.

To do the 2nd task in ansible using apt module do:

- name: Ensure some basics
  apt:
    name: aptitude
🌐
Wazuh
documentation.wazuh.com › current › quickstart.html
Quickstart · Wazuh documentation
# sed -i "s/^deb /#deb /" /etc/apt/sources.list.d/wazuh.list # apt update · Now that your Wazuh installation is ready, you can start deploying the Wazuh agent. This can be used to protect laptops, desktops, servers, cloud instances, containers, or virtual machines.
🌐
Jeff Geerling
jeffgeerling.com › blog › 2022 › ansible-playbook-upgrade-ubuntudebian-servers-and-reboot-if-needed
Ansible playbook to upgrade Ubuntu/Debian servers and reboot if needed - Jeff Geerling
January 26, 2022 - ansible.builtin.reboot: when: reboot_required_file.stat.exists == true - name: Remove dependencies that are no longer required. ansible.builtin.apt: autoremove: yes · Updating all your servers with Ansible · apt_key deprecated in Debian/Ubuntu - how to fix in Ansible ·
🌐
Cloudflare
developers.cloudflare.com › directory › cloudflare one › networks › connectors › cloudflare tunnel › downloads › update cloudflared
Update cloudflared · Cloudflare One docs
1 month ago - If installed via apt: Update the cloudflared package: Terminal window · sudo apt-get update && sudo apt-get install --only-upgrade cloudflared · Restart the service: Terminal window · sudo systemctl restart cloudflared.service · If installed via dpkg -i: Use the following commands if you installed cloudflared using the dpkg package manager.
🌐
hashcat
hashcat.net › hashcat
hashcat - advanced password recovery
Ansible Vault · Mozilla key3.db · Mozilla key4.db · Apple Keychain · 7-Zip · RAR3 · RAR5 · PKZIP · PKZIP Master Key · PKZIP Master Key (6 byte optimization) SecureZIP AES-128 · SecureZIP AES-192 · SecureZIP AES-256 · Veeam VBK · WinZip · Android Backup ·
🌐
Jeff Geerling
jeffgeerling.com › blog › 2022 › aptkey-deprecated-debianubuntu-how-fix-ansible
apt_key deprecated in Debian/Ubuntu - how to fix in Ansible - Jeff Geerling
August 30, 2022 - The apt-key command has been deprecated and suggests to ‘manage keyring files in trusted.gpg.d instead’. See the Debian wiki for details. This module is kept for backwards compatiblity for systems that still use apt-key as the main way to manage apt repository keys. So traditionally, I would use a task like the following in my Ansible roles and playbooks:
🌐
Ubuntu Community Hub
discourse.ubuntu.com › project discussion › release
Ubuntu 24.04 LTS (Noble Numbat) Release Notes - Release - Ubuntu Community Hub
March 4, 2026 - Noble Numbat Release Notes Table of Contents Introduction New features in 24.04 LTS Known Issues Official flavours More information Introduction These release notes for Ubuntu 24.04 LTS (Noble Numbat) provide an overvi…
🌐
4sysops
4sysops.com › home › blog › articles › install ubuntu packages with the ansible apt module
Install Ubuntu packages with the Ansible apt module – 4sysops
December 26, 2023 - You can use the Ansible apt module to manage packages that use the apt package manager on Debian-based Linux distributions, such as Ubuntu. The module allows you to perform all basic package management operations, such as installing, removing, ...
🌐
Reddit
reddit.com › r/ansible › why would the "apt" module not install the specified package version?
r/ansible on Reddit: Why would the "apt" module not install the specified package version?
January 12, 2021 -

In my Kubernetes deployment playbook I need to install specific package versions, as follows:

  • containerd.io=1.2.13-2

  • docker-ce=5:19.03.11~3-0~ubuntu-focal

  • docker-ce-cli=5:19.03.11~3-0~ubuntu-focal

If I install them manually, everything works as expected:

apt-get install -y containerd.io=1.2.13-2 docker-ce=5:19.03.11~3-0~ubuntu-focal docker-ce-cli=5:19.03.11~3-0~ubuntu-focal

If I specify the package versions as part of the playbook, I get the correct versions of containerd.io and docker-ce but I get a later version of docker-ce-cli. This seems to have happened by the time Ansible gets to installing docker-ce-cli, although I can't figure out why.

My task is as follows:

- name: "Install Packages"
  package: name={{ item }} state=present
  with_items:
    - containerd.io=1.2.13-2
    - docker-ce=5:19.03.11~3-0~ubuntu-focal
    - docker-ce-cli=5:19.03.11~3-0~ubuntu-focal
  become: yes
  register: install_k8s_common_packages
  tags: k8s_common

The error says the task fails because the requirements would end up downgrading docker-ce-cli, so I'm assuming one of the previous packages (containerd.io or docker-ce) has installed docker-ce-cli along with it, although there's nothing in the Ansible messages to suggest that.

What am I doing wrong? Obviously Ansible complains about using apt-get if I use the working command above via the command module.