Skip to content

Commit 25188a7

Browse files
committed
fix: fix code to check for idempotency
1 parent 5a131fc commit 25188a7

File tree

4 files changed

+17
-26
lines changed

4 files changed

+17
-26
lines changed

tasks/main.yml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,14 @@
99
# no_log: true
1010
when: sudo_check_if_configured | bool
1111

12-
- name: Compare
13-
set_fact:
14-
comparison_result: "{{ ansible_facts['sudoers'].sudoers_files
15-
| combine({'include_files': omit})
16-
== sudo_sudoers_files }}"
17-
when: sudo_check_if_configured | bool
18-
19-
- name: Sudo is already configured
12+
- name: Sudo is already configured - skip rest of role
2013
ansible.builtin.meta: end_host
2114
when:
22-
- (comparison_result | default(false)) | bool
15+
- __actual == sudo_sudoers_files
2316
- sudo_check_if_configured | bool
17+
vars:
18+
__actual: "{{ ansible_facts['sudoers'].sudoers_files | map('dict2items') |
19+
map('rejectattr', 'key', 'match', '^include_files$') | map('items2dict') }}"
2420

2521
- name: Ensure sudo is installed
2622
package:

tests/roles/linux-system-roles.sudo/handlers

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../../../library/

tests/tests_check_if_configured.yml

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
- name: Test setup
1010
include_tasks: tasks/setup.yml
1111

12-
1312
- name: Install new sudoers file
1413
ansible.builtin.copy:
1514
src: files/tests_default_sudoers.in # Local file path
@@ -18,31 +17,27 @@
1817
group: root
1918
mode: '0440' # Correct permission for the sudoers file
2019

21-
22-
- name: print file
20+
- name: Print file
2321
command: cat /etc/sudoers
24-
25-
26-
- name: Check if the directory exists
27-
stat:
28-
path: /path/to/directory
29-
register: dir_stat
30-
22+
changed_when: false
3123

3224
- name: Remove all files from sudoers.d directory
33-
ansible.builtin.file:
34-
path: /etc/sudoers.d
35-
state: absent # Recursively remove the entire directory
36-
when: dir_stat.stat.exists
37-
25+
shell: |
26+
set -euxo pipefail
27+
for file in /etc/sudoers.d/*; do
28+
if [ -f "$file" ]; then
29+
rm "$file"
30+
fi
31+
done
32+
changed_when: true
3833

3934
- name: Run the role
4035
include_role:
4136
name: linux-system-roles.sudo
4237
vars:
4338
sudo_check_if_configured: true
4439

45-
- name: Check header for ansible_managed, fingerprint
40+
- name: Check that there is no header for ansible_managed, fingerprint
4641
include_tasks: tasks/check_not_present_header.yml
4742
vars:
4843
__file: /etc/sudoers

0 commit comments

Comments
 (0)