diff --git a/playbooks/ntp.yml b/playbooks/ntp.yml new file mode 100644 index 0000000..79f8f96 --- /dev/null +++ b/playbooks/ntp.yml @@ -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 diff --git a/roles/ntp/README.md b/roles/ntp/README.md new file mode 100644 index 0000000..86eff5e --- /dev/null +++ b/roles/ntp/README.md @@ -0,0 +1,4 @@ +# `finallycoffee.base.ntp` + +Install `ntp`, configure a timezone by sym-linking `/etc/localtime` +and enable the systemd service. diff --git a/roles/ntp/defaults/main.yml b/roles/ntp/defaults/main.yml new file mode 100644 index 0000000..9b3388f --- /dev/null +++ b/roles/ntp/defaults/main.yml @@ -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" diff --git a/roles/ntp/meta/main.yml b/roles/ntp/meta/main.yml new file mode 100644 index 0000000..b7a1326 --- /dev/null +++ b/roles/ntp/meta/main.yml @@ -0,0 +1,8 @@ +--- +allow_duplicates: true +dependencies: [] +galaxy_info: + role_name: ntp + description: Install network time protocol daemon + galaxy_tags: + - ntp diff --git a/roles/ntp/tasks/main.yml b/roles/ntp/tasks/main.yml new file mode 100644 index 0000000..516dcee --- /dev/null +++ b/roles/ntp/tasks/main.yml @@ -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 }}" diff --git a/roles/ntp/vars/main.yml b/roles/ntp/vars/main.yml new file mode 100644 index 0000000..30a4706 --- /dev/null +++ b/roles/ntp/vars/main.yml @@ -0,0 +1,4 @@ +--- +ntp_states: + - "present" + - "absent"