Skip to content

Commit c46a955

Browse files
authored
[common] Fix failure condition in file test (#204)
* [common] Fix failure condition in file test failed_when uses and for list items, and fails only when all conditions are met * [common][file] Use assert instead of failed_when This allows more information to be shown about why the test failed.
1 parent 70857ad commit c46a955

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

roles/common/tasks/file_tests.yml

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,20 @@
33
ansible.builtin.stat:
44
path: "{{ item }}"
55
register: fstats
6-
failed_when:
7-
- fstats.stat.pw_name != "root"
8-
- fstats.stat.size | int < 300
9-
- not fstats.stat.exists
10-
- not fstats.stat.isreg
6+
7+
- name: |
8+
Verify the file {{ item }} exists
9+
{{ common_file_test_id }}
10+
ansible.builtin.assert:
11+
that:
12+
- fstats.stat.exists
13+
- fstats.stat.pw_name == "root"
14+
- fstats.stat.size | int > 300
15+
- fstats.stat.isreg
16+
fail_msg: |
17+
File {{ item }} does not meet the required conditions:
18+
{%- if not fstats.stat.exists %} file does not exist.{% endif %}
19+
{%- if fstats.stat.exists and fstats.stat.pw_name != "root" %} file owner must be root (got {{ fstats.stat.pw_name }});{% endif %}
20+
{%- if fstats.stat.exists and fstats.stat.size | int < 300 %} file must be larger than 300 bytes (got {{ fstats.stat.size }} bytes);{% endif %}
21+
{%- if fstats.stat.exists and not fstats.stat.isreg | bool %} file must be a regular file.{% endif %}
22+
success_msg: "The test passed"

0 commit comments

Comments
 (0)