Configure APT

For a full list of keys, refer to the APT configure module schema.

Example 1

Cloud-init version 23.4 will generate a deb822-formatted sources file at /etc/apt/sources.list.d/<distro>.sources instead of /etc/apt/sources.list when sources_list content is in deb822 format.

1#cloud-config
2apt:
3    sources_list: |
4      Types: deb
5      URIs: http://archive.ubuntu.com/ubuntu/
6      Suites: $RELEASE
7      Components: main

Example 2

 1#cloud-config
 2apt:
 3  preserve_sources_list: false
 4  disable_suites:
 5    - $RELEASE-updates
 6    - backports
 7    - $RELEASE
 8    - mysuite
 9  primary:
10    - arches:
11        - amd64
12        - i386
13        - default
14      uri: http://us.archive.ubuntu.com/ubuntu
15      search:
16        - http://cool.but-sometimes-unreachable.com/ubuntu
17        - http://us.archive.ubuntu.com/ubuntu
18      search_dns: false
19    - arches:
20        - s390x
21        - arm64
22      uri: http://archive-to-use-for-arm64.example.com/ubuntu
23
24  security:
25    - arches:
26        - default
27      search_dns: true
28  sources_list: |
29      deb $MIRROR $RELEASE main restricted
30      deb-src $MIRROR $RELEASE main restricted
31      deb $PRIMARY $RELEASE universe restricted
32      deb $SECURITY $RELEASE-security multiverse
33  debconf_selections:
34      set1: the-package the-package/some-flag boolean true
35  conf: |
36      APT {
37          Get {
38              Assume-Yes 'true';
39              Fix-Broken 'true';
40          }
41      }
42  proxy: http://[[user][:pass]@]host[:port]/
43  http_proxy: http://[[user][:pass]@]host[:port]/
44  ftp_proxy: ftp://[[user][:pass]@]host[:port]/
45  https_proxy: https://[[user][:pass]@]host[:port]/
46  sources:
47      source1:
48          keyid: keyid
49          keyserver: keyserverurl
50          source: deb [signed-by=$KEY_FILE] http://<url>/ bionic main
51      source2:
52          source: ppa:<ppa-name>
53      source3:
54          source: deb $MIRROR $RELEASE multiverse
55          key: |
56              ------BEGIN PGP PUBLIC KEY BLOCK-------
57              <key data>
58              ------END PGP PUBLIC KEY BLOCK-------
59      source4:
60          source: deb $MIRROR $RELEASE multiverse
61          append: false
62          key: |
63              ------BEGIN PGP PUBLIC KEY BLOCK-------
64              <key data>
65              ------END PGP PUBLIC KEY BLOCK-------

Update APT on first boot

This example will update the apt repository on first boot; it runs the apt-get update command.

The default is false. However, if packages are given, or if package_upgrade is set to true, then the update will be done irrespective of this setting.

#cloud-config
package_update: true

Specify mirrors

  • Default: auto select based on cloud metadata in EC2, the default is <region>.archive.ubuntu.com.

One can either specify a URI to use as a mirror with the uri key, or a list of URLs using the search key, which will have cloud-init search the list for the first mirror available. This option is limited in that it only verifies that the mirror is DNS-resolvable (or an IP address).

If neither mirror is set (the default), then use the mirror provided by the DataSource. In EC2, that means using <region>.ec2.archive.ubuntu.com.

If no mirror is provided by the DataSource, but search_dns is true, then search for DNS names <distro>-mirror in each of: - FQDN of this host per cloud metadata - localdomain - no domain (which would search domains listed in /etc/resolv.conf)

If there is a DNS entry for <distro>-mirror, then it is assumed that there is a distro mirror at http://<distro>-mirror.<domain>/<distro>. That gives the cloud provider the opportunity to set up mirrors of a distro and expose them only by creating DNS entries.

If none of that is found, then the default distro mirror is used.

#cloud-config
apt:
  primary:
    - arches: [default]
      uri: http://us.archive.ubuntu.com/ubuntu/
# or
apt:
  primary:
    - arches: [default]
      search:
        - http://local-mirror.mydomain
        - http://archive.ubuntu.com
# or
apt:
  primary:
    - arches: [default]
      search_dns: True