Skip to content

Commit

Permalink
test: check generated files for ansible_managed, fingerprint
Browse files Browse the repository at this point in the history
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 two parameters:

* `__file` - required - the full path of the file to check e.g. `/etc/realmd.conf`
* `__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 realmd header for ansible_managed, fingerprint
  include_tasks: tasks/check_header.yml
  vars:
    __file: /etc/realmd.conf
    __fingerprint: "system_role:ad_integration"
```
  • Loading branch information
richm committed Apr 19, 2023
1 parent 37ba72a commit 0f209bf
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
15 changes: 15 additions & 0 deletions tests/tasks/check_header.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# SPDX-License-Identifier: MIT
---
- name: Get file
slurp:
path: "{{ __file }}"
register: __content

- name: Check for presence of ansible managed header, fingerprint
assert:
that:
- ansible_managed in content
- __fingerprint in content
vars:
content: "{{ __content.content | b64decode }}"
ansible_managed: "{{ lookup('template', 'get_ansible_managed.j2') }}"
1 change: 1 addition & 0 deletions tests/templates/get_ansible_managed.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{ ansible_managed | comment(__comment_type | d("plain")) }}
6 changes: 6 additions & 0 deletions tests/tests_basic_join.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@
register: __stat_result
failed_when: not __stat_result.stat.exists
changed_when: false

- name: Test - Check realmd headers
include_tasks: tasks/check_header.yml
vars:
__file: /etc/realmd.conf
__fingerprint: "system_role:ad_integration"

0 comments on commit 0f209bf

Please sign in to comment.