diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fde75209..feeb28258 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * **role:sshd, playbook:setup_basic**: sshd turns new connections away earlier while many logins are pending (`MaxStartups 10:30:60` instead of the OpenSSH default `10:30:100`), as the CIS Benchmarks require, and `MaxStartups` is configurable with `sshd__max_startups__host_var` / `sshd__max_startups__group_var`. A `MaxStartups` set through `sshd__raw` no longer takes effect, move it to the variable. Hosts with many simultaneous SSH logins need a higher value, for example `'100:30:200'` on GitLab hosts ([#723](https://github.com/Linuxfabrik/lfops/issues/723)). * **role:grav, playbook:setup_grav**: The code of Grav, its configuration, pages, plugins and themes belong to `root`, so a hole in Grav or a plugin can no longer plant code in the site. The web server only writes `assets`, `backup`, `cache`, `images`, `logs`, `tmp`, `user/accounts` and `user/data`, and only these keep the SELinux type `httpd_sys_rw_content_t`. Grav, its plugins and themes are updated as `root` by `grav-selfupgrade-.timer` and `grav-update-.timer`, which replace `grav-selfupgrade.timer` and `grav-update.timer`, so several Grav sites on one host are each updated. Run other `bin/gpm` commands with `grav-gpm ` instead of `sudo -u apache bin/gpm`, which now fails, and deploy content below `user/` as `root`. The role no longer installs the Admin Panel, which could not save anything anymore, and drops `grav__skip_admin` and `grav__users`; it lists the Admin Panel plugins still installed at the end of the play, for you to remove. The vHost no longer accepts `POST`, and `setup_grav` no longer sets the SELinux boolean `httpd_can_network_connect`, which keeps its current value. * **role:apache_httpd**: The `proxy` vHost template waits 30 instead of 5 seconds for the backend, so slower responses no longer end in a 502, and a hanging backend holds the connection longer. Set `conf_proxy_timeout: 5` on the vHost to keep the previous value. +* **role:hostname**: The hostname resolves to the host's IP address instead of a loopback address that cloud-init or the Debian installer mapped it to in `/etc/hosts` (Lynis NAME-4406). Clients that reach a local service by the host name no longer connect if the service only listens on `127.0.0.1` or, like a MariaDB account `user@localhost`, only accepts local connections. Point them to `localhost` or `127.0.0.1` before updating. * **role:icingaweb2, playbook:setup_icinga2_master**: IcingaWeb2 accepts selections of up to 500 objects and has 5 minutes for long requests such as PDF exports. A reverse proxy in front of it needs `apache_httpd__conf_limit_request_field_size` and `apache_httpd__conf_limit_request_line` set to `131072` as well. * **role:nextcloud**: On an existing installation, the role no longer resets apps and app settings that the Nextcloud admin changed in the web interface, such as an app the role disables by default. It applies its full list of apps and app settings to a fresh installation only, and afterwards keeps the entries of `nextcloud__apps__*_var` and `nextcloud__app_configs__*_var` plus the apps and settings it relies on, such as `admin_audit`, `password_policy` and `twofactor_totp`. To keep an app or setting under Ansible control, add it to `nextcloud__apps__host_var` / `nextcloud__app_configs__host_var`. * **role:system_update**: Hosts install all updates daily instead of every Tuesday, Rocky hosts check for security hot-fixes four times a day instead of once, and pending updates are no longer announced by mail the day before. Mails for applied updates, reboots and failures stay. To keep weekly updates with the announcement, set `system_update__update_day: 'Tue'` and `system_update__notify_and_schedule_enabled: true`; to keep the single daily security run, set `system_update__security_on_calendar`. diff --git a/extensions/molecule/hostname/converge.yml b/extensions/molecule/hostname/converge.yml new file mode 100644 index 000000000..67b1e125e --- /dev/null +++ b/extensions/molecule/hostname/converge.yml @@ -0,0 +1,2 @@ +- name: 'Converge hostname playbook' + ansible.builtin.import_playbook: 'linuxfabrik.lfops.hostname' diff --git a/extensions/molecule/hostname/inventory/group_vars/systems_under_test.yml b/extensions/molecule/hostname/inventory/group_vars/systems_under_test.yml new file mode 100644 index 000000000..671050e4c --- /dev/null +++ b/extensions/molecule/hostname/inventory/group_vars/systems_under_test.yml @@ -0,0 +1,2 @@ +# A domain part, so that the FQDN and the short name differ and both have to be handled. +hostname__domain_part: 'example.com' diff --git a/extensions/molecule/hostname/inventory/hosts.yml b/extensions/molecule/hostname/inventory/hosts.yml new file mode 100644 index 000000000..1a27e18dd --- /dev/null +++ b/extensions/molecule/hostname/inventory/hosts.yml @@ -0,0 +1,15 @@ +# yamllint disable rule:empty-values +lfops_hostname: + children: + systems_under_test: + +systems_under_test: + hosts: + debian12-vm: + debian13-vm: + rocky8-vm: + rocky9-vm: + rocky10-vm: + ubuntu2204-vm: + ubuntu2404-vm: + ubuntu2604-vm: diff --git a/extensions/molecule/hostname/molecule.yml b/extensions/molecule/hostname/molecule.yml new file mode 100644 index 000000000..4dbd00e7f --- /dev/null +++ b/extensions/molecule/hostname/molecule.yml @@ -0,0 +1,10 @@ +# Molecule scenario marker for the 'hostname' scenario, run with +# `molecule test --scenario-name hostname`. +# +# The prepare playbook is overridden to map the hostname to the loopback addresses the way +# cloud-init and the Debian installer do, which the hostname role cleans up; see the +# scenario-local prepare.yml. It imports the shared vm-prepare.yml first, so the systems under +# test are still waited for and their facts still gathered. +provisioner: + playbooks: + prepare: '${MOLECULE_SCENARIO_DIRECTORY}/prepare.yml' diff --git a/extensions/molecule/hostname/prepare.yml b/extensions/molecule/hostname/prepare.yml new file mode 100644 index 000000000..5c14fe035 --- /dev/null +++ b/extensions/molecule/hostname/prepare.yml @@ -0,0 +1,41 @@ +# Wait for the systems under test and gather their facts, exactly as every other scenario does. +- name: 'Prepare VMs for Ansible' + ansible.builtin.import_playbook: '../playbooks/vm-prepare.yml' + + +# Map the hostname to the loopback addresses ahead of the localhost entries, as cloud-init does +# with manage_etc_hosts (templates/hosts.redhat.tmpl: 127.0.0.1 and ::1, Hetzner images) and as +# the Debian installer and templates/hosts.debian.tmpl do (127.0.1.1). The test images do not get +# these lines from cloud-init, so without this the role would have nothing to clean up and +# verify.yml would pass on a host that was never in that state. The last line maps a name that +# only starts with the hostname and has to survive. +- name: 'Map the hostname to the loopback addresses' + hosts: 'systems_under_test' + + vars: + __molecule__fqdn: '{{ inventory_hostname ~ "." ~ hostname__domain_part }}' + + tasks: + + - name: 'Add the loopback entries to /etc/hosts' + ansible.builtin.lineinfile: + path: '/etc/hosts' + line: '{{ item }}' + insertbefore: 'BOF' + loop: + - '127.0.0.1 {{ __molecule__fqdn }} {{ inventory_hostname }}' + - '::1 {{ __molecule__fqdn }} {{ inventory_hostname }}' + # The Debian installer takes the domain from DHCP, which may differ from the inventory. + - "127.0.1.1\t{{ inventory_hostname }}.example\t{{ inventory_hostname }} # Debian installer" + - '127.0.0.2 {{ inventory_hostname }}-alias' + + - name: 'getent ahosts {{ inventory_hostname }}' + ansible.builtin.command: 'getent ahosts {{ inventory_hostname }}' + changed_when: false + register: '__molecule__getent_result' + + - name: 'Assert that the hostname resolves to a loopback address before the role runs' + ansible.builtin.assert: + that: + - '__molecule__getent_result["stdout_lines"][0] is match("(127[.]|::1)")' + fail_msg: '{{ __molecule__getent_result["stdout"] }}' diff --git a/extensions/molecule/hostname/verify.yml b/extensions/molecule/hostname/verify.yml new file mode 100644 index 000000000..024830979 --- /dev/null +++ b/extensions/molecule/hostname/verify.yml @@ -0,0 +1,63 @@ +- name: 'Verify the hostname and its local name resolution' + hosts: 'systems_under_test' + gather_facts: false + + vars: + __molecule__fqdn: '{{ inventory_hostname ~ "." ~ hostname__domain_part }}' + + tasks: + + - name: 'Gather the network facts' + ansible.builtin.setup: + gather_subset: + - '!all' + - 'network' + + - name: 'hostname --fqdn' + ansible.builtin.command: 'hostname --fqdn' + changed_when: false + register: '__molecule__hostname_result' + + - name: 'Assert that the system uses the configured FQDN' + ansible.builtin.assert: + that: + - '__molecule__hostname_result["stdout"] == __molecule__fqdn' + fail_msg: '{{ __molecule__hostname_result["stdout"] }}' + + # What NAME-4406 is about: the FQDN and the short name resolve to the IP of the interface + # only, no longer to a loopback address that cloud-init or the installer mapped them to. + - name: 'getent ahosts and ' + ansible.builtin.command: 'getent ahosts {{ item }}' + changed_when: false + register: '__molecule__getent_result' + loop: + - '{{ __molecule__fqdn }}' + - '{{ inventory_hostname }}' + + - name: 'Assert that the hostname resolves to {{ ansible_facts["default_ipv4"]["address"] }} only' + ansible.builtin.assert: + that: + - 'item["stdout_lines"] | map("split") | map("first") | unique == [ansible_facts["default_ipv4"]["address"]]' + fail_msg: '{{ item["stdout"] }}' + loop: '{{ __molecule__getent_result["results"] }}' + loop_control: + label: '{{ item["item"] }}' + + # The role must not take away what others need: localhost keeps its loopback address, and + # a loopback line for a name that only starts with the hostname (see prepare.yml) stays. + - name: 'getent ahostsv4 localhost and -alias' + ansible.builtin.command: 'getent ahostsv4 {{ item }}' + changed_when: false + register: '__molecule__getent_loopback_result' + loop: + - 'localhost' + - '{{ inventory_hostname }}-alias' + + - name: 'Assert that localhost and -alias still resolve to a loopback address' + ansible.builtin.assert: + that: + - 'item["stdout_lines"][0] is match("127[.]")' + fail_msg: '{{ item["stdout"] }}' + loop: '{{ __molecule__getent_loopback_result["results"] }}' + loop_control: + label: '{{ item["item"] }}' diff --git a/roles/hostname/README.md b/roles/hostname/README.md index da05d717e..bcc7d3a06 100644 --- a/roles/hostname/README.md +++ b/roles/hostname/README.md @@ -8,9 +8,16 @@ This role sets the system hostname (the name returned by `hostname` / `hostnamec ## How the Role Behaves -The role adds a marker block to `/etc/hosts` that maps the FQDN (and the short name) to `hostname__etc_hosts_ip` (the host's primary IPv4 address by default). It does not template the whole file, because `/etc/hosts` is co-managed by cloud-init, which maintains the localhost entries. +The role adds a marker block to `/etc/hosts` that maps the FQDN (and the short name) to `hostname__etc_hosts_ip` (the host's primary IPv4 address by default). It does not template the whole file, so entries written by others stay. -If cloud-init is configured with `manage_etc_hosts: true`, it rewrites `/etc/hosts` on every boot and would drop this block. On the standard cloud images used with LFOps cloud-init only manages the localhost line, so the block persists. Set `hostname__manage_etc_hosts: false` to leave `/etc/hosts` untouched. +cloud-init (with `manage_etc_hosts: true`) and the Debian installer also map the hostname to a loopback address (`127.0.0.1`, `127.0.1.1` or `::1`). Since glibc returns the `/etc/hosts` entries in file order, the hostname would resolve to the loopback address, and Lynis reports this as NAME-4406. The role therefore removes loopback lines that carry nothing but the short name, alone or with any domain (`myhost`, `myhost.example`, `myhost.example.com`). A loopback line that lists other names as well, such as `127.0.0.1 localhost myhost`, is left untouched. + +Set `hostname__manage_etc_hosts: false` to leave `/etc/hosts` untouched. + + +## Known Limitations + +* As long as cloud-init is installed and `manage_etc_hosts` is `true`, cloud-init may render `/etc/hosts` from its template again at boot (on every boot with the upstream default, once per instance on Hetzner images). This drops the marker block and maps the hostname to the loopback address again, until the next run of the role. `setup_basic` removes cloud-init with the `cloud_init` role. To keep cloud-init, set `manage_etc_hosts: false` in the user-data. A file in `/etc/cloud/cloud.cfg.d/` does not help, since the vendor-data and user-data of the provider take precedence over it. ## Tags @@ -49,7 +56,7 @@ If cloud-init is configured with `manage_etc_hosts: true`, it rewrites `/etc/hos `hostname__manage_etc_hosts` -* Whether to maintain the `/etc/hosts` entry that maps the hostname to `hostname__etc_hosts_ip`. Set to `false` to leave `/etc/hosts` untouched. +* Whether to maintain the `/etc/hosts` entry that maps the hostname to `hostname__etc_hosts_ip`, and to remove the loopback entries that carry nothing but the hostname. Set to `false` to leave `/etc/hosts` untouched. * Type: Bool. * Default: `true` diff --git a/roles/hostname/meta/argument_specs.yml b/roles/hostname/meta/argument_specs.yml index 4cb139c7c..805d915bf 100644 --- a/roles/hostname/meta/argument_specs.yml +++ b/roles/hostname/meta/argument_specs.yml @@ -38,5 +38,6 @@ argument_specs: default: true description: >- Whether to maintain an `/etc/hosts` entry mapping the hostname - to `hostname__etc_hosts_ip`. Set to `false` to leave `/etc/hosts` - untouched. + to `hostname__etc_hosts_ip`, and to remove the loopback entries + that carry nothing but the hostname. Set to `false` to leave + `/etc/hosts` untouched. diff --git a/roles/hostname/tasks/main.yml b/roles/hostname/tasks/main.yml index 061dcc323..a92ba93da 100644 --- a/roles/hostname/tasks/main.yml +++ b/roles/hostname/tasks/main.yml @@ -4,10 +4,9 @@ ansible.builtin.hostname: name: '{{ hostname__full_hostname }}' - # /etc/hosts is co-managed by cloud-init (the localhost entries), so we manage - # only our own marker block here instead of templating the whole file. Mapping - # the FQDN (and short name) to the real interface IP gives proper local name - # resolution and avoids mapping the hostname to a loopback address. + # Only our own marker block instead of templating the whole file, so entries written by others + # (cloud-init, the installer, the admin) stay. Mapping the FQDN (and short name) to the real + # interface IP gives proper local name resolution. - name: 'Map {{ hostname__full_hostname }} to {{ hostname__etc_hosts_ip }} in /etc/hosts' ansible.builtin.blockinfile: path: '/etc/hosts' @@ -17,5 +16,24 @@ - 'hostname__manage_etc_hosts | bool' - 'hostname__etc_hosts_ip | length > 0' + # cloud-init (manage_etc_hosts) and the Debian installer map the hostname to 127.0.0.1, + # 127.0.1.1 or ::1 as well. glibc returns the /etc/hosts lines in file order, so the hostname + # resolves to the loopback address instead of the IP mapped above, and Lynis reports NAME-4406. + # Only lines that carry nothing but the short name, alone or with any domain, are removed. The + # domain may differ from ours, e.g. the Debian installer writes "127.0.1.1 debian13.network + # debian13" on Debian 13 with the domain from DHCP. A loopback line with other names stays. + # Verified against cloud-init 26.2 (templates/hosts.*.tmpl, cc_update_etc_hosts.py), Hetzner + # Rocky 9 images, Debian 13 and Rocky 9.4 with Lynis 3.1.7. + - name: 'Remove the loopback entries of {{ hostname__full_hostname }} from /etc/hosts' + ansible.builtin.lineinfile: + path: '/etc/hosts' + regexp: '^\s*(127(\.[0-9]{1,3}){3}|::1)(\s+{{ __hostname__short_name }}(\.[^\s#]+)?)+\s*(#.*)?$' + state: 'absent' + vars: + __hostname__short_name: '{{ hostname__full_hostname.split(".")[0] | regex_escape }}' + when: + - 'hostname__manage_etc_hosts | bool' + - 'hostname__etc_hosts_ip | length > 0' + tags: - 'hostname'