Skip to main content

Managing Packages

1. Installing Packages

Option Values Notes
name package_name Name of the package to install
state present, latest State of the package (default: present)
update_cache yes, no Update the package cache before installing (default: no)

Example: Install a package

- name: Install nginx
  apt:
    name: nginx
    state: present

Example: Install latest version of a package

- name: Install latest version of nginx
  apt:
    name: nginx
    state: latest
    update_cache: yes
Note: The apt module is used for Debian/Ubuntu systems. For Red Hat/CentOS, use the yum module.

2. Using Apt Module

Parameter Description Values Notes
name Name of the package to manage string or list Supports wildcards like *
state Desired state of the package present, absent, latest Defaults to present
update_cache Update the apt package cache before installing yes, no Useful when installing new packages
cache_valid_time Cache validity in seconds integer Defaults to 3600 (1 hour)

Example: Install a package

- name: Install nginx
  apt:
    name: nginx
    state: present

Example: Install multiple packages

- name: Install multiple packages
  apt:
    name:
      - git
      - curl
      - vim
    state: present

Example: Update cache and install package

- name: Update cache and install package
  apt:
    name: docker-ce
    state: latest
    update_cache: yes
Note: The apt module requires the python3-apt package to be installed on the target host.

3. Using Yum Module

Parameter Description Values
name Name of the package to install or remove httpd
state State of the package present, absent, latest
update_cache Update the package cache before installing or updating packages true, false
conf_file Path to the yum configuration file /etc/yum.conf

Example: Install a package with Yum

- name: Install httpd package
  yum:
    name: httpd
    state: present

Example: Install a package with update_cache enabled

- name: Install httpd with updated cache
  yum:
    name: httpd
    state: present
    update_cache: true

Example: Remove a package with Yum

- name: Remove httpd package
  yum:
    name: httpd
    state: absent

4. Using DNF Module

Parameter Description Values
name Name of the package to install or remove package_name
state State of the package present, absent, latest
update_cache Update the package cache before installing or updating packages true, false
conf_file Path to the DNF configuration file /etc/dnf/dnf.conf

Example: Install a package using DNF

- name: Install httpd package
  dnf:
    name: httpd
    state: present

Example: Remove a package using DNF

- name: Remove httpd package
  dnf:
    name: httpd
    state: absent

Example: Update package cache and install latest version

- name: Update package cache and install latest httpd
  dnf:
    name: httpd
    state: latest
    update_cache: true

5. Updating Package Cache

Option Values Description
ansible.builtin.yum update_cache Whether to update the package cache before installing packages
ansible.builtin.apt update_cache Whether to update the package cache before installing packages
ansible.builtin.dnf update_cache Whether to update the package cache before installing packages

Example: Updating Package Cache with YUM

- name: Update YUM package cache
  ansible.builtin.yum:
    update_cache: yes

Example: Updating Package Cache with APT

- name: Update APT package cache
  ansible.builtin.apt:
    update_cache: yes
Note: The update_cache option is typically used in combination with package installation or upgrade tasks to ensure the latest package information is available.

6. Removing Packages

Package State Description
name nginx Name of the package to remove
state absent State of the package (absent to remove)

Example: Remove nginx package

- name: Remove nginx package
  apt:
    name: nginx
    state: absent
Note: The apt module is used for Debian/Ubuntu systems. For other systems like RHEL/CentOS, use the yum or dnf module.
Warning: Removing packages can lead to dependency issues. Ensure that removing the package does not break other services or applications.

7. Installing Specific Versions

Option Values Description
name package_name Name of the package to install
version 1.2.3 Specific version to install
state present Ensure package is installed at specified version

Example: Install specific version of a package

- name: Install specific version of package
  apt:
    name: nginx
    version: 1.18.0-0ubuntu1
    state: present
Note: The version parameter is supported by the apt module but not all package managers. Ensure your package manager supports version pinning.

8. Installing Multiple Packages

Package Version Repository Notes
nginx 1.18.0 stable Web server
mysql-server 8.0.23 official Database server
python3-pip 20.3.4 system Python package manager

Example: Installing Multiple Packages

- name: Install multiple packages
  apt:
    name:
      - nginx
      - mysql-server
      - python3-pip
    state: present

9. Using Pip Module

Parameter Description Example
name List of packages to install or uninstall name: ["requests", "flask"]
state Whether to install (present) or remove (absent) packages state: present
version Specific version to install (e.g., 1.2.3) version: 2.28.1
executable Path to the pip executable (e.g., /usr/local/bin/pip3) executable: /usr/local/bin/pip3
virtualenv Path to the virtual environment directory virtualenv: /home/user/myproject/venv

Example: Install a Python Package

- name: Install requests package
  pip:
    name: requests
    state: present

Example: Install Specific Version of Package

- name: Install specific version of flask
  pip:
    name: flask
    version: 2.2.3
    state: present

Example: Use Virtual Environment

- name: Install packages in virtualenv
  pip:
    name: 
      - django
      - gunicorn
    virtualenv: /opt/myapp/venv
    state: present
Note: The pip module requires Python and pip to be installed on the target host.

10. Managing Repositories

Repository Type Description Module Notes
apt_repository Manage APT repositories on Debian/Ubuntu systems community.general.apt_repository Supports GPG key management, repository state (present/absent)
yum_repository Manage YUM repositories on RedHat/CentOS systems community.general.yum_repository Supports GPG key, enabled/disabled state, priority
dnf_repository Manage DNF repositories on Fedora systems community.general.dnf_repository Similar to yum but for newer Fedora versions
pip_repository Manage pip repositories for Python packages community.general.pip_repository Supports index URL, trusted hosts, and extra indices

Example: Adding a Debian APT Repository

- name: Add repository for Ansible
  community.general.apt_repository:
    repo: "deb https://download.example.com/debian stable main"
    state: present
    update_cache: yes

Example: Adding a YUM Repository with GPG Key

- name: Add custom YUM repository
  community.general.yum_repository:
    name: myrepo
    baseurl: https://download.example.com/yum/stable
    gpgcheck: yes
    gpgkey: https://download.example.com/yum/RPM-GPG-KEY-example
    enabled: yes
Note: Ensure GPG keys are valid and accessible to avoid package verification errors.
Warning: Adding repositories can introduce security risks if sources are not trusted.

11. Adding APT Keys

Parameter Description Example
key The key to add, can be a URL or a GPG key content https://example.com/repo-key.gpg
key_id The ID of the GPG key to add (optional if key is provided) ABC123DEF456
keyserver The keyserver to fetch the key from (optional) keyserver.ubuntu.com
state Whether to add or remove the key present

Example: Adding an APT key from a URL

- name: Add APT key from URL
  ansible.builtin.apt_key:
    url: https://example.com/repo-key.gpg
    state: present

Example: Adding an APT key from a keyserver

- name: Add APT key from keyserver
  ansible.builtin.apt_key:
    key_id: ABC123DEF456
    keyserver: keyserver.ubuntu.com
    state: present

12. Installing from URLs

Parameter Description Example
src URL to the package file https://example.com/package.deb
pkg Name of the package to install myapp
state State of the package (present/absent) present

Example: Installing a Debian package from URL

- name: Install package from URL
  apt:
    deb: https://example.com/myapp.deb
    state: present
Note: The apt module supports installing .deb packages directly from URLs.

Was this article helpful?