diff --git a/roles/pihole/README.md b/roles/pihole/README.md index 42e5c17..f5db3df 100644 --- a/roles/pihole/README.md +++ b/roles/pihole/README.md @@ -4,14 +4,17 @@ Installs [Pi-hole](https://pi-hole.net) v6 by cloning the upstream repository an ## Supported platforms -Ubuntu 24.04 (noble) and 26.04 (resolute), the two images the molecule scenario runs on. The role asserts that the host is Ubuntu before any change. It targets Ubuntu only, not Debian/Proxmox hosts: the upstream installer is run on the Ubuntu hosts it is tested on. 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. +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 `ansible_env.HOME` and `ansible_user_id`, and fails at the Ubuntu assert without facts. +- 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 (`ansible_user_id`). +- `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 @@ -22,11 +25,10 @@ Ubuntu 24.04 (noble) and 26.04 (resolute), the two images the molecule scenario | `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_force_install` | `false` | No effect: the install condition ends in `or true`. | | `pihole_install_script` | the quoted `/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: argument validation templates every listed variable, and their defaults read gathered facts and the merged settings, which do not exist yet when it runs. +`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. @@ -34,7 +36,7 @@ The upstreams default to `1.1.1.1` and `8.8.8.8`, `config.dns.domain` to `lan` a ## Tags -The input asserts carry `always` (and `assert`), so they run whatever tags are selected. The merge import carries `default`, `backups-only`, `config-only` and `updates-only`; the installed check (stat and fact) carries `updates-only`; the configuration-directory check and the reconfigure import carry `config-only`. The version fact, configuration and installation tasks carry no tag of their own, so they run only when no tags are selected. +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 @@ -56,7 +58,7 @@ The input asserts carry `always` (and `assert`), so they run whatever tags are s ## 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 rendered `pihole.toml`, 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. +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 diff --git a/roles/pihole/defaults/main.yml b/roles/pihole/defaults/main.yml index 8b3ee0a..abdfdd4 100644 --- a/roles/pihole/defaults/main.yml +++ b/roles/pihole/defaults/main.yml @@ -12,7 +12,7 @@ pihole_default: version: "v6.4" # I don't like using /tmp because it requires running git as root # most suitable "tmp"-like path under user's HOME is ~/.cache/ I guess - path: "{{ ansible_env.HOME }}/.cache/pi-hole" + path: "{{ ansible_facts['env']['HOME'] }}/.cache/pi-hole" gh_slug: "pi-hole/pi-hole" user: pihole group: pihole @@ -486,9 +486,6 @@ pihole_overrides: {} pihole_testing: false pihole_testing_timeout: 5 -# no effect: the install condition ends in "or true" -pihole_force_install: false - # path to Pi-Hole's automated install script # supports the "--unattended" flag # NOTE the inner quotes are important since path contains a space diff --git a/roles/pihole/meta/argument_specs.yml b/roles/pihole/meta/argument_specs.yml index e27e2ef..a0ffda9 100644 --- a/roles/pihole/meta/argument_specs.yml +++ b/roles/pihole/meta/argument_specs.yml @@ -17,9 +17,11 @@ argument_specs: C(changetoyourownpassword). - >- C(pihole_default) (every setting rendered into C(pihole.toml)) and C(pihole_install_script) (the quoted - installer path under C(repo.path)) are role defaults deliberately not listed here: argument validation - templates every listed variable, and their defaults read gathered facts and the merged settings, which do - not exist yet when it runs. Override settings through C(pihole_overrides) instead. + installer path under C(repo.path)) are role defaults deliberately not listed here, because argument + validation templates every listed variable. C(pihole_install_script) reads the merged settings, which the + role sets only after validation. Templating C(pihole_default) would turn a missing C(pihole_web_password) + into an undefined-variable error before the clean missing-required-arguments message. Override settings + through C(pihole_overrides) instead. options: pihole_web_password: type: raw @@ -64,13 +66,6 @@ argument_specs: default: 5 description: - Seconds to pause after the merge result is printed. Only used when C(pihole_testing) is true. - pihole_force_install: - type: bool - default: false - description: - - >- - No effect: the install task condition ends in C(or true). Whether the installer runs depends only on - C(config.files.database) not existing yet. pihole_apt_dependencies: type: list elements: str diff --git a/roles/pihole/tasks/install.yml b/roles/pihole/tasks/install.yml index 8ce83fe..de7951d 100644 --- a/roles/pihole/tasks/install.yml +++ b/roles/pihole/tasks/install.yml @@ -27,8 +27,7 @@ dest: "{{ __pihole_effective.repo.path }}" version: "{{ __pihole_version }}" become: true - become_user: "{{ ansible_user_id }}" - register: __pihole_repo + become_user: "{{ ansible_facts['user_id'] }}" # what happens if we run the install script with a pre-existing pihole on the system? @@ -38,11 +37,9 @@ # on my machine the install script produced a never-ending output of "y" lines immediately after # successfully restarting the pihole-FTL service (if output was printed to file it quickly # filled the available disk space). Did this happen because Pi-Hole was already installed? +# Only on a first install: on an installed Pi-hole the role leaves upgrades alone. - name: Run Pi-Hole installation script in unattended mode - when: - - (__pihole_repo.changed | bool) or (pihole_force_install | bool) or true - # but not if pihole is already installed, in that case let the updater do its job - - not (__pihole_installed | bool) + when: not (__pihole_installed | bool) block: # the unattended install only works if pihole.toml already exists (configure.yml) @@ -65,11 +62,16 @@ ansible.builtin.slurp: src: "{{ __pihole_effective.repo.path }}/pihole_install.log" register: __pihole_install_log - when: pihole_testing | bool + # under --check the installer was skipped, so there is no log to read + when: + - pihole_testing | bool + - not ansible_check_mode # https://www.shellhacks.com/ansible-cat-file-print-read-file-content - name: Display the install log ansible.builtin.debug: msg: "{{ __pihole_install_log.content | b64decode }}" - when: pihole_testing | bool + when: + - pihole_testing | bool + - not ansible_check_mode # END OF BLOCK diff --git a/roles/pihole/tasks/main.yml b/roles/pihole/tasks/main.yml index 0fd3994..783aed5 100644 --- a/roles/pihole/tasks/main.yml +++ b/roles/pihole/tasks/main.yml @@ -5,13 +5,11 @@ ansible.builtin.import_tasks: validate.yml tags: [always, assert] +# Fact-only, under always: every later task reads the merged settings, so skipping a tag +# must not skip the merge. - name: Merge configuration defaults ansible.builtin.import_tasks: merge-defaults.yml - tags: - - default - - backups-only - - config-only - - updates-only + tags: always - name: Set Pi-Hole version variable ansible.builtin.set_fact: @@ -29,12 +27,13 @@ tags: updates-only # Read by the template tasks: under --check the directory is not created, so on a first run -# there is no directory to render pihole.toml into. +# there is no directory to render pihole.toml into. Under always, because one reader is +# untagged and the other tagged config-only. - name: Check whether the Pi-Hole configuration directory exists ansible.builtin.stat: path: "{{ __pihole_effective.config.files.database | dirname }}" register: __pihole_config_dir - tags: config-only + tags: always - name: Configure Pi-Hole ansible.builtin.import_tasks: configure.yml diff --git a/roles/pihole/tasks/merge-defaults.yml b/roles/pihole/tasks/merge-defaults.yml index 73e7929..67ac3c5 100644 --- a/roles/pihole/tasks/merge-defaults.yml +++ b/roles/pihole/tasks/merge-defaults.yml @@ -20,5 +20,5 @@ - name: Pausing to highlight merge result ansible.builtin.wait_for: - timeout: "{{ pihole_testing_timeout | default(5) }}" + timeout: "{{ pihole_testing_timeout }}" when: pihole_testing | bool diff --git a/roles/pihole/tasks/validate.yml b/roles/pihole/tasks/validate.yml index b2b8664..32b40b2 100644 --- a/roles/pihole/tasks/validate.yml +++ b/roles/pihole/tasks/validate.yml @@ -2,7 +2,7 @@ # Input checks. Imported first in main.yml with tags: always, so a bad input fails before any # change. pihole_web_password is required by the argument spec, which runs before this file. -# The role also reads gathered facts (ansible_env.HOME, ansible_user_id), so a run without +# The role also reads gathered facts (env.HOME, user_id), so a run without # them fails here too. - name: Require Ubuntu ansible.builtin.assert: