feat(ntp): add role and playbook

This commit is contained in:
2025-12-30 22:13:13 +01:00
parent f765345da3
commit 3e43cca13b
6 changed files with 65 additions and 0 deletions

7
playbooks/ntp.yml Normal file
View File

@@ -0,0 +1,7 @@
---
- name: Install and configure network time protocol daemon
hosts: "{{ ntp_hosts | default('ntp') }}"
become: "{{ ntp_become | default(false) }}"
gather_facts: "{{ ntp_gather_facts | default(false) }}"
roles:
- role: finallycoffee.base.ntp

4
roles/ntp/README.md Normal file
View File

@@ -0,0 +1,4 @@
# `finallycoffee.base.ntp`
Install `ntp`, configure a timezone by sym-linking `/etc/localtime`
and enable the systemd service.

View File

@@ -0,0 +1,14 @@
---
ntp_state: present
ntp_package_name: "ntp"
ntp_timezone: "Europe/Paris"
ntp_systemd_service_name: "ntpd.service"
ntp_systemd_service_state: >-2
{{ (ntp_state == 'present') | ternary('started', 'stopped') }}
ntp_systemd_service_enabled: >-2
{{ (ntp_state == 'present') }}
ntp_etc_localtime_file: "/etc/localtime"
ntp_usr_share_zoneinfo_path: "/usr/share/zoneinfo"

8
roles/ntp/meta/main.yml Normal file
View File

@@ -0,0 +1,8 @@
---
allow_duplicates: true
dependencies: []
galaxy_info:
role_name: ntp
description: Install network time protocol daemon
galaxy_tags:
- ntp

28
roles/ntp/tasks/main.yml Normal file
View File

@@ -0,0 +1,28 @@
---
- name: Check if 'ntp_state' is valid
ansible.builtin.fail:
msg: >-2
Invalid state '{{ ntp_state }}'! Valid
states are {{ ntp_states | join(', ') }}.
when: ntp_state not in ntp_states
- name: Ensure system package is {{ ntp_state }}
ansible.builtin.package:
name: "{{ ntp_package_name }}"
state: "{{ ntp_state }}"
- name: Ensure /etc/localtime is symlinked
ansible.builtin.file:
src: "{{ ntp_usr_share_zoneinfo_path }}/{{ ntp_timezone }}"
dest: "{{ ntp_etc_localtime_file }}"
state: "{{ (ntp_state == 'present') | ternary('link', 'absent') }}"
- name: Ensure ntp systemd service is configured
ansible.builtin.systemd:
name: "{{ ntp_systemd_service_name }}"
enabled: "{{ ntp_systemd_service_enabled }}"
- name: Ensure ntp systemd service is {{ ntp_systemd_service_state }}
ansible.builtin.systemd:
name: "{{ ntp_systemd_service_name }}"
state: "{{ ntp_systemd_service_state }}"

4
roles/ntp/vars/main.yml Normal file
View File

@@ -0,0 +1,4 @@
---
ntp_states:
- "present"
- "absent"