Files
Kamino/roles/pihole
Defame1297 d84fb4a2f4 fix(pihole): address review findings
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
2026-11-25 18:53:21 +00:00
..
2026-11-25 18:53:21 +00:00
2026-11-25 18:53:21 +00:00

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 the env and user_id facts, and fails at the Ubuntu assert without facts.
  • Role dependencies: none.
  • pihole_web_password is required (argument spec): a secret of its own, sourced from a vaulted variable. It is needed even when pihole_overrides sets the password, because the merge templates all of pihole_default, which references it.
  • pihole_overrides must set config.dns.interface, which has no usable default.
  • become is used for the package, user, file, template and installer tasks. The repository is cloned as the connecting user (the user_id fact).

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 define pihole_overrides in one place only.

  • Secrets: the password is hashed twice with SHA-256 in the template (pwhash). The merge and both template tasks use no_log, and the templates diff: false, so a pihole.toml change shows no diff. pihole_testing masks the web password, app password and TOTP secret in its output. totp_secret and app_password default to empty strings.

  • Install flow: the role creates the pihole system user (nologin), the configuration directory and, on first install, pihole.toml, because the unattended installer needs it first. It clones repo.version and runs the installer with --unattended only when config.files.database does not exist yet. A change to pihole.toml notifies the reconfigure pihole handler, which runs the installer with --reconfigure --unattended.

  • Updates: the role does not upgrade an installed Pi-hole; changing repo.version only moves the clone.

  • Check mode: --check is the supported preview; a play-level check_mode: true is not supported. On a host where the configuration directory does not exist yet, the pihole.toml template tasks are skipped under --check, because the directory they write into is not created.

  • misc.readOnly defaults to true, so Pi-hole does not change pihole.toml through 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.maxDBdays days (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_log is used on every task that carries the password; see Secrets above.