feat(nfs_client): add a role that manages NFS client entries in fstab #451
No files matched your search
@@ -2,7 +2,7 @@
|
||||
|
||||
An Ansible Collection of reusable, hardened roles for a self-hosted home lab:
|
||||
`cloudflared`, `docker`, `fail2ban`, `gitea`, `gitea_runner`, `network_interfaces`,
|
||||
`pihole`, `pve_lxc`, `sshd`, `system_baseline`, `systemd_hardening`, `tailscale`, `traefik`, `ufw` and
|
||||
`nfs_client`, `pihole`, `pve_lxc`, `sshd`, `system_baseline`, `systemd_hardening`, `tailscale`, `traefik`, `ufw` and
|
||||
`user_management`, plus the internal `_binary_update`. Roles still to come are listed under [Roadmap](#roadmap).
|
||||
|
||||
Status: roles arrive in waves. Release 3.2.0 holds the `cloudflared`, `docker`, `fail2ban`, `gitea`, `gitea_runner`, `network_interfaces`, `pihole`, `pve_lxc`, `sshd`, `system_baseline`, `systemd_hardening`, `tailscale`, `traefik`, `ufw` and `user_management` roles (and the internal `_binary_update`, which `cloudflared`, `gitea`, `gitea_runner` and `traefik` call).
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
---
|
||||
minor_changes:
|
||||
- >-
|
||||
nfs_client - New role. Installs ``nfs-common`` and manages NFS mount entries in ``/etc/fstab`` from ``nfs_client_mounts``
|
||||
(``src``, ``path``, optional ``opts`` and ``state`` ``present`` or ``absent``), with ``nfs_client_default_opts``
|
||||
(default ``_netdev,nofail``) for an entry that sets none. It creates each mount point without owner, group or mode,
|
||||
writes only the fstab line, never mounts or unmounts, and reloads systemd after a change. An ``absent`` entry loses its
|
||||
line and nothing else. Declares Ubuntu noble and resolute; the molecule scenario never mounts a share.
|
||||
+2
-2
@@ -7,7 +7,7 @@ authors:
|
||||
- Defame1297
|
||||
description: >-
|
||||
Reusable, hardened Ansible roles for a self-hosted home lab (cloudflared, docker, fail2ban,
|
||||
gitea, gitea_runner, network_interfaces, pihole, pve_lxc, sshd, system_baseline,
|
||||
gitea, gitea_runner, network_interfaces, nfs_client, pihole, pve_lxc, sshd, system_baseline,
|
||||
systemd_hardening, tailscale, traefik, ufw, user_management; _binary_update is internal).
|
||||
license:
|
||||
- GPL-3.0-or-later
|
||||
@@ -22,7 +22,7 @@ homepage: https://git.rkdr.net/mortis/kamino
|
||||
issues: https://git.rkdr.net/mortis/kamino/issues
|
||||
# CI tests the versions pinned in requirements.yml; the lower bounds here are not tested.
|
||||
# No upper bound, so a consumer's newer pin does not conflict. community.general is used by system_baseline and ufw,
|
||||
# ansible.posix by system_baseline and user_management, community.proxmox by pve_lxc (Proxmox VE guest lifecycle).
|
||||
# ansible.posix by nfs_client, system_baseline and user_management, community.proxmox by pve_lxc (Proxmox VE guest lifecycle).
|
||||
# community.proxmox's modules import the Python library proxmoxer (2.3 or later) on the controller; galaxy.yml
|
||||
# cannot declare a Python dependency, so the README says it and requirements-dev.txt pins it for CI.
|
||||
dependencies:
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
# nfs_client
|
||||
|
||||
Installs the NFS client (`nfs-common`) and manages NFS mount entries in `/etc/fstab`. It writes the fstab line, creates the mount point and reloads systemd; it never mounts or unmounts anything. Variables and their types are defined in [`meta/argument_specs.yml`](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 a Debian-family host before any change.
|
||||
|
||||
Untested platforms: Debian 13 (trixie). The role has no OS-specific branch, but no scenario runs on it until the Debian image (#226) exists.
|
||||
|
||||
## Requirements and run order
|
||||
|
||||
- Use it by its fully qualified name, `mortis.kamino.nfs_client`, in a play that gathers facts: the Debian-family assert reads them.
|
||||
- Role dependencies: none. The role calls `ansible.posix.mount`, so the Collection's `ansible.posix` dependency applies.
|
||||
- `become` is used for every task that changes the host.
|
||||
- The role does not set up an NFS server, autofs, CIFS/SMB or Kerberos, and it does not wait for a mount.
|
||||
|
||||
## Role variables
|
||||
|
||||
| Variable | Default | Description |
|
||||
|---|---|---|
|
||||
| `nfs_client_mounts` | `[]` | The mounts to manage: a list of entries, each mount point once. With no entries only the client package is installed. |
|
||||
| `nfs_client_default_opts` | `_netdev,nofail` | Options for an entry that sets none. |
|
||||
|
||||
An entry takes these keys:
|
||||
|
||||
| Key | Default | Description |
|
||||
|---|---|---|
|
||||
| `src` | required | The export as `server:/export`, one word without spaces. |
|
||||
| `path` | required | The mount point: absolute, without spaces, not `/`, no trailing slash. |
|
||||
| `opts` | `nfs_client_default_opts` | The fstab options, one comma-separated word. Put the NFS version here, for example `vers=4.2`. |
|
||||
| `state` | `present` | `present` writes the fstab line. `absent` removes it and nothing else. |
|
||||
|
||||
## Tags
|
||||
|
||||
- `nfs_client`: everything the role does.
|
||||
- `install`: the client package.
|
||||
- `configure`: the mount points and the fstab entries.
|
||||
|
||||
Validation runs under `always`, so a bad input fails before any change whatever tags the run selects.
|
||||
|
||||
## Example play
|
||||
|
||||
```yaml
|
||||
- name: NFS client
|
||||
hosts: nfs_clients
|
||||
roles:
|
||||
- role: mortis.kamino.nfs_client
|
||||
vars:
|
||||
nfs_client_mounts:
|
||||
- src: nas.example:/export/data
|
||||
path: /mnt/data
|
||||
- src: nas.example:/export/media
|
||||
path: /mnt/media
|
||||
opts: _netdev,nofail,ro,vers=4.2
|
||||
```
|
||||
|
||||
## Tests
|
||||
|
||||
`molecule/default` runs on Ubuntu noble and resolute: a check on a fresh host, the converge, a check on the configured host, an idempotence run, and `verify.yml`. The scenario seeds an fstab line for `/mnt/data` that names another server and a line and a directory for `/mnt/old`, so the run shows that a line is replaced once and that an `absent` entry loses only its line.
|
||||
|
||||
`verify.yml` first checks a refusal for each validation on the controller, once, in check mode (a missing or empty `src`, a `src` without an export, a relative path, the root, a trailing slash, options with a space or none, a duplicate path, `mounted` and `remounted`, a host without facts). It then asserts, on each container, the client package, the exact fstab fields, that a new mount point is root's and an existing one keeps its owner and mode, that the removed entry's directory and file are intact, that `/proc/mounts` shows nothing mounted, and that systemd has a mount unit for each new entry and none for the removed one.
|
||||
|
||||
What it does not show: that a real share mounts at boot (the server in the scenario does not exist), that `x-systemd.automount` or any option other than the defaults behaves, and Debian trixie.
|
||||
|
||||
## Notes
|
||||
|
||||
- **Why only the fstab.** `ansible.posix.mount` with `state: mounted` mounts from the play's own process. Where that process runs in a private mount namespace (a hardened sshd unit can), systemd and containers do not see the mount. The role writes the line and reloads systemd, and the share is mounted at the next boot or with `systemctl start <unit>.mount`. New options reach a share that is already mounted only after it is remounted or the host reboots.
|
||||
- **No owner, group or mode on the mount point.** Once a share is mounted the path is the export root, and a `chown` or `chmod` there changes the permissions on the NFS server. A new directory gets the system defaults, and an existing one is left as it is.
|
||||
- **A share that is mounted and unreachable.** On a re-run the role looks at each `present` mount point, and a mounted path whose server is gone can block that look. Fix or unmount the share, or leave that entry out of the run, before running the role again.
|
||||
- **`absent` leaves the share alone.** The entry's line is removed; the share stays mounted until it is unmounted and the mount point is not deleted.
|
||||
- **Default options.** `_netdev` orders the mount after the network and puts it in `remote-fs.target`; `nofail` keeps an unreachable server from holding up the boot. `x-systemd.automount` changes when a mount appears and can hide an unreachable server until first access, so it is left to the host that wants it. The `docker` role's `docker_stop_at_shutdown` relies on the same `remote-fs.target` ordering.
|
||||
- `--check` is the supported preview; a play-level `check_mode: true` is not supported. `apt` and the fstab edit both report what would change.
|
||||
- Idempotent: yes.
|
||||
@@ -0,0 +1,8 @@
|
||||
---
|
||||
# Each entry: src (server:/export) and path (absolute mount point) are required; opts falls back
|
||||
# to nfs_client_default_opts; state is present (default) or absent.
|
||||
nfs_client_mounts: []
|
||||
|
||||
# _netdev orders the mount after the network and puts it in remote-fs.target; nofail keeps an
|
||||
# unreachable server from holding up the boot. x-systemd.automount is left to the host that wants it.
|
||||
nfs_client_default_opts: "_netdev,nofail"
|
||||
@@ -0,0 +1,7 @@
|
||||
---
|
||||
# systemd builds a mount unit from each fstab line only when it reloads, so the reload is what
|
||||
# makes a changed line known to systemd. It mounts nothing.
|
||||
- name: Reload systemd for the nfs_client fstab entries
|
||||
become: true
|
||||
ansible.builtin.systemd_service:
|
||||
daemon_reload: true
|
||||
@@ -0,0 +1,57 @@
|
||||
---
|
||||
argument_specs:
|
||||
main:
|
||||
short_description: Install the NFS client and manage NFS entries in /etc/fstab
|
||||
description:
|
||||
- >-
|
||||
Asserts a Debian-family host, installs C(nfs-common), creates each mount point of a C(present) entry without
|
||||
owner, group or mode, writes or removes the entry's line in C(/etc/fstab), and reloads systemd after a change
|
||||
so that the mount unit is generated now. Nothing is mounted.
|
||||
- >-
|
||||
It never mounts or unmounts anything. An C(absent) entry only loses its fstab line: the share stays mounted
|
||||
until it is unmounted or the host reboots, and the mount point is not removed. Needs gathered facts.
|
||||
options:
|
||||
nfs_client_mounts:
|
||||
type: list
|
||||
elements: dict
|
||||
default: []
|
||||
description:
|
||||
- >-
|
||||
The NFS mounts to manage. Each mount point may appear once. With the default (no entries) only the client
|
||||
package is installed.
|
||||
options:
|
||||
src:
|
||||
type: str
|
||||
required: true
|
||||
description:
|
||||
- The export as C(server:/export), one word without spaces.
|
||||
path:
|
||||
type: str
|
||||
required: true
|
||||
description:
|
||||
- >-
|
||||
The mount point: an absolute path without spaces, not C(/) and without a trailing slash. Created, when
|
||||
the entry is C(present), with no owner, group or mode.
|
||||
opts:
|
||||
type: str
|
||||
description:
|
||||
- >-
|
||||
The fstab options, one comma-separated word without spaces. Falls back to C(nfs_client_default_opts)
|
||||
when left out or empty. Put the NFS version here, for example C(vers=4.2).
|
||||
state:
|
||||
type: str
|
||||
default: present
|
||||
choices: [present, absent]
|
||||
description:
|
||||
- >-
|
||||
C(present) writes the fstab line. C(absent) removes it and nothing else. The role has no C(mounted),
|
||||
C(unmounted) or C(remounted): mounting is left to systemd, so that a mount made from a play running in a
|
||||
private mount namespace (a hardened sshd unit) cannot go unseen by systemd and by containers.
|
||||
nfs_client_default_opts:
|
||||
type: str
|
||||
default: "_netdev,nofail"
|
||||
description:
|
||||
- >-
|
||||
Options for an entry that sets none. C(_netdev) orders the mount after the network and puts it in
|
||||
C(remote-fs.target); C(nofail) keeps an unreachable server from holding up the boot.
|
||||
C(x-systemd.automount) is not in the default.
|
||||
@@ -0,0 +1,18 @@
|
||||
---
|
||||
galaxy_info:
|
||||
role_name: nfs_client
|
||||
namespace: mortis
|
||||
author: Dragonish
|
||||
license: GPL-3.0-or-later
|
||||
description: Install the NFS client and manage NFS mount entries in /etc/fstab, without mounting anything.
|
||||
min_ansible_version: "2.22"
|
||||
platforms:
|
||||
- name: Ubuntu
|
||||
versions:
|
||||
- noble
|
||||
- resolute
|
||||
galaxy_tags:
|
||||
- nfs
|
||||
- fstab
|
||||
- storage
|
||||
dependencies: []
|
||||
@@ -0,0 +1,7 @@
|
||||
---
|
||||
# The mounts come from molecule.yml.
|
||||
- name: Converge
|
||||
hosts: all
|
||||
gather_facts: true
|
||||
roles:
|
||||
- role: mortis.kamino.nfs_client
|
||||
@@ -0,0 +1,75 @@
|
||||
---
|
||||
# Runs on the repository's own runner (label kamino-molecule), in the molecule-ubuntu2404
|
||||
# (MOLECULE_IMAGE) and molecule-ubuntu2604 (MOLECULE_IMAGE_2604) images, pinned in
|
||||
# .gitea/workflows/molecule.yml. Needs the Ubuntu mirrors: the role installs nfs-common. The
|
||||
# containers run systemd as PID 1 with a writable cgroup, so a daemon-reload builds mount units
|
||||
# from /etc/fstab; no privileged mode. Nothing is ever mounted: the server below does not exist.
|
||||
driver:
|
||||
name: docker
|
||||
platforms:
|
||||
- name: ${KAMINO_RUN_ID:-local}-nfs-client-noble
|
||||
labels:
|
||||
net.rkdr.mortis.molecule-run: "${KAMINO_RUN_ID:-local}"
|
||||
image: ${MOLECULE_IMAGE}
|
||||
pre_build_image: true
|
||||
pull: true
|
||||
command: /sbin/init
|
||||
cgroupns_mode: host
|
||||
volumes:
|
||||
- /sys/fs/cgroup:/sys/fs/cgroup:rw
|
||||
tmpfs:
|
||||
- /run
|
||||
- /run/lock
|
||||
security_opts:
|
||||
- apparmor=unconfined
|
||||
- name: ${KAMINO_RUN_ID:-local}-nfs-client-resolute
|
||||
labels:
|
||||
net.rkdr.mortis.molecule-run: "${KAMINO_RUN_ID:-local}"
|
||||
image: ${MOLECULE_IMAGE_2604}
|
||||
pre_build_image: true
|
||||
pull: true
|
||||
command: /sbin/init
|
||||
cgroupns_mode: host
|
||||
volumes:
|
||||
- /sys/fs/cgroup:/sys/fs/cgroup:rw
|
||||
tmpfs:
|
||||
- /run
|
||||
- /run/lock
|
||||
security_opts:
|
||||
- apparmor=unconfined
|
||||
provisioner:
|
||||
name: ansible
|
||||
# One source for every play, so the converge and the plays in verify.yml start from the same
|
||||
# valid input: a default-options mount, one with its own options, one to remove (prepare.yml
|
||||
# seeds its line), and /mnt/data, whose seeded line has another server and must be replaced.
|
||||
inventory:
|
||||
group_vars:
|
||||
all:
|
||||
nfs_client_mounts:
|
||||
- src: nas.example:/export/data
|
||||
path: /mnt/data
|
||||
- src: nas.example:/export/media
|
||||
path: /mnt/media
|
||||
opts: _netdev,nofail,ro,vers=4.2
|
||||
- src: nas.example:/export/old
|
||||
path: /mnt/old
|
||||
state: absent
|
||||
verifier:
|
||||
name: ansible
|
||||
scenario:
|
||||
# molecule's default sequence minus both destroys (scripts/run_molecule_scenarios.sh removes the
|
||||
# containers as soon as this scenario ends; after a local run, run `molecule destroy` yourself),
|
||||
# plus a check before the converge (fresh host) and one after it (configured host).
|
||||
test_sequence:
|
||||
- dependency
|
||||
- cleanup
|
||||
- syntax
|
||||
- create
|
||||
- prepare
|
||||
- check
|
||||
- converge
|
||||
- check
|
||||
- idempotence
|
||||
- side_effect
|
||||
- verify
|
||||
- cleanup
|
||||
@@ -0,0 +1,43 @@
|
||||
---
|
||||
# The state a host has before the role runs: a line for /mnt/data that names another server (the
|
||||
# role must replace it, once) and a line and a mount point for /mnt/old, the entry the converge
|
||||
# removes. The file in /mnt/old shows that removing the line leaves the directory alone.
|
||||
- name: Prepare
|
||||
hosts: all
|
||||
gather_facts: false
|
||||
tasks:
|
||||
# The image ships without apt lists, and --check does not refresh them, so without this the
|
||||
# fresh-host check finds no nfs-common to install.
|
||||
- name: Refresh the apt cache
|
||||
ansible.builtin.apt:
|
||||
update_cache: true
|
||||
|
||||
- name: Seed the fstab lines
|
||||
ansible.builtin.lineinfile:
|
||||
path: /etc/fstab
|
||||
line: "{{ item }}"
|
||||
create: true
|
||||
mode: "0644"
|
||||
loop:
|
||||
- old-nas.example:/export/data /mnt/data nfs defaults 0 0
|
||||
- nas.example:/export/old /mnt/old nfs _netdev,nofail 0 0
|
||||
|
||||
- name: Create the mount point that the converge leaves alone
|
||||
ansible.builtin.file:
|
||||
path: /mnt/old
|
||||
state: directory
|
||||
mode: "0750"
|
||||
|
||||
- name: Put a file in it
|
||||
ansible.builtin.copy:
|
||||
content: kept
|
||||
dest: /mnt/old/keep
|
||||
mode: "0644"
|
||||
|
||||
# An existing mount point of a present entry: the role must leave its owner and mode alone.
|
||||
- name: Create a mount point with an owner and a mode of its own
|
||||
ansible.builtin.file:
|
||||
path: /mnt/media
|
||||
state: directory
|
||||
owner: daemon
|
||||
mode: "0710"
|
||||
@@ -0,0 +1,21 @@
|
||||
---
|
||||
# One failing run: includes the role with the inputs the caller sets on the include, and asserts
|
||||
# that the expected message, not something else, stopped it. Input: nfs_client_test_case (name,
|
||||
# expect).
|
||||
- name: "Run the role, case {{ nfs_client_test_case.name }}"
|
||||
block:
|
||||
- name: "Include the role, case {{ nfs_client_test_case.name }}"
|
||||
ansible.builtin.include_role:
|
||||
name: mortis.kamino.nfs_client
|
||||
|
||||
# Only reached when the include returned. The message must not contain the expected
|
||||
# text, so the rescue below cannot mistake it for the failure under test.
|
||||
- name: "Fail because the include returned, case {{ nfs_client_test_case.name }}"
|
||||
ansible.builtin.fail:
|
||||
msg: The role accepted the input
|
||||
rescue:
|
||||
- name: "The expected failure stopped the run, case {{ nfs_client_test_case.name }}"
|
||||
ansible.builtin.assert:
|
||||
that: nfs_client_test_case.expect in (ansible_failed_result.msg | default(''))
|
||||
fail_msg: "Expected '{{ nfs_client_test_case.expect }}', got: {{ ansible_failed_result.msg | default('') }}"
|
||||
quiet: true
|
||||
@@ -0,0 +1,247 @@
|
||||
---
|
||||
# The refusals below stop in validation, before any task that changes a host, so they need no
|
||||
# container: they run once, on the controller, in check mode so that a regressed guard cannot
|
||||
# change it.
|
||||
#
|
||||
# Runs first, before any play gathers facts: the role refuses a host it cannot identify as
|
||||
# Debian-family, before any change.
|
||||
- name: Refuse a host without facts
|
||||
hosts: localhost
|
||||
connection: local
|
||||
check_mode: true
|
||||
gather_facts: false
|
||||
tasks:
|
||||
- name: Run the role without gathered facts
|
||||
ansible.builtin.include_tasks: tasks/failed_run.yml
|
||||
vars:
|
||||
nfs_client_test_case:
|
||||
name: no-facts
|
||||
expect: nfs_client supports Debian-family hosts only
|
||||
|
||||
# Negative tests: each validation stops the role before any change, with its own message. The
|
||||
# inputs replace the converge's mounts for one include only.
|
||||
- name: Refuse invalid input
|
||||
hosts: localhost
|
||||
connection: local
|
||||
check_mode: true
|
||||
gather_facts: true
|
||||
tasks:
|
||||
- name: Refuse a state the role does not have
|
||||
ansible.builtin.include_tasks: tasks/failed_run.yml
|
||||
vars:
|
||||
nfs_client_mounts:
|
||||
- {src: "nas.example:/export/data", path: /mnt/data, state: mounted}
|
||||
nfs_client_test_case:
|
||||
name: state-mounted
|
||||
expect: "value of state must be one of: present, absent, got: mounted"
|
||||
|
||||
- name: Refuse remounted
|
||||
ansible.builtin.include_tasks: tasks/failed_run.yml
|
||||
vars:
|
||||
nfs_client_mounts:
|
||||
- {src: "nas.example:/export/data", path: /mnt/data, state: remounted}
|
||||
nfs_client_test_case:
|
||||
name: state-remounted
|
||||
expect: "value of state must be one of: present, absent, got: remounted"
|
||||
|
||||
- name: Refuse an entry without a src
|
||||
ansible.builtin.include_tasks: tasks/failed_run.yml
|
||||
vars:
|
||||
nfs_client_mounts:
|
||||
- {path: /mnt/data}
|
||||
nfs_client_test_case:
|
||||
name: src-missing
|
||||
expect: "missing required arguments: src"
|
||||
|
||||
- name: Refuse an empty src
|
||||
ansible.builtin.include_tasks: tasks/failed_run.yml
|
||||
vars:
|
||||
nfs_client_mounts:
|
||||
- {src: "", path: /mnt/data}
|
||||
nfs_client_test_case:
|
||||
name: src-empty
|
||||
expect: nfs_client_mounts src must be one word of the form server:/export
|
||||
|
||||
- name: Refuse a src without an export
|
||||
ansible.builtin.include_tasks: tasks/failed_run.yml
|
||||
vars:
|
||||
nfs_client_mounts:
|
||||
- {src: nas.example, path: /mnt/data}
|
||||
nfs_client_test_case:
|
||||
name: src-no-export
|
||||
expect: nfs_client_mounts src must be one word of the form server:/export
|
||||
|
||||
- name: Refuse a src with a space after the export
|
||||
ansible.builtin.include_tasks: tasks/failed_run.yml
|
||||
vars:
|
||||
nfs_client_mounts:
|
||||
- {src: "nas.example:/export/my data", path: /mnt/data}
|
||||
nfs_client_test_case:
|
||||
name: src-space
|
||||
expect: nfs_client_mounts src must be one word of the form server:/export
|
||||
|
||||
- name: Refuse a mount point with a space
|
||||
ansible.builtin.include_tasks: tasks/failed_run.yml
|
||||
vars:
|
||||
nfs_client_mounts:
|
||||
- {src: "nas.example:/export/data", path: "/mnt/my data"}
|
||||
nfs_client_test_case:
|
||||
name: path-space
|
||||
expect: nfs_client_mounts path must be an absolute path
|
||||
|
||||
- name: Refuse a relative mount point
|
||||
ansible.builtin.include_tasks: tasks/failed_run.yml
|
||||
vars:
|
||||
nfs_client_mounts:
|
||||
- {src: "nas.example:/export/data", path: mnt/data}
|
||||
nfs_client_test_case:
|
||||
name: path-relative
|
||||
expect: nfs_client_mounts path must be an absolute path
|
||||
|
||||
- name: Refuse the root as a mount point
|
||||
ansible.builtin.include_tasks: tasks/failed_run.yml
|
||||
vars:
|
||||
nfs_client_mounts:
|
||||
- {src: "nas.example:/export/data", path: /}
|
||||
nfs_client_test_case:
|
||||
name: path-root
|
||||
expect: nfs_client_mounts path must be an absolute path
|
||||
|
||||
- name: Refuse a trailing slash
|
||||
ansible.builtin.include_tasks: tasks/failed_run.yml
|
||||
vars:
|
||||
nfs_client_mounts:
|
||||
- {src: "nas.example:/export/data", path: /mnt/data/}
|
||||
nfs_client_test_case:
|
||||
name: path-trailing-slash
|
||||
expect: nfs_client_mounts path must be an absolute path
|
||||
|
||||
- name: Refuse options with a space
|
||||
ansible.builtin.include_tasks: tasks/failed_run.yml
|
||||
vars:
|
||||
nfs_client_mounts:
|
||||
- {src: "nas.example:/export/data", path: /mnt/data, opts: "ro, nofail"}
|
||||
nfs_client_test_case:
|
||||
name: opts-space
|
||||
expect: nfs_client_mounts options must be a non-empty comma-separated word
|
||||
|
||||
- name: Refuse an entry with no options when the default is empty
|
||||
ansible.builtin.include_tasks: tasks/failed_run.yml
|
||||
vars:
|
||||
nfs_client_default_opts: ""
|
||||
nfs_client_mounts:
|
||||
- {src: "nas.example:/export/data", path: /mnt/data}
|
||||
nfs_client_test_case:
|
||||
name: opts-empty
|
||||
expect: nfs_client_mounts options must be a non-empty comma-separated word
|
||||
|
||||
- name: Refuse the same mount point twice
|
||||
ansible.builtin.include_tasks: tasks/failed_run.yml
|
||||
vars:
|
||||
nfs_client_mounts:
|
||||
- {src: "nas.example:/export/data", path: /mnt/data}
|
||||
- {src: "nas.example:/export/other", path: /mnt/data}
|
||||
nfs_client_test_case:
|
||||
name: duplicate-path
|
||||
expect: nfs_client_mounts lists the same path twice
|
||||
|
||||
- name: Verify the host
|
||||
hosts: all
|
||||
gather_facts: true
|
||||
tasks:
|
||||
- name: Read the installed packages
|
||||
ansible.builtin.package_facts:
|
||||
manager: apt
|
||||
|
||||
- name: The client package is installed
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- "'nfs-common' in ansible_facts.packages"
|
||||
quiet: true
|
||||
|
||||
- name: Read fstab
|
||||
ansible.builtin.slurp:
|
||||
src: /etc/fstab
|
||||
register: nfs_client_test_fstab
|
||||
|
||||
# Each line split into its six fields, so the assertions do not depend on the spacing the
|
||||
# module writes.
|
||||
- name: Split the fstab lines into fields
|
||||
ansible.builtin.set_fact:
|
||||
nfs_client_test_rows: >-
|
||||
{{ (nfs_client_test_fstab.content | b64decode).splitlines()
|
||||
| reject('match', '^#')
|
||||
| map('split')
|
||||
| reject('equalto', [])
|
||||
| list }}
|
||||
|
||||
# The seeded line for /mnt/data named another server and the default options: the role
|
||||
# replaces it, so exactly one line is left for the path.
|
||||
- name: The default-options entry replaced the seeded line
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- "['nas.example:/export/data', '/mnt/data', 'nfs', '_netdev,nofail', '0', '0'] in nfs_client_test_rows"
|
||||
- nfs_client_test_rows | selectattr('1', 'eq', '/mnt/data') | list | length == 1
|
||||
quiet: true
|
||||
|
||||
- name: The entry with its own options keeps them
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- "['nas.example:/export/media', '/mnt/media', 'nfs', '_netdev,nofail,ro,vers=4.2', '0', '0'] in nfs_client_test_rows"
|
||||
quiet: true
|
||||
|
||||
- name: The absent entry lost its line
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- nfs_client_test_rows | selectattr('1', 'eq', '/mnt/old') | list | length == 0
|
||||
quiet: true
|
||||
|
||||
- name: Stat the mount points
|
||||
ansible.builtin.stat:
|
||||
path: "{{ item }}"
|
||||
loop: [/mnt/data, /mnt/media, /mnt/old, /mnt/old/keep]
|
||||
register: nfs_client_test_stat
|
||||
|
||||
# prepare.yml gave /mnt/media an owner and a mode and /mnt/old a mode of its own. The role
|
||||
# created /mnt/data (a new directory is root's) and changed neither of the others, and the
|
||||
# removed entry's directory and the file in it are untouched.
|
||||
- name: The mount points exist, keep their owner and mode, and the removed entry's directory is intact
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- nfs_client_test_stat.results | map(attribute='stat.exists') | min
|
||||
- nfs_client_test_stat.results[0].stat.isdir
|
||||
- nfs_client_test_stat.results[0].stat.pw_name == 'root'
|
||||
- nfs_client_test_stat.results[1].stat.pw_name == 'daemon'
|
||||
- nfs_client_test_stat.results[1].stat.mode == '0710'
|
||||
- nfs_client_test_stat.results[2].stat.mode == '0750'
|
||||
quiet: true
|
||||
|
||||
- name: Read what is mounted
|
||||
ansible.builtin.slurp:
|
||||
src: /proc/mounts
|
||||
register: nfs_client_test_mounts
|
||||
|
||||
- name: Nothing was mounted
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- "'/mnt/data' not in (nfs_client_test_mounts.content | b64decode)"
|
||||
- "'/mnt/media' not in (nfs_client_test_mounts.content | b64decode)"
|
||||
quiet: true
|
||||
|
||||
# The daemon-reload is what turns an fstab line into a unit; the generator ran again, so the
|
||||
# new lines have a unit and the removed one has none. systemctl and not the systemd module,
|
||||
# which fails on a unit that is not loaded, the very state asserted for the removed entry.
|
||||
- name: Ask systemd about the mount units # noqa: command-instead-of-module
|
||||
ansible.builtin.command: "systemctl show --property=LoadState --value {{ item }}"
|
||||
loop: [mnt-data.mount, mnt-media.mount, mnt-old.mount]
|
||||
register: nfs_client_test_units
|
||||
changed_when: false
|
||||
check_mode: false
|
||||
|
||||
- name: Systemd knows the new entries and not the removed one
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- nfs_client_test_units.results[0].stdout == 'loaded'
|
||||
- nfs_client_test_units.results[1].stdout == 'loaded'
|
||||
- nfs_client_test_units.results[2].stdout == 'not-found'
|
||||
quiet: true
|
||||
@@ -0,0 +1,48 @@
|
||||
---
|
||||
# Validation first, under always, so a bad input fails before any change whatever tags the run
|
||||
# selects.
|
||||
- name: Validate the role inputs
|
||||
ansible.builtin.import_tasks: validate.yml
|
||||
tags: always
|
||||
|
||||
- name: Install the NFS client package
|
||||
become: true
|
||||
ansible.builtin.apt:
|
||||
name: "{{ __nfs_client_package }}"
|
||||
state: present
|
||||
update_cache: true
|
||||
cache_valid_time: 3600
|
||||
tags: [nfs_client, install]
|
||||
|
||||
# No owner, group or mode: once a share is mounted the path is the export root, and a chown or
|
||||
# chmod there changes the permissions on the NFS server.
|
||||
- name: Create the mount points # noqa: risky-file-permissions
|
||||
become: true
|
||||
ansible.builtin.file:
|
||||
path: "{{ item.path }}"
|
||||
state: directory
|
||||
loop: "{{ __nfs_client_entries | selectattr('state', 'eq', 'present') | list }}"
|
||||
loop_control:
|
||||
label: "{{ item.path }}"
|
||||
tags: [nfs_client, configure]
|
||||
|
||||
# present writes the fstab line and mounts nothing. An absent entry only loses its line:
|
||||
# ansible.posix.mount's own absent would also unmount the share and delete the mount point.
|
||||
- name: Manage the fstab entries
|
||||
become: true
|
||||
ansible.posix.mount:
|
||||
src: "{{ item.src }}"
|
||||
path: "{{ item.path }}"
|
||||
fstype: nfs
|
||||
opts: "{{ item.opts }}"
|
||||
dump: "0"
|
||||
passno: "0"
|
||||
state: "{{ 'present' if item.state == 'present' else 'absent_from_fstab' }}"
|
||||
loop: "{{ __nfs_client_entries }}"
|
||||
loop_control:
|
||||
label: "{{ item.path }}"
|
||||
notify: Reload systemd for the nfs_client fstab entries
|
||||
tags: [nfs_client, configure]
|
||||
|
||||
- name: Flush handlers
|
||||
ansible.builtin.meta: flush_handlers
|
||||
@@ -0,0 +1,73 @@
|
||||
---
|
||||
# Input checks. Imported first in main.yml with tags: always, so a bad input fails before any
|
||||
# change. The argument spec (types, choices, required keys) runs before this file; the rules
|
||||
# below are the ones between values. Each assert covers the whole list, not one item per loop
|
||||
# pass, so the failure message is the assert's own. fstab fields are split on whitespace, so
|
||||
# "one word" is tested with split rather than a pattern.
|
||||
|
||||
# The package and the fstab format are Debian's, and the role reads gathered facts.
|
||||
- name: Require a Debian-family host
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- ansible_facts['os_family'] | default('') == 'Debian'
|
||||
fail_msg: >-
|
||||
nfs_client supports Debian-family hosts only (found: {{ ansible_facts['os_family'] | default('no gathered facts') }}).
|
||||
The role reads gathered facts, so run it in a play that gathers them.
|
||||
quiet: true
|
||||
|
||||
# Fact-computing tasks that the checks below read. A statement block in one template would leave
|
||||
# whitespace around the result and make it a string instead of a list, so the list is built by
|
||||
# appending one entry per pass.
|
||||
- name: Start the resolved entries
|
||||
ansible.builtin.set_fact:
|
||||
__nfs_client_entries: []
|
||||
|
||||
# Defaults of the suboptions are applied here: the spec's suboption defaults are documentation.
|
||||
- name: Resolve the entries
|
||||
ansible.builtin.set_fact:
|
||||
__nfs_client_entries: >-
|
||||
{{ __nfs_client_entries + [{
|
||||
'src': item.src | trim,
|
||||
'path': item.path,
|
||||
'opts': (item.opts | default('', true) | trim) or (nfs_client_default_opts | trim),
|
||||
'state': item.state | default('present', true)
|
||||
}] }}
|
||||
loop: "{{ nfs_client_mounts }}"
|
||||
loop_control:
|
||||
label: "{{ item.path }}"
|
||||
|
||||
- name: Require a server and an export in every src
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- __nfs_client_entries | map(attribute='src') | map('split') | map('length') | reject('eq', 1) | list | length == 0
|
||||
- __nfs_client_entries | map(attribute='src') | reject('search', '^[^ ]+:/') | list | length == 0
|
||||
fail_msg: >-
|
||||
nfs_client_mounts src must be one word of the form server:/export, without spaces
|
||||
(offending: {{ __nfs_client_entries | map(attribute='src') | reject('search', '^[^ ]+:/') | list }}).
|
||||
quiet: true
|
||||
|
||||
- name: Require an absolute mount point that is not the root
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- __nfs_client_entries | map(attribute='path') | map('split') | map('length') | reject('eq', 1) | list | length == 0
|
||||
- __nfs_client_entries | map(attribute='path') | reject('search', '^/[^ ]*[^/ ]$') | list | length == 0
|
||||
fail_msg: >-
|
||||
nfs_client_mounts path must be an absolute path without spaces, not the root and without a trailing slash
|
||||
(offending: {{ __nfs_client_entries | map(attribute='path') | reject('search', '^/[^ ]*[^/ ]$') | list }}).
|
||||
quiet: true
|
||||
|
||||
- name: Require options that fit one fstab field
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- __nfs_client_entries | map(attribute='opts') | map('split') | map('length') | reject('eq', 1) | list | length == 0
|
||||
fail_msg: >-
|
||||
nfs_client_mounts options must be a non-empty comma-separated word without spaces, from the entry's opts or, when
|
||||
that is empty, from nfs_client_default_opts.
|
||||
quiet: true
|
||||
|
||||
- name: Require one entry per mount point
|
||||
ansible.builtin.assert:
|
||||
that:
|
||||
- __nfs_client_entries | map(attribute='path') | unique | list | length == __nfs_client_entries | length
|
||||
fail_msg: nfs_client_mounts lists the same path twice; each mount point may appear once.
|
||||
quiet: true
|
||||
@@ -0,0 +1,3 @@
|
||||
---
|
||||
# Constant, not an input: the package that carries mount.nfs on Debian and Ubuntu.
|
||||
__nfs_client_package: nfs-common
|
||||
Reference in new issue
Block a user