ReferenceResources
apt
Manage system packages on Debian/Ubuntu hosts.
The apt resource manages packages using apt-get. It is declarative and idempotent.
Input
| Field | Type | Default | Required | Description |
|---|---|---|---|---|
name | string | string[] | — | Yes | Package name(s) to manage |
state | "present" | "absent" | "latest" | "present" | No | Target state |
update | boolean | false | No | Run apt-get update before the package action |
Output
| Field | Type | Description |
|---|---|---|
packages | Record<string, string> | Map of package name to installed version |
changed | boolean | Whether any packages were modified |
Behavior
Check phase
- Asserts the target host uses apt (via
ctx.facts.pkgManager). Allowsnull/unknown but fails on an explicit non-apt package manager. - Queries
dpkg-queryfor each package's install status and version. - For
state: "present"— in desired state if all packages are installed. - For
state: "absent"— in desired state if all packages are not installed. - For
state: "latest"— queriesapt-cache policyto compare installed version against candidate version.
Apply phase
present/latest: runssudo DEBIAN_FRONTEND=noninteractive apt-get install -y -qq <packages>absent: runssudo DEBIAN_FRONTEND=noninteractive apt-get remove -y -qq <packages>- If
update: true, runssudo apt-get update -qqbefore the package action
Annotations
| Property | Value |
|---|---|
| Nature | Declarative |
| Idempotent | Yes |
| Destructive | Yes (when state: "absent") |
| Read-only | No |
| Required capabilities | exec |
Examples
Install a single package
await apt({ name: "nginx", state: "present" })Install multiple packages
await apt({ name: ["git", "curl", "htop"], state: "present" })Install with update
await apt({ name: "nginx", state: "present", update: true })This runs apt-get update before the package action. It's most useful before installs or upgrades
when the package index may be stale.
Remove a package
await apt({ name: "apache2", state: "absent" })Ensure latest version
await apt({ name: "nginx", state: "latest" })Upgrades the package if a newer version is available in the apt cache.
Gotchas
- The
aptresource requiressudoon the remote host for install/remove operations. - When using
nameas an array, all packages are installed/removed in a singleapt-getcall. state: "latest"compares against the candidate version fromapt-cache policy. Run withupdate: trueif you need the latest from the repository.- Only works on hosts with
apt-get(Debian, Ubuntu, and derivatives).