ansible.builtin.apt (v2.16.5) — module

Manages apt-packages

| "added in version" 0.0.2 of ansible.builtin"

Authors: Matthew Williams (@mgwilliams)

Install Ansible via pip

Install with pip install ansible-core==2.16.5

Description

Manages I(apt) packages (such as for Debian/Ubuntu).


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Install apache httpd  (state=present is optional)
  ansible.builtin.apt:
    name: apache2
    state: present
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Update repositories cache and install "foo" package
  ansible.builtin.apt:
    name: foo
    update_cache: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Remove "foo" package
  ansible.builtin.apt:
    name: foo
    state: absent
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Install the package "foo"
  ansible.builtin.apt:
    name: foo
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Install a list of packages
  ansible.builtin.apt:
    pkg:
    - foo
    - foo-tools
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Install the version '1.00' of package "foo"
  ansible.builtin.apt:
    name: foo=1.00
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Update the repository cache and update package "nginx" to latest version using default release squeeze-backport
  ansible.builtin.apt:
    name: nginx
    state: latest
    default_release: squeeze-backports
    update_cache: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Install the version '1.18.0' of package "nginx" and allow potential downgrades
  ansible.builtin.apt:
    name: nginx=1.18.0
    state: present
    allow_downgrade: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Install zfsutils-linux with ensuring conflicted packages (e.g. zfs-fuse) will not be removed.
  ansible.builtin.apt:
    name: zfsutils-linux
    state: latest
    fail_on_autoremove: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Install latest version of "openjdk-6-jdk" ignoring "install-recommends"
  ansible.builtin.apt:
    name: openjdk-6-jdk
    state: latest
    install_recommends: no
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Update all packages to their latest version
  ansible.builtin.apt:
    name: "*"
    state: latest
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Upgrade the OS (apt-get dist-upgrade)
  ansible.builtin.apt:
    upgrade: dist
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Run the equivalent of "apt-get update" as a separate step
  ansible.builtin.apt:
    update_cache: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Only run "update_cache=yes" if the last one is more than 3600 seconds ago
  ansible.builtin.apt:
    update_cache: yes
    cache_valid_time: 3600
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Pass options to dpkg on run
  ansible.builtin.apt:
    upgrade: dist
    update_cache: yes
    dpkg_options: 'force-confold,force-confdef'
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Install a .deb package
  ansible.builtin.apt:
    deb: /tmp/mypackage.deb
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Install the build dependencies for package "foo"
  ansible.builtin.apt:
    pkg: foo
    state: build-dep
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Install a .deb package from the internet
  ansible.builtin.apt:
    deb: https://example.com/python-ppq_0.1-1_all.deb
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Remove useless packages from the cache
  ansible.builtin.apt:
    autoclean: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Remove dependencies that are no longer required
  ansible.builtin.apt:
    autoremove: yes
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Remove dependencies that are no longer required and purge their configuration files
  ansible.builtin.apt:
    autoremove: yes
    purge: true
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Run the equivalent of "apt-get clean" as a separate step
  apt:
    clean: yes

Inputs

    
deb:
    description:
    - Path to a .deb package on the remote machine.
    - If :// in the path, ansible will attempt to download deb before installing. (Version
      added 2.1)
    - Requires the C(xz-utils) package to extract the control file of the deb package
      to install.
    required: false
    type: path
    version_added: '1.6'
    version_added_collection: ansible.builtin

name:
    aliases:
    - package
    - pkg
    description:
    - A list of package names, like V(foo), or package specifier with version, like V(foo=1.0)
      or V(foo>=1.0). Name wildcards (fnmatch) like V(apt*) and version wildcards like
      V(foo=1.0*) are also supported.
    elements: str
    type: list

clean:
    default: 'no'
    description:
    - Run the equivalent of C(apt-get clean) to clear out the local repository of retrieved
      package files. It removes everything but the lock file from /var/cache/apt/archives/
      and /var/cache/apt/archives/partial/.
    - Can be run as part of the package installation (clean runs before install) or as
      a separate step.
    type: bool
    version_added: '2.13'
    version_added_collection: ansible.builtin

force:
    default: 'no'
    description:
    - Corresponds to the C(--force-yes) to I(apt-get) and implies O(allow_unauthenticated=yes)
      and O(allow_downgrade=yes)
    - This option will disable checking both the packages' signatures and the certificates
      of the web servers they are downloaded from.
    - This option *is not* the equivalent of passing the C(-f) flag to I(apt-get) on the
      command line
    - '**This is a destructive operation with the potential to destroy your system, and
      it should almost never be used.** Please also see C(man apt-get) for more information.'
    type: bool

purge:
    default: 'no'
    description:
    - Will force purging of configuration files if O(state=absent) or O(autoremove=yes).
    type: bool

state:
    choices:
    - absent
    - build-dep
    - latest
    - present
    - fixed
    default: present
    description:
    - Indicates the desired package state. V(latest) ensures that the latest version is
      installed. V(build-dep) ensures the package build dependencies are installed. V(fixed)
      attempt to correct a system with broken dependencies in place.
    type: str

upgrade:
    choices:
    - dist
    - full
    - 'no'
    - safe
    - 'yes'
    default: 'no'
    description:
    - If yes or safe, performs an aptitude safe-upgrade.
    - If full, performs an aptitude full-upgrade.
    - If dist, performs an apt-get dist-upgrade.
    - 'Note: This does not upgrade a specific package, use state=latest for that.'
    - 'Note: Since 2.4, apt-get is used as a fall-back if aptitude is not present.'
    type: str
    version_added: '1.1'
    version_added_collection: ansible.builtin

