From 5df55d1a3d554f4b4fc49c0ce88527a59a0ed109 Mon Sep 17 00:00:00 2001 From: Kasra Amirsarvari Date: Fri, 31 Jan 2020 07:58:58 +0100 Subject: [PATCH] Improvement on how crony.conf is built --- defaults/main.yml | 53 +++++++++++++++++++++++----------------- templates/chrony.conf.j2 | 37 ++++++++-------------------- 2 files changed, 41 insertions(+), 49 deletions(-) diff --git a/defaults/main.yml b/defaults/main.yml index 2058fce..9ee7184 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -1,29 +1,38 @@ --- ### Setting up the /etc/chrony.conf file. -## This dictionary contains a standard set of values that build up the configuration file. -## It is tied to the templates/chrony.conf.j2 template file. -role_chrony_chrony_dot_conf_parameters: - server: - - server 0.rhel.pool.ntp.org iburst minpoll 4 maxpoll 6 polltarget 16 - - server 1.rhel.pool.ntp.org iburst minpoll 4 maxpoll 6 polltarget 16 - - server 2.rhel.pool.ntp.org iburst minpoll 4 maxpoll 6 polltarget 16 - - server 3.rhel.pool.ntp.org iburst minpoll 4 maxpoll 6 polltarget 16 - drift_file: driftfile /var/lib/chrony/drift - makestep: makestep 1.0 3 - rtcsync: rtcsync - hwtimestamp: hwtimestamp * - minsources: minsources 2 - # allow: allow 192.168.0.0/16 - bindcmdaddress: - - 'bindcmdaddress 127.0.0.1' - - 'bindcmdaddress ::1' - # local: local stratum 10 - keyfile: keyfile /etc/chrony.keys - logdir: logdir /var/log/chrony - log: log measurements statistics tracking +## These variables together build up the configuration file. +## It is primarily tied to the templates/chrony.conf.j2 template file. +## +## Some parameters may contain free text values. No specific parsing of values +## takes place due to the fact that if it would increase the complexity of the +## Ansible role as will it's maintainability. It is strongly advised to consult +## the official and latest documentation in order to prevent unintended +## misconfiguration. Make sure to test your setup and verify a correct working +## set. The given defaults are part of a minimal, stable and recommended set +## of configuration parameters. +## +## Man pages: +## Enterprise Linux, Fedora: man chrony.conf +## +## The list of these variables require a naming convention to enable a pattern. +## The pattern is rendered as a list to iterate over once the configuration +## template gets deployed on the desired target destination. + +role_chrony_chrony_dot_conf_parameters_server: + - server 0.rhel.pool.ntp.org iburst minpoll 4 maxpoll 6 polltarget 16 + - server 1.rhel.pool.ntp.org iburst minpoll 4 maxpoll 6 polltarget 16 + - server 2.rhel.pool.ntp.org iburst minpoll 4 maxpoll 6 polltarget 16 + - server 3.rhel.pool.ntp.org iburst minpoll 4 maxpoll 6 polltarget 16 +role_chrony_chrony_dot_conf_parameters_makestep: makestep 1.0 3 +role_chrony_chrony_dot_conf_parameters_rtcsync: rtcsync +role_chrony_chrony_dot_conf_parameters_hwtimestamp: hwtimestamp * +role_chrony_chrony_dot_conf_parameters_minsources: minsources 2 +role_chrony_chrony_dot_conf_parameters_drift_file: driftfile /var/lib/chrony/drift +role_chrony_chrony_dot_conf_parameters_keyfile: keyfile /etc/chrony.keys +role_chrony_chrony_dot_conf_parameters_logdir: logdir /var/log/chrony ### Define the required timezone. ## Run the command 'timedatectl list-timezones' to see all available timezones. -role_chrony_time_zone: 'Europe/Amsterdam' +role_chrony_time_zone: Europe/Amsterdam ... diff --git a/templates/chrony.conf.j2 b/templates/chrony.conf.j2 index 9605d52..45eb6f4 100644 --- a/templates/chrony.conf.j2 +++ b/templates/chrony.conf.j2 @@ -1,29 +1,12 @@ # {{ ansible_managed }} -{% if role_chrony_chrony_dot_conf_parameters.server is defined and role_chrony_chrony_dot_conf_parameters.server|length %}{% for server in role_chrony_chrony_dot_conf_parameters.server %} -{{ server }} -{% endfor %}{% endif %} - -{% if role_chrony_chrony_dot_conf_parameters.drift_file is defined and role_chrony_chrony_dot_conf_parameters.drift_file|length %}{{ role_chrony_chrony_dot_conf_parameters.drift_file }}{% endif %} - -{% if role_chrony_chrony_dot_conf_parameters.makestep is defined and role_chrony_chrony_dot_conf_parameters.makestep|length %}{{ role_chrony_chrony_dot_conf_parameters.makestep }}{% endif %} - -{% if role_chrony_chrony_dot_conf_parameters.rtcsync is defined and role_chrony_chrony_dot_conf_parameters.rtcsync|length %}{{ role_chrony_chrony_dot_conf_parameters.rtcsync }}{% endif %} - -{% if role_chrony_chrony_dot_conf_parameters.hwtimestamp is defined and role_chrony_chrony_dot_conf_parameters.hwtimestamp|length %}{{ role_chrony_chrony_dot_conf_parameters.hwtimestamp }}{% endif %} - -{% if role_chrony_chrony_dot_conf_parameters.minsources is defined and role_chrony_chrony_dot_conf_parameters.minsources|length %}{{ role_chrony_chrony_dot_conf_parameters.minsources }}{% endif %} - -{% if role_chrony_chrony_dot_conf_parameters.allow is defined and role_chrony_chrony_dot_conf_parameters.allow|length %}{{ role_chrony_chrony_dot_conf_parameters.allow }}{% endif %} - -{% if role_chrony_chrony_dot_conf_parameters.bindcmdaddress is defined and role_chrony_chrony_dot_conf_parameters.bindcmdaddress|length %}{% for source in role_chrony_chrony_dot_conf_parameters.bindcmdaddress %} -{{ source }} -{% endfor %}{% endif %} - -{% if role_chrony_chrony_dot_conf_parameters.local is defined and role_chrony_chrony_dot_conf_parameters.local|length %}{{ role_chrony_chrony_dot_conf_parameters.local }}{% endif %} - -{% if role_chrony_chrony_dot_conf_parameters.keyfile is defined and role_chrony_chrony_dot_conf_parameters.keyfile|length %}{{ role_chrony_chrony_dot_conf_parameters.keyfile }}{% endif %} - -{% if role_chrony_chrony_dot_conf_parameters.logdir is defined and role_chrony_chrony_dot_conf_parameters.logdir|length %}{{ role_chrony_chrony_dot_conf_parameters.logdir }}{% endif %} - -{% if role_chrony_chrony_dot_conf_parameters.log is defined and role_chrony_chrony_dot_conf_parameters.log|length %}{{ role_chrony_chrony_dot_conf_parameters.log }}{% endif %} +{% set chrony_conf_vars = vars.keys() | select('match', '^role_chrony_chrony_dot_conf_parameters_.*$') %} +{% for key in chrony_conf_vars %} +{% if vars[key] is iterable and vars[key] is not string %} +{% for item in vars[key] %} +{{ item }} +{% endfor %} +{% else %} +{{ vars[key] }} +{% endif %} +{% endfor %} \ No newline at end of file