Ansible on RHEL 10: autofs over NFS and Time Sync with chrony

The seventh lab in the Ansible series: one playbook sets the time zone on both servers, makes server1 the time server for the network with the RHEL timesync role, exports two NFS directories from server1, and mounts them on demand on server2 with autofs. Includes autofs failing where a manual mount works, time sync that worked on the first run and then quietly stopped, a time server that won't serve without an upstream, root that can't write over NFS, and a user who lost access because of a different UID.

Goal: network shares and the same time on server1 and server2, kept in one playbook:

  • both servers use the same time zone
  • server1 is the time server for the network. It has no upstream, because the network is offline
  • server2 takes its time from server1
  • server1 exports /exports/projects (read-write) and /exports/docs (read-only) over NFS
  • on server2, /shares/projects and /shares/docs are mounted by autofs when someone uses them, and unmounted again when nobody does

These are the autofs and time tasks from my RHCSA practice exercises.

Starting point: the setup from the earlier labs in this series: server1 is the control node, the project is in ~/ansible, both servers install packages from the ISO repositories, and rhel-system-roles is installed from lab 2.

Lab environment:

HostIP addressRole
server1192.168.10.10control node + managed node, NFS server, time server
server2192.168.10.11managed node, NFS client

The addresses are examples. Replace them with your own.

Tested on: CentOS Stream 10 containers with systemd (ansible-core 2.16, rhel-system-roles 2.4, chrony 4, autofs 5). Every output below comes from that test. Restarting the server2 container counted as the reboot test.

1. Which tool for what

TaskToolIn ansible-core?
time zoneansible.builtin.file (symlink)yes
chrony, client and serverredhat.rhel_system_roles.timesyncno, but on the ISO
NFS exports, autofs mapsansible.builtin.copyyes
exportfs, servicesansible.builtin.command, systemd_serviceyes
firewallredhat.rhel_system_roles.firewallno, but on the ISO

community.general.timezone and ansible.posix.mount aren’t available offline. autofs doesn’t need mount anyway: it mounts on its own.

The time zone is a symlink. timedatectl set-timezone does nothing else than point /etc/localtime to a file in /usr/share/zoneinfo. The file module does the same, and on the next run it sees that the link is already right.

2. Variables

Values that both servers share go in group_vars/servers.yml:

timezone: Europe/Berlin
lab_network: 192.168.10.0/24
nfs_server: 192.168.10.10
ntp_server: 192.168.10.10

The timesync role is configured per host, because server1 and server2 have different jobs.

host_vars/server1.yml:

timesync_ntp_servers: []
timesync_chrony_custom_settings:
  - "local stratum 10"
  - "allow {{ lab_network }}"

host_vars/server2.yml:

timesync_ntp_servers:
  - hostname: "{{ ntp_server }}"
    iburst: true
  • timesync_ntp_servers: [] - server1 has no upstream. On a network with internet access, you’d list public servers here.
  • local stratum 10 lets server1 serve its own clock as the reference when it has no upstream. Section 6 shows what happens without it.
  • allow lets clients from the network ask server1 for the time. By default, chrony answers no one.
  • iburst sends a few quick requests at start, so server2 is in sync within seconds instead of minutes.

3. The playbook

cd ~/ansible
vim autofs-time.yml
---
- name: Time zone and time sync on every server
  hosts: servers
  become: true
  tasks:
    - name: Time zone is set
      ansible.builtin.file:
        src: "/usr/share/zoneinfo/{{ timezone }}"
        dest: /etc/localtime
        state: link

    - name: chrony is configured
      ansible.builtin.include_role:
        name: redhat.rhel_system_roles.timesync

- name: NFS exports on server1
  hosts: server1
  become: true
  tasks:
    - name: nfs-utils is installed
      ansible.builtin.dnf:
        name: nfs-utils
        state: present

    - name: Export directories exist
      ansible.builtin.file:
        path: "/exports/{{ item }}"
        state: directory
        mode: "0755"
      loop:
        - projects
        - docs

    - name: Exports file
      ansible.builtin.copy:
        dest: /etc/exports.d/lab.exports
        content: |
          /exports/projects {{ lab_network }}(rw,sync)
          /exports/docs     {{ lab_network }}(ro,sync)
        mode: "0644"
      notify: Reload exports

    - name: NFS server is running
      ansible.builtin.systemd_service:
        name: nfs-server
        state: started
        enabled: true

    - name: NFS and NTP are allowed in the firewall
      ansible.builtin.include_role:
        name: redhat.rhel_system_roles.firewall
      vars:
        firewall:
          - service: nfs
            state: enabled
          - service: ntp
            state: enabled

  handlers:
    - name: Reload exports
      ansible.builtin.command: exportfs -ra

