Skip to content

Commit 98d6f81

Browse files
committed
change nhc configuration from replace to lineinfile
1 parent 1462904 commit 98d6f81

File tree

3 files changed

+25
-26
lines changed

3 files changed

+25
-26
lines changed

ansible/roles/nhc/README.md

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ to update the autogenerated health checks:
6363

6464
## Role Variables
6565

66-
- `nhc_replacements`: Optional, default empty list. A list of mappings
67-
defining replacements in the autogenerated health checks. Items must have
68-
keys `regexp` and `replace` which are as for [ansible.builtin.replace](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/replace_module.html).
66+
- `nhc_config_changes`: Optional, default empty list. A list of mappings
67+
defining replacements in the autogenerated health checks. Mappings may have
68+
any keys from [ansible.builtin.lineinfile](https://docs.ansible.com/ansible/latest/collections/ansible/builtin/lineinfile_module.html).
6969
Note that the NHC [configuration line format](https://github.com/mej/nhc?tab=readme-ov-file#configuration-file-syntax) is:
7070

7171
TARGET || CHECK
@@ -75,20 +75,15 @@ to update the autogenerated health checks:
7575
'^(\s+\S+\s+)\|\|(\s+.*)$'
7676

7777
captures the `TARGET` and `||` separator as `\1` and the actual check as `\2`.
78-
Hence the following item would comment-out checks on a particular interface
79-
on all nodes:
80-
81-
- regexp: '^(\s+\S+\s+\|\|\s+)(check_hw_eth eth0)$'
82-
replace: '#\1\2'
83-
84-
See documentation for `ansible.builtin.replace` for more information. This is
85-
an example only - for this actual case removing the line entirely with
86-
`replace: ''` might be better. Using https://regex101.com/ (in Python
87-
mode) or similar may be useful during development.
88-
89-
- `nhc_replacements_default`: Optional. As above, but by default includes a
90-
mapping to remote the autogenerated timestamp line from the check configuration
91-
file for idempotency.
78+
Hence - as an example only - the following comments-out checks on a
79+
specific interface for all nodes:
80+
81+
```yaml
82+
nhc_config_changes:
83+
- regexp: '^(\s+\S+\s+\|\|\s+)(check_hw_eth eth0)$'
84+
line: '#\1\2'
85+
backrefs: yes
86+
```
9287
9388
## Structure
9489
Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
nhc_replacements_default:
2-
# note this matches a multiline string
3-
- regexp: '# This file was automatically generated by nhc-genconf\n#.*'
4-
replace: '# This file was automatically generated by nhc-genconf\n# (timestamp removed for idempotency)'
5-
6-
nhc_replacements: []
1+
nhc_config_changes: []

ansible/roles/nhc/tasks/main.yml

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,18 @@
99
failed_when: _nhc_genconf.rc > 1 # https://github.com/mej/nhc/issues/158
1010

1111
- name: Apply modifications to autogenerated NHC configuration
12+
ansible.builtin.lineinfile: "{{ item | combine(nhc_lineinfile_defaults) }}"
13+
loop: "{{ nhc_config_changes }}"
14+
vars:
15+
nhc_lineinfile_defaults:
16+
path: "{{ nhc_tempfile.path }}"
17+
18+
- name: Remove timestamp from autogenerated NHC configuration
1219
ansible.builtin.replace:
1320
path: "{{ nhc_tempfile.path }}"
14-
regexp: "{{ item.regexp }}"
15-
replace: "{{ item.replace }}"
16-
loop: "{{ nhc_replacements_default + nhc_replacements }}"
21+
# note this matches a multiline string:
22+
regexp: '# This file was automatically generated by nhc-genconf\n#.*'
23+
replace: '# This file was automatically generated by nhc-genconf\n# (timestamp removed for idempotency)'
1724

1825
- name: Copy modified NHC configuration to active location
1926
ansible.builtin.copy:
@@ -23,3 +30,5 @@
2330
owner: root
2431
group: root
2532
mode: u=rw,go= # HealthCheckProgram is run by root
33+
34+
- meta: end_here

0 commit comments

Comments
 (0)