Skip to content

Commit db2a4cc

Browse files
committed
test: check generated files for ansible_managed, fingerprint
Clean up more ansible-lint issues in code and README Add the following files: tests/tasks/check_header.yml and tests/templates/get_ansible_managed.j2. Use check_header.yml to check generated files for the ansible_managed and fingerprint headers. check_header.yml takes these parameters. `fingerprint` is required, and one of `__file` or `__file_content`: * `__file` - the full path of the file to check e.g. `/etc/realmd.conf` * `__file_content` - the output of `slurp` of the file * `__fingerprint` - required - the fingerprint string `system_role:$ROLENAME` e.g. `__fingerprint: "system_role:postfix"` * `__comment_type` - optional, default `plain` - the type of comments used e.g. `__comment_type: c` for C/C++-style comments. `plain` uses `#`. See https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_filters.html#adding-comments-to-files for the different types of comment styles supported. Example: ``` - name: Check generated files for ansible_managed, fingerprint include_tasks: tasks/check_header.yml vars: __file: /etc/myfile.conf __fingerprint: "system_role:my_role" ``` Signed-off-by: Rich Megginson <[email protected]>
1 parent b8eb8e3 commit db2a4cc

File tree

7 files changed

+79
-15
lines changed

7 files changed

+79
-15
lines changed