autoclean:
    default: 'no'
    description:
    - If V(true), cleans the local repository of retrieved package files that can no longer
      be downloaded.
    type: bool
    version_added: '2.4'
    version_added_collection: ansible.builtin

autoremove:
    default: 'no'
    description:
    - If V(true), remove unused dependency packages for all module states except V(build-dep).
      It can also be used as the only option.
    - Previous to version 2.4, autoclean was also an alias for autoremove, now it is its
      own separate command. See documentation for further information.
    type: bool
    version_added: '2.1'
    version_added_collection: ansible.builtin

policy_rc_d:
    default: null
    description:
    - Force the exit code of /usr/sbin/policy-rc.d.
    - For example, if I(policy_rc_d=101) the installed package will not trigger a service
      start.
    - If /usr/sbin/policy-rc.d already exists, it is backed up and restored after the
      package installation.
    - If V(null), the /usr/sbin/policy-rc.d isn't created/changed.
    type: int
    version_added: '2.8'
    version_added_collection: ansible.builtin

dpkg_options:
    default: force-confdef,force-confold
    description:
    - Add dpkg options to apt command. Defaults to '-o "Dpkg::Options::=--force-confdef"
      -o "Dpkg::Options::=--force-confold"'
    - Options should be supplied as comma separated list
    type: str

lock_timeout:
    default: 60
    description:
    - How many seconds will this action wait to acquire a lock on the apt db.
    - Sometimes there is a transitory lock and this will retry at least until timeout
      is hit.
    type: int
    version_added: '2.12'
    version_added_collection: ansible.builtin

only_upgrade:
    default: 'no'
    description:
    - Only upgrade a package if it is already installed.
    type: bool
    version_added: '2.1'
    version_added_collection: ansible.builtin

update_cache:
    aliases:
    - update-cache
    description:
    - Run the equivalent of C(apt-get update) before the operation. Can be run as part
      of the package installation or as a separate step.
    - Default is not to update the cache.
    type: bool

force_apt_get:
    default: 'no'
    description:
    - Force usage of apt-get instead of aptitude
    type: bool
    version_added: '2.4'
    version_added_collection: ansible.builtin

allow_downgrade:
    aliases:
    - allow-downgrade
    - allow_downgrades
    - allow-downgrades
    default: 'no'
    description:
    - Corresponds to the C(--allow-downgrades) option for I(apt).
    - This option enables the named package and version to replace an already installed
      higher version of that package.
    - Note that setting O(allow_downgrade=true) can make this module behave in a non-idempotent
      way.
    - (The task could end up with a set of packages that does not match the complete list
      of specified packages to install).
    - O(allow_downgrade) is only supported by C(apt) and will be ignored if C(aptitude)
      is detected or specified.
    type: bool
    version_added: '2.12'
    version_added_collection: ansible.builtin

default_release:
    aliases:
    - default-release
    description:
    - Corresponds to the C(-t) option for I(apt) and sets pin priorities
    type: str

cache_valid_time:
    default: 0
    description:
    - Update the apt cache if it is older than the O(cache_valid_time). This option is
      set in seconds.
    - As of Ansible 2.4, if explicitly set, this sets O(update_cache=yes).
    type: int

fail_on_autoremove:
    default: 'no'
    description:
    - Corresponds to the C(--no-remove) option for C(apt).
    - If V(true), it is ensured that no packages will be removed or the task will fail.
    - O(fail_on_autoremove) is only supported with O(state) except V(absent).
    - O(fail_on_autoremove) is only supported by C(apt) and will be ignored if C(aptitude)
      is detected or specified.
    type: bool
    version_added: '2.11'
    version_added_collection: ansible.builtin

install_recommends:
    aliases:
    - install-recommends
    description:
    - Corresponds to the C(--no-install-recommends) option for I(apt). V(true) installs
      recommended packages.  V(false) does not install recommended packages. By default,
      Ansible will use the same defaults as the operating system. Suggested packages are
      never installed.
    type: bool

update_cache_retries:
    default: 5
    description:
    - Amount of retries if the cache update fails. Also see O(update_cache_retry_max_delay).
    type: int
    version_added: '2.10'
    version_added_collection: ansible.builtin

allow_unauthenticated:
    aliases:
    - allow-unauthenticated
    default: 'no'
    description:
    - Ignore if packages cannot be authenticated. This is useful for bootstrapping environments
      that manage their own apt-key setup.
    - 'O(allow_unauthenticated) is only supported with O(state): V(install)/V(present)'
    type: bool
    version_added: '2.1'
    version_added_collection: ansible.builtin

allow_change_held_packages:
    default: 'no'
    description:
    - Allows changing the version of a package which is on the apt hold list
    type: bool
    version_added: '2.13'
    version_added_collection: ansible.builtin

update_cache_retry_max_delay:
    default: 12
    description:
    - Use an exponential backoff delay for each retry (see O(update_cache_retries)) up
      to this max delay in seconds.
    type: int
    version_added: '2.10'
    version_added_collection: ansible.builtin

Outputs

cache_update_time:
  description: time of the last cache update (0 if unknown)
  returned: success, in some cases
  sample: 1425828348000
  type: int
cache_updated:
  description: if the cache was updated or not
  returned: success, in some cases
  sample: true
  type: bool
stderr:
  description: error output from apt
  returned: success, when needed
  sample: 'AH00558: apache2: Could not reliably determine the server''s fully qualified
    domain name, using 127.0.1.1. Set the ''ServerName'' directive globally to ...'
  type: str
stdout:
  description: output from apt
  returned: success, when needed
  sample: "Reading package lists...\nBuilding dependency tree...\nReading state information...\n\
    The following extra packages will be installed:\n  apache2-bin ..."
  type: str