Apt

Manage apt packages and repositories.

apt.deb

Add/remove .deb file packages.

apt.deb(source, present=True, force=False)
  • source: filename or URL of the .deb file

  • present: whether or not the package should exist on the system

  • force: whether to force the package install by passing –force-yes to apt

Note:

When installing, apt-get install -f will be run to install any unmet dependencies.

URL sources with present=False:

If the .deb file isn’t downloaded, pyinfra can’t remove any existing package as the file won’t exist until mid-deploy.

Example:

# Note: Assumes wget is installed.
apt.deb(
    {'Install Chrome via deb'},
    'https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb',
)

apt.key

Add apt gpg keys with apt-key.

apt.key(key=None, keyserver=None, keyid=None)
  • key: filename or URL

  • keyserver: URL of keyserver to fetch key from

  • keyid: key identifier when using keyserver

Note:

Always returns an add command, not state checking.

keyserver/id:

These must be provided together.

Examples:

# Note: If using URL, wget is assumed to be installed.
apt.key(
    {'Add the Docker apt gpg key'},
    key='https://download.docker.com/linux/ubuntu/gpg',
)

apt.key(
    {'Install VirtualBox key'},
    'https://www.virtualbox.org/download/oracle_vbox_2016.asc',
)

apt.packages

Install/remove/update packages & update apt.

apt.packages(
    packages=None, present=True, latest=False, update=False, cache_time=None, upgrade=False,
    force=False, no_recommends=False, allow_downgrades=False
)
  • packages: list of packages to ensure

  • present: whether the packages should be installed

  • latest: whether to upgrade packages without a specified version

  • update: run apt update before installing packages

  • cache_time: when used with update, cache for this many seconds

  • upgrade: run apt upgrade before installing packages

  • force: whether to force package installs by passing –force-yes to apt

  • no_recommends: don’t install recommended packages

  • allow_downgrades: allow downgrading packages with version (–allow-downgrades)

Versions:

Package versions can be pinned like apt: <pkg>=<version>

Cache time:

When cache_time is set the /var/lib/apt/periodic/update-success-stamp file is touched upon successful update. Some distros already do this (Ubuntu), but others simply leave the periodic directory empty (Debian).

Examples:

# Update package list and install packages
apt.packages(
    {'Install Asterisk and Vim'},
    ['asterisk', 'vim'],
    update=True,
)

# Install the latest versions of packages (always check)
apt.packages(
    {'Install latest Vim'},
    ['vim'],
    latest=True,
)

# Note: host.fact.os_version is the same as `uname -r` (ex: '4.15.0-72-generic')
apt.packages(
    {'Install kernel headers'},
    ['linux-headers-{}'.format(host.fact.os_version)],
    update=True,
)

apt.ppa

Add/remove Ubuntu ppa repositories.

apt.ppa(name, present=True)
  • name: the PPA name (full ppa:user/repo format)

  • present: whether it should exist

Note:

requires apt-add-repository on the remote host

Example:

# Note: Assumes software-properties-common is installed.
apt.ppa(
    {'Add the Bitcoin ppa'},
    'ppa:bitcoin/bitcoin',
)

apt.repo

Add/remove apt repositories.

apt.repo(name, present=True, filename=None)
  • name: apt source string eg deb http://X hardy main

  • present: whether the repo should exist on the system

  • filename: optional filename to use /etc/apt/sources.list.d/<filename>.list. By default uses /etc/apt/sources.list.

Example:

apt.repo(
    {'Install VirtualBox repo'},
    'deb https://download.virtualbox.org/virtualbox/debian bionic contrib',
)

apt.update

Updates apt repositories.

apt.update(cache_time=None, touch_periodic=False)
  • cache_time: cache updates for this many seconds

  • touch_periodic: touch /var/lib/apt/periodic/update-success-stamp after update

apt.upgrade

Upgrades all apt packages.

apt.upgrade()