README.md

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
timesync
22
========
3+
34
![CI Testing](https://github.com/linux-system-roles/timesync/workflows/tox/badge.svg)
45

56
This role installs and configures an NTP and/or PTP implementation to operate
@@ -109,7 +110,8 @@ Example Playbook
109110
Install and configure ntp to synchronize the system clock with three NTP servers:
110111
111112
```yaml
112-
- hosts: targets
113+
- name: Manage timesync with 3 servers
114+
hosts: targets
113115
vars:
114116
timesync_ntp_servers:
115117
- hostname: foo.example.com
@@ -127,7 +129,8 @@ Install and configure linuxptp to synchronize the system clock with a
127129
grandmaster in PTP domain number 0, which is accessible on interface eth0:
128130
129131
```yaml
130-
- hosts: targets
132+
- name: Manage timesync in PTP domain 0, interface eth0
133+
hosts: targets
131134
vars:
132135
timesync_ptp_domains:
133136
- number: 0
@@ -141,7 +144,8 @@ multiple NTP servers and PTP domains for a highly accurate and resilient
141144
synchronization:
142145
143146
```yaml
144-
- hosts: targets
147+
- name: Manage multiple NTP servers and PTP domains
148+
hosts: targets
145149
vars:
146150
timesync_ntp_servers:
147151
- hostname: foo.example.com
@@ -167,9 +171,9 @@ Install and configure chrony with multiple NTP servers and custom advanced
167171
settings: log `measurements`, `statistics` and `tracking`
168172
into `/var/log/chrony`:
169173

170-
171174
```yaml
172-
- hosts: targets
175+
- name: Manage with custom advanced settings
176+
hosts: targets
173177
vars:
174178
timesync_ntp_servers:
175179
- hostname: foo.example.com

tasks/main.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,16 @@
2727
- name: Check if only NTP is needed
2828
set_fact:
2929
timesync_mode: 1
30-
when: timesync_ptp_domains|length == 0
30+
when: timesync_ptp_domains | length == 0
3131

3232
- name: Check if single PTP is needed
3333
set_fact:
3434
timesync_mode: 2
3535
when:
3636
- timesync_mode is not defined
37-
- timesync_ntp_servers|length == 0
38-
- timesync_ptp_domains|length == 1
39-
- timesync_ptp_domains[0].interfaces|length == 1
37+
- timesync_ntp_servers | length == 0
38+
- timesync_ptp_domains | length == 1
39+
- timesync_ptp_domains[0].interfaces | length == 1
4040

4141
- name: Check if both NTP and PTP are needed
4242
set_fact:

templates/ntpd.sysconfig.j2

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
{{ ansible_managed | comment }}
22
{{ "system_role:timesync" | comment(prefix="", postfix="") }}
33

4-
OPTIONS="-g{{ ' -u ntp:ntp -p /var/run/ntpd.pid' if ansible_distribution in ['OracleLinux', 'RedHat', 'CentOS'] and ansible_distribution_major_version|int < 7 else '' }}"
4+
OPTIONS="-g{{ ' -u ntp:ntp -p /var/run/ntpd.pid' if ansible_distribution in ['OracleLinux', 'RedHat', 'CentOS'] and ansible_distribution_major_version | int < 7 else '' }}"

tests/tasks/check_header.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-License-Identifier: MIT
2+
---
3+
- name: Get file
4+
slurp:
5+
path: "{{ __file }}"
6+
register: __content
7+
when: not __file_content is defined
8+
9+
- name: Check for presence of ansible managed header, fingerprint
10+
assert:
11+
that:
12+
- ansible_managed in content
13+
- __fingerprint in content
14+
vars:
15+
content: "{{ (__file_content | d(__content)).content | b64decode }}"
16+
ansible_managed: "{{ lookup('template', 'get_ansible_managed.j2') }}"
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{{ ansible_managed | comment(__comment_type | d("plain")) }}

tests/tests_chrony.yml

+15-2
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
timesync_step_threshold: 0.01
2020
timesync_dhcp_ntp_servers: true
2121
timesync_min_sources: 2
22-
roles:
23-
- linux-system-roles.timesync
2422

2523
tasks:
24+
- name: Run the role
25+
include_role:
26+
name: linux-system-roles.timesync
27+
public: true
28+
2629
- name: Run chrony tests
2730
tags: tests::verify
2831
block:
@@ -54,3 +57,13 @@
5457
assert:
5558
that:
5659
- "'tracking.log' in logfiles.stdout"
60+
61+
- name: Check headers for ansible_managed, fingerprint
62+
include_tasks: tasks/check_header.yml
63+
loop:
64+
- "{{ timesync_chrony_conf_path }}"
65+
- "{{ timesync_chrony_sysconfig_path }}"
66+
loop_control:
67+
loop_var: __file
68+
vars:
69+
__fingerprint: "system_role:timesync"

tests/tests_ntp.yml

+33-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,12 @@
2222
timesync_dhcp_ntp_servers: true
2323
timesync_min_sources: 2
2424
timesync_ntp_hwts_interfaces: ["*"]
25-
roles:
26-
- linux-system-roles.timesync
27-
2825
tasks:
26+
- name: Run the role
27+
include_role:
28+
name: linux-system-roles.timesync
29+
public: true
30+
2931
- name: Run test
3032
tags: tests::verify
3133
block:
@@ -109,3 +111,31 @@
109111
- ntp_conf is not search('172\.16\.123\.2.*true')
110112
- ntp_conf is search('172\.16\.123\.3.*true')
111113
when: ntp_conf is defined
114+
115+
- name: Check chrony conf for ansible_managed, fingerprint
116+
include_tasks: tasks/check_header.yml
117+
vars:
118+
__file_content: "{{ chrony_conf_encoded }}"
119+
__fingerprint: "system_role:timesync"
120+
when: chrony_conf is defined
121+
122+
- name: Check chrony sysconf for ansible_managed, fingerprint
123+
include_tasks: tasks/check_header.yml
124+
vars:
125+
__file: "{{ timesync_chrony_sysconfig_path }}"
126+
__fingerprint: "system_role:timesync"
127+
when: chrony_conf is defined
128+
129+
- name: Check ntp conf for ansible_managed, fingerprint
130+
include_tasks: tasks/check_header.yml
131+
vars:
132+
__file_content: "{{ ntp_conf_encoded }}"
133+
__fingerprint: "system_role:timesync"
134+
when: ntp_conf is defined
135+
136+
- name: Check ntp sysconf for ansible_managed, fingerprint
137+
include_tasks: tasks/check_header.yml
138+
vars:
139+
__file: "{{ timesync_ntp_sysconfig_path }}"
140+
__fingerprint: "system_role:timesync"
141+
when: ntp_conf is defined

0 commit comments

Comments
 (0)