Remove the pihole_force_install input, which had no effect, and the dead term of the install condition. Tag the merge and the configuration directory check always, so --skip-tags config-only no longer skips facts that later tasks read. Skip the installer log under --check, where the installer did not run. Read the env and user_id facts through ansible_facts, drop a redundant default, state the real reasons two defaults stay out of the argument spec, and bring the README to the fixed heading set. Assisted-by: Claude Code:claude-opus-5-5
7.6 KiB
pihole
Installs Pi-hole v6 by cloning the upstream repository and running its installer unattended, with pihole.toml rendered by the role from a merged settings dictionary. Variables and their types are defined in meta/argument_specs.yml, which is the source of truth.
Supported platforms
Ubuntu 24.04 (noble) and 26.04 (resolute), the platforms the molecule scenario runs on. The role asserts that the host is Ubuntu before any change. The package dependencies come from ansible.builtin.apt. The host needs outbound access to the upstream Pi-hole repository (repo.remote in the merged settings) and to the apt mirrors.
The role targets Ubuntu hosts that serve DNS, not Proxmox nodes, so it does not cover Debian trixie: the upstream installer is run on the Ubuntu hosts it is tested on.
Requirements and run order
- Use it by its fully qualified name,
mortis.kamino.pihole, in a play that gathers facts: the role reads theenvanduser_idfacts, and fails at the Ubuntu assert without facts. - Role dependencies: none.
pihole_web_passwordis required (argument spec): a secret of its own, sourced from a vaulted variable. It is needed even whenpihole_overridessets the password, because the merge templates all ofpihole_default, which references it.pihole_overridesmust setconfig.dns.interface, which has no usable default.becomeis used for the package, user, file, template and installer tasks. The repository is cloned as the connecting user (theuser_idfact).
Role variables
| Variable | Default | Description |
|---|---|---|
pihole_web_password |
none (required) | The web/API password, the default of the merged config.webserver.api.password. no_log. |
pihole_overrides |
{} |
Overrides merged recursively over pihole_default. Must set config.dns.interface. |
pihole_default |
all Pi-hole settings, see defaults/main.yml |
Every setting rendered into pihole.toml. Override keys through pihole_overrides, not by redefining it. |
pihole_testing |
false |
Print the merged settings (secrets masked), pause, and on a first install print the installer log. |
pihole_testing_timeout |
5 |
Seconds to pause when testing. |
pihole_install_script |
the quoted <repo.path>/automated install/basic-install.sh |
Installer command. |
pihole_apt_dependencies |
curl, git, procps, lighttpd-mod-deflate, apt-utils, whiptail, dnsutils, bc, unzip, wget, sudo, netcat-openbsd, cron, iproute2 | apt packages installed first. Overriding replaces the list. |
pihole_default and pihole_install_script are not in the argument spec, because argument validation templates every listed variable: pihole_install_script reads the merged settings, which the role sets only after validation, and templating pihole_default would turn a missing pihole_web_password into an undefined-variable error before the clean "missing required arguments" message.
Keys of pihole_overrides that the role itself reads: repo.remote, repo.version (default v6.4), repo.path (default ~/.cache/pi-hole), user and group (default pihole), config.files.database (its directory receives pihole.toml), config.dns.upstreams (must stay a non-empty list), config.dns.interface (must be a non-empty string) and config.webserver.api.password (must stay a string other than changetoyourownpassword). Every other key is rendered into pihole.toml by the template.
The upstreams default to 1.1.1.1 and 8.8.8.8, config.dns.domain to lan and the password to pihole_web_password. Source an overridden password from a vaulted variable.
Tags
The input asserts carry always (and assert), so they run whatever tags are selected. The merge and the configuration-directory check, which only compute facts, carry always too, so skipping a tag never skips a fact a later task reads. The installed check (stat and fact) carries updates-only, and the reconfigure import carries config-only. The version fact, configuration and installation tasks carry no tag of their own, so they run only when no tags are selected.
Example play
- name: Run Pi-hole
hosts: dns_servers
roles:
- role: mortis.kamino.pihole
vars:
pihole_web_password: "{{ vault_pihole_web_password }}"
pihole_overrides:
config:
dns:
interface: eth0
upstreams:
- 9.9.9.9
- 149.112.112.112
Tests
The default molecule scenario runs on noble and resolute. It replaces the upstream repository with a local stub whose installer only logs its arguments and creates the database file, so it tests the merge, the asserts, the user, the configuration directory and the rendered pihole.toml (owner, group and mode), the clone's checked-out version, the installer sequence (install once, then reconfigure from the handler) and idempotence, not Pi-hole itself: no pihole-FTL service runs, so that the service loaded the configuration is not verified. It runs check before the converge (a first-run preview) and after the idempotence run. Negative tests in verify.yml cover a host without Ubuntu facts, a missing pihole_web_password, an unset interface, empty upstreams, a non-string password and the placeholder password.
Notes
-
Idempotent: yes.
-
Merge model: the role sets an internal fact to
pihole_default | combine(pihole_overrides | default({}), recursive=True). The input is only read, never overwritten, so each run recomputes the merge. Dictionaries are not merged across variable sources, so definepihole_overridesin one place only. -
Secrets: the password is hashed twice with SHA-256 in the template (
pwhash). The merge and both template tasks useno_log, and the templatesdiff: false, so apihole.tomlchange shows no diff.pihole_testingmasks the web password, app password and TOTP secret in its output.totp_secretandapp_passworddefault to empty strings. -
Install flow: the role creates the
piholesystem user (nologin), the configuration directory and, on first install,pihole.toml, because the unattended installer needs it first. It clonesrepo.versionand runs the installer with--unattendedonly whenconfig.files.databasedoes not exist yet. A change topihole.tomlnotifies thereconfigure piholehandler, which runs the installer with--reconfigure --unattended. -
Updates: the role does not upgrade an installed Pi-hole; changing
repo.versiononly moves the clone. -
Check mode:
--checkis the supported preview; a play-levelcheck_mode: trueis not supported. On a host where the configuration directory does not exist yet, thepihole.tomltemplate tasks are skipped under--check, because the directory they write into is not created. -
misc.readOnlydefaults totrue, so Pi-hole does not changepihole.tomlthrough its API or CLI and the role's rendering stays authoritative. -
The web interface listens on ports 8080 and 8443 by default (
config.webserver.port), not 80. The role defines no firewall rules. -
The FTL database keeps queries for
config.database.maxDBdaysdays (default 91) and can grow large; lower that key to limit it. -
Double-hash a password by hand from stdin, for comparison with
pwhash:echo -n somelongandsecretstringofyourown | sha256sum | awk '{printf "%s",$1 }' | sha256sum -
Upstream references, not verified by this repository: https://github.com/pi-hole/pi-hole, https://docs.pi-hole.net/ftldns/configfile, https://github.com/pi-hole/pi-hole/issues/5283, https://discourse.pi-hole.net/t/support-writing-to-pihole-toml-directly/70522.
-
no_logis used on every task that carries the password; see Secrets above.