27.4. Automating Debian Provisioning
Installing a machine by hand does not scale past the first few hosts, and worse, no two hand-installed machines are quite alike. Debian automates provisioning at two distinct moments, and the choice between them is the first decision:
| Preseeding | cloud-init | |
|---|---|---|
| Stage | inside debian-installer (d-i) | first boot of a prepared image |
| Input format | debconf answers, one file | #cloud-config YAML, user-data + meta-data |
| Delivery | kernel command line (preseed/url=), local media, or initrd |
NoCloud seed disk, or a metadata service |
| Typical use | bare metal, virtual machines installed from scratch | cloud images, templates, clones |
| Covered in | this chapter, delivered over PXE in Section 27.5 | Chapter 24, Section 24.2.4 |
The two compose rather than compete: a preseeded install (or a cloud image) brings a machine to a working state, and configuration management brings it the rest of the way. This chapter builds both kinds of input on auto1, validates them without needing a client, and then hands the finished files to the network-boot machinery of Section 27.5:
Figure 27.3. The provisioning pipeline. Inputs on the left are prepared and validated offline; the middle stages run unattended on the new machine; the dashed line marks the handoff where configuration management takes over.
27.4.1. Installer Preseeding and Unattended Installation
The debian-installer asks every question through debconf, and debconf
answers can be supplied in advance: that is the whole mechanism. A
preseed file is a list of owner question type value lines, each
answering one installer question in advance. Here is the complete
example, preseed/handbook.cfg in the workspace:
### Localization and keyboard
d-i debian-installer/locale string en_US.UTF-8
d-i keyboard-configuration/xkb-keymap select us
### Network and host identity
d-i netcfg/choose_interface select auto
d-i netcfg/get_hostname string provisioned
d-i netcfg/get_domain string automation.lab
### Account setup; the password is a crypt hash, never plain text
d-i passwd/root-login boolean false
d-i passwd/user-fullname string Automation User
d-i passwd/username string john
d-i passwd/user-password-crypted password $6$LOmIjOQs.7BNwluH$wxCPpTcMW7pv6YZJH2P3BjiKBlacvJcp2qJtiZTiRKrATes.GKJwfvCkhiTqQ8JNPHh9XtNlJ3CwAB7l0iMK5/
d-i passwd/user-default-groups string sudo
### Clock and mirror
d-i clock-setup/utc boolean true
d-i time/zone string UTC
d-i mirror/country string manual
d-i mirror/http/hostname string deb.debian.org
d-i mirror/http/directory string /debian
d-i mirror/http/proxy string
### Partitioning
d-i partman-auto/method string regular
d-i partman-auto/choose_recipe select atomic
d-i partman-partitioning/confirm_write_new_label boolean true
d-i partman/choose_partition select finish
d-i partman/confirm boolean true
d-i partman/confirm_nooverwrite boolean true
### Packages, boot loader, and handoff marker
tasksel tasksel/first multiselect standard, ssh-server
d-i pkgsel/include string curl
d-i grub-installer/only_debian boolean true
d-i finish-install/reboot_in_progress note
d-i preseed/late_command string in-target sh -c 'printf "provisioned\n" > /etc/handbook-provisioned'
Each block answers a stage of the installer. The localization and
network lines need no explanation beyond their values. The account block
is where preseeding earns care: root login is disabled outright, the
interactive user is john, and the password arrives through
passwd/user-password-crypted - never passwd/user-password, which
would put a plaintext password into the file and, because preseed
fetching is plain HTTP in Section 27.5, onto the provisioning network.
The mirror block sets an explicit host so the installer does not need
to guess one. The partitioning block is the one that must be read with
respect: partman-auto with the atomic recipe wipes the target disk
into a single-partition layout, and the three confirm booleans are
the answers that suppress the installer's last-chance warnings - the
whole point of an unattended install, and exactly why this file should
only ever be delivered to machines whose disks are meant to be
overwritten. The tasksel line installs the standard system plus the
SSH server; pkgsel/include adds individual packages; the
late_command runs inside the installed system and drops the marker
file that Section 27.4.4 and Section 27.5.7 use as proof that the
automation, not a person, finished the install.
Validate the file's syntax without installing anything:
$ debconf-set-selections -c preseed/handbook.cfg
-c (--checkonly) parses and exits; the command prints nothing and
exits 0 when the file is well-formed. It is a syntax check, not a
semantic one - a misspelled question name parses fine and is simply
never matched, so the file's real acceptance test is a disposable
install (Section 27.5.7). Check the one line that must be a hash, and
that no plaintext password leaked into the file:
$ grep -n 'passwd/user-password-crypted' preseed/handbook.cfg
14:d-i passwd/user-password-crypted password $6$LOmIjOQs.7BNwluH$wxCPpTcMW7pv6YZJH2P3BjiKBlacvJcp2qJtiZTiRKrATes.GKJwfvCkhiTqQ8JNPHh9XtNlJ3CwAB7l0iMK5/
$ grep -F 'Example-Only-Password' preseed/handbook.cfg
The second command exits 1 having printed nothing: the plaintext this
hash was generated from appears nowhere in the file. Generate your own
hash with openssl passwd -6 (the openssl package) or mkpasswd -m
sha-512 (the whois package); the $6$ prefix marks a SHA-512 crypt
hash, the same format /etc/shadow stores. A preseed file also
answers questions you did not think to set: with auto=true
priority=critical on the kernel command line (Section 27.5.3), the
installer asks nothing it has an answer for and applies packaged
defaults for anything unset.
27.4.2. Cloud Image Initialization and First-Boot Configuration
A cloud image skips the installer entirely: it is a pre-built,
unconfigured Debian disk that expects to be told who it is on first
boot. cloud-init is the standard interpreter for that telling, and
its NoCloud data source takes the instructions from a small seed disk
you build yourself. Here is cloud-init/user-data:
#cloud-config
hostname: provisioned
fqdn: provisioned.automation.lab
manage_etc_hosts: true
ssh_pwauth: false
users:
- name: john
gecos: Automation User
groups:
- sudo
shell: /bin/bash
lock_passwd: false
passwd: "$6$LOmIjOQs.7BNwluH$wxCPpTcMW7pv6YZJH2P3BjiKBlacvJcp2qJtiZTiRKrATes.GKJwfvCkhiTqQ8JNPHh9XtNlJ3CwAB7l0iMK5/"
ssh_authorized_keys:
- ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKNkDr+tmy7xztAAF0/FshHqIGsvhTqSWJ66fn8GQLlY cloud-init-example
package_update: false
packages:
- curl
runcmd:
- [ sh, -c, 'printf "provisioned by cloud-init\n" > /etc/handbook-provisioned' ]
The first line, #cloud-config, is the format marker cloud-init keys
on. The content mirrors the preseed file deliberately - same account,
same groups, same marker file - because both files express the same
policy at different moments: users: creates john with the same crypt
hash under passwd:, ssh_authorized_keys: installs an example login
key (a public key only; the private half never leaves the operator),
and runcmd: runs shell commands late in first boot, writing the same
/etc/handbook-provisioned marker as the preseed late_command. The
companion file cloud-init/meta-data is much shorter:
instance-id: iid-handbook-0001
local-hostname: provisioned
instance-id is the identity cloud-init keys its per-instance work
to: modules in the per-instance frequency run once for each new ID,
and changing the ID on a reused image is what makes cloud-init treat
the next boot as a fresh instance (Section 27.4.3 relies on the same
idea).
None of the tools this subsection uses is part of a minimal
installation: cloud-init itself, cloud-localds (from the
cloud-image-utils package), and isoinfo (from genisoimage).
Install them on auto1 first - sudo apt install cloud-init
cloud-image-utils genisoimage - then validate the user-data against
cloud-init's schema:
$ cloud-init schema --config-file cloud-init/user-data
Valid schema cloud-init/user-data
The command prints the verdict on standard output; the two warnings it prints on standard error (no datasource detected, defaulting instance paths) are expected - this machine is validating a file, not booting from a cloud, so cloud-init has no live datasource to consult. Build the seed image:
$ cloud-localds cloud-init/seed.iso cloud-init/user-data cloud-init/meta-data
cloud-localds packs the two files into a small ISO9660 image whose
volume label, cidata, is what the NoCloud source looks for; it is
silent on success. Inspect what it built:
$ isoinfo -l -i cloud-init/seed.iso
Directory listing of /
d--------- 0 0 0 2048 Nov 23 2026 [ 28 02] .
d--------- 0 0 0 2048 Nov 23 2026 [ 28 02] ..
---------- 0 0 0 59 Nov 23 2026 [ 31 00] META_DAT.;1
---------- 0 0 0 614 Nov 23 2026 [ 32 00] USER_DAT.;1
ISO9660 stores the file names in its uppercase 8.3 form - USER_DAT.;1
is user-data, META_DAT.;1 is meta-data - and the NoCloud source
reads them back under their proper names. To use the seed, attach
seed.iso to a cloud image as a second disk before first boot;
Chapter 24 does exactly that with a Debian
genericcloud image in Section 24.2.4, where the first-boot sequence -
NoCloud discovery, user creation, runcmd execution - can be watched
end to end. The work here is the input side: schema-validated
user-data, a correctly-paired meta-data, and a seed image whose
contents you have listed.
27.4.3. Avoiding Duplicate Machine IDs and Host Keys
Copying a disk image is how fleets are built and how identity bugs are
cloned. Two files make a Debian machine itself, and both are copied
along with everything else unless you intervene: /etc/machine-id,
the 32-hex-digit identity systemd assigns and uses to key the journal,
systemd-networkd's per-machine state, and a range of client
identifiers; and the SSH host keys, which are the server's identity to
every client that connects to it. Duplicate machine IDs make two
machines share journal identities and DHCP client identifiers; duplicate
host keys make every clone's SSH connection trigger the known-hosts
conflict path of Chapter 20 - or worse, quietly accept a
key the reader has already trusted for a different machine.
Exercise both resets on a copy, using a plain directory as a stand-in for a mounted image - the operations are identical. On auto1, as john, in the workspace:
$ mkdir -p clone/etc/ssh && sudo cp /etc/machine-id clone/etc/machine-id
$ cmp /etc/machine-id clone/etc/machine-id
Neither command printed anything - the mkdir/cp pair worked
silently, and cmp is silent because the files are identical: the
duplicate the rest of this subsection eliminates. Reset the clone's
machine ID:
$ sudo truncate -s 0 clone/etc/machine-id && sudo systemd-machine-id-setup --root="$PWD/clone"
Initializing machine ID from random generator.
The message comes from systemd on standard error: an empty or missing
/etc/machine-id makes systemd-machine-id-setup generate a fresh
random ID and install it - in the alternate root given by --root.
Confirm the duplicate is gone:
$ cmp /etc/machine-id clone/etc/machine-id
/etc/machine-id clone/etc/machine-id differ: byte 1, line 1
cmp exiting nonzero with that message is the desired outcome. Now the
host keys, with the one option that exists for exactly this repair:
$ sudo ssh-keygen -A -f "$PWD/clone"
ssh-keygen: generating new host keys: RSA ECDSA ED25519
ssh-keygen option |
Description |
|---|---|
-A |
Generate all missing default host key types, under /etc/ssh (or ROOT/etc/ssh). |
-f PATH |
Use PATH as the key file - or, with -A, as the alternate root. |
-l |
Print the fingerprint of an existing key file. |
Check what the clone now carries:
$ find clone/etc/ssh -type f -printf '%f\n' | sort
ssh_host_ecdsa_key
ssh_host_ecdsa_key.pub
ssh_host_ed25519_key
ssh_host_ed25519_key.pub
ssh_host_rsa_key
ssh_host_rsa_key.pub
Fresh RSA, ECDSA, and ED25519 key pairs, distinct from auto1's. Both
operations transfer directly to real provisioning: for a template,
truncate /etc/machine-id (or remove it) and delete /etc/ssh/ssh_host_*
before the image is ever booted again, and the next boot regenerates
both - the kernel command line systemd.machine_id= offers a
per-boot alternative. A cloud-init image short-circuits the whole
issue when seeded properly: cloud-init's per-instance handling, keyed
to the instance-id of Section 27.4.2, regenerates the machine ID on
a new instance. Whichever path builds your machines, the acceptance
check is the same: after provisioning, no two running machines may
share a machine ID, and no two SSH servers may share a host key -
compare with cmp or sha256sum exactly as done here.
Chapter 24 covers the cloning workflow that
makes these resets routine (Section 24.4.3).
27.4.4. Handing Provisioned Systems to Configuration Management
The last stage of provisioning is deciding what provisioning is not
responsible for. The preseed's late_command and cloud-init's
runcmd can do anything a shell can - and that is precisely their
trap: a long runcmd list is an unversioned, host-specific,
hard-to-test script wearing a provisioning file's clothes. Draw the
boundary instead: provisioning delivers a machine that boots, has an
identity, has an account the control node can reach, and carries one
marker; everything past that point belongs to the same playbooks that
already manage the fleet.
The marker file is the contract between the halves. Both inputs in
this section write /etc/handbook-provisioned, and its presence on a
freshly provisioned host says: the automation completed, no human was
needed, and the machine is ready for its real configuration. On the
control node, enrolling the new host is one inventory line - the
node1 entry of Section 27.3.1, with the address of the new machine -
and the first playbook run is the acceptance test of the whole chain.
Run it as john from the Ansible project directory,
~/debian-handbook/automation/ansible, as in Section 27.3:
$ ansible-playbook --limit node1 site.yml
...
PLAY RECAP *********************************************************************
node1 : ok=6 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Selected output - every task reports ok, no changes. Read that
recap as a design goal, not an accident: a provisioned host that
converges with changed=0 proves the image and the playbook agree on
what a baseline machine looks like. A first run that reports a long
list of changes is telling you the image drifted from the baseline -
fix the image, or you will pay the drift tax on every host you
provision. The handoff completes when the answer to "who configured
this machine?" is always the same: the repository did.