- name: Automount on server2
  hosts: server2
  become: true
  tasks:
    - name: autofs and nfs-utils are installed
      ansible.builtin.dnf:
        name:
          - autofs
          - nfs-utils
        state: present

    - name: Master map entry for /shares
      ansible.builtin.copy:
        dest: /etc/auto.master.d/shares.autofs
        content: |
          /shares  /etc/auto.shares
        mode: "0644"
      notify: Reload autofs

    - name: Map for /shares
      ansible.builtin.copy:
        dest: /etc/auto.shares
        content: |
          *  -fstype=nfs4,rw,sync  {{ nfs_server }}:/exports/&
        mode: "0644"
      notify: Reload autofs

    - name: autofs is running
      ansible.builtin.systemd_service:
        name: autofs
        state: started
        enabled: true

  handlers:
    - name: Reload autofs
      ansible.builtin.systemd_service:
        name: autofs
        state: reloaded

There are three plays, one per job. Each play only runs on the hosts that need it.

How to read the NFS and autofs parts:

  • /etc/exports.d/lab.exports - NFS reads every .exports file in that directory as well as /etc/exports. A separate file belongs to this playbook alone, and nothing else edits it.
  • exportfs -ra loads the changed exports without restarting the NFS server. It’s a handler, so it only runs when the file changed.
  • /etc/auto.master.d/shares.autofs - the same idea for autofs. The line means: the directory /shares is managed by autofs, and the map /etc/auto.shares says what goes in it.
  • * and & in the map - a wildcard. When someone opens /shares/projects, * matches projects, and & is replaced with the same word. So /shares/projects mounts server1:/exports/projects, and a new export on server1 works on server2 without changing the map.
  • -fstype=nfs4 - section 4 explains why it’s needed.
  • ro for docs is set on the server, in the exports file. The rw in the map only asks for write access. The server decides.

4. Run it

ansible-playbook --syntax-check autofs-time.yml
ansible-playbook autofs-time.yml

The second run:

PLAY RECAP *********************************************************************
server1  : ok=47   changed=0    unreachable=0    failed=0    skipped=48 ...
server2  : ok=31   changed=0    unreachable=0    failed=0    skipped=36 ...

On server2, /shares is empty until someone uses it:

ansible server2 -a "ls /shares"
ansible server2 -a "ls -ld /shares/projects /shares/docs"
ansible server2 -a "findmnt -t nfs4 -no TARGET,SOURCE"
(empty)
drwxr-xr-x. 2 root root 6 Nov 23 17:04 /shares/docs
drwxr-xr-x. 2 root root 6 Nov 23 17:04 /shares/projects
/shares/projects 192.168.10.10:/exports/projects
/shares/docs     192.168.10.10:/exports/docs

ls /shares doesn’t show the shares, because with a wildcard map autofs can’t know the names in advance. It only knows a name when someone asks for it. After 5 minutes without use, autofs unmounts them again.

5. autofs failed, the manual mount worked

The first version of the map had no -fstype=nfs4:

*  -rw,sync  {{ nfs_server }}:/exports/&

/shares/projects didn’t exist:

ls: cannot access '/shares/projects': No such file or directory

A manual mount of the same export worked:

mount -t nfs server1:/exports/projects /mnt

With logging = debug in /etc/autofs.conf, the autofs log showed why:

get_nfs_info: called with host server1(192.168.10.10) proto 6 version 0x60
get_portmap_client: host not reachable getting portmap client
mount(nfs): no hosts available

Before mounting, autofs checks that the server is reachable and which NFS versions it offers. Without a version given, it asks rpcbind on port 111. The firewall on server1 only allows the nfs service (port 2049), so autofs got no answer and decided the server was down. mount doesn’t check first, so it went straight to NFSv4 on port 2049 and worked.

With -fstype=nfs4, autofs checks NFSv4 on port 2049 only:

get_nfs_info: nfs v4 rpc ping time: 0.000102
prune_host_list: selected subset of hosts that support NFS4 over TCP

showmount -e server1 fails for the same reason:

clnt_create: RPC: Unable to receive

showmount needs rpc-bind and mountd on the server, which NFSv4 doesn’t use. You could open them in the firewall, but then you have two more open ports just for a listing. A failing showmount doesn’t mean NFS is broken.

6. Time sync that stopped on its own

The first version of the firewall task only had nfs. Right after the first run, server2 showed:

MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* server1                      10   6   170   308  +3860ns[+6689ns] +/- 8567ns

^* means server2 is synchronised to server1, so it looked fine. But the time play runs before the NFS play, so server2 synced first, and only after that did the firewall role start firewalld on server1. A few minutes later:

^* server1                      10   6   360   332  +3860ns[+6689ns] +/- 8567ns

Reach is an octal number showing the last 8 requests, one bit each. 170 and then 360 means the last requests got no answer, and LastRx (seconds since the last answer) kept growing. chrony keeps the ^* for a while with the last good value, so nothing looks broken. A test request that doesn’t change the clock confirmed it:

chronyd -Q "server server1 iburst maxsamples 1"
No suitable source for synchronisation

NTP uses UDP port 123, which the firewall now blocked. With ntp added to the firewall list, the same command answered:

System clock wrong by -0.000028 seconds (ignored)

7. A time server that won’t serve

To see what local stratum 10 is for, I removed it from host_vars/server1.yml and ran the playbook. On server2:

^? server1                       0   6     0     -     +0ns[   +0ns] +/-    0ns
No suitable source for synchronisation

^? means the source can’t be used. server1 had no upstream and was itself not synchronised, so it told its clients that its time can’t be trusted. local stratum 10 says: use the local clock as the reference, at stratum 10. Stratum is the distance from a real clock source. 10 is far enough that if a client ever gets a better source, it will prefer that one. With the line back, server2 was in sync again within seconds:

^* server1                      10   6     7     0  +7079ns[  +25us] +/-   31us

8. Who can write to the share

projects is exported rw, but root on server2 couldn’t write to it:

ansible server2 -b -a "touch /shares/projects/root-test"
touch: cannot touch '/shares/projects/root-test': Permission denied

NFS turns root from a client into nobody on the server (root_squash, the default). Otherwise root on any client would be root on the server’s files. And /exports/projects is owned by root with mode 755, so nobody can’t write.

For a regular user to write, the server checks the numeric UID, not the name. I made /exports/projects on server1 owned by alice with UID 1100, and created alice on server2 with the same UID:

-rw-r--r--. 1 1100 1100 0 Nov 23 17:10 alice-file

Then I recreated alice on server2 with UID 1101:

touch: cannot touch '/shares/projects/alice2': Permission denied

Same name, different number, no access. Users who work with NFS shares need the same UID on every server. In lab 4, the users are created without a fixed UID, so they get the same number only if they were created in the same order on both servers. For shares, set uid: in the user task.

docs refuses everyone, root included, because the server exported it ro:

touch: cannot touch '/shares/docs/x': Read-only file system

Verification

From ~/ansible on server1:

ansible all -a "timedatectl show -p Timezone"              # Timezone=Europe/Berlin
ansible server2 -a "chronyc sources"                       # ^* 192.168.10.10, Reach growing to 377
ansible server1 -b -a "exportfs -v"                        # projects rw, docs ro
ansible server1 -b -a "firewall-cmd --list-services"       # ... nfs ntp ...
ansible server2 -a "ls /shares/projects /shares/docs"      # mounts on access
ansible server2 -a "findmnt -t nfs4"                       # both shares, type nfs4
ansible-playbook autofs-time.yml                           # changed=0 on both

After a restart of server2, /shares/projects mounted again on first use and chronyc sources showed ^* for server1 within a minute.

Safety notes

  • Use -fstype=nfs4 in autofs maps when the firewall only allows nfs. Without it, autofs reports the server as unreachable.
  • Check Reach and LastRx, not only ^*. chrony keeps showing a source as selected for a while after it stops answering.
  • Open ntp in the firewall on the time server. The time play can pass before the firewall is even running.
  • Keep local stratum only on a server with no upstream. On a server with internet access, real sources are better than its own clock.
  • Don’t export with no_root_squash unless you have to. It makes root on every client root on the exported files.
  • Give NFS users the same UID on every server. The server checks numbers, not names.
  • A wrong time zone name fails safely. With Europe/Berlinn, the file task stopped with src file does not exist and left /etc/localtime as it was.