Skip to content
This repository was archived by the owner on Oct 7, 2025. It is now read-only.

Commit ec47c79

Browse files
authored
Merge pull request #2485 from codeenigma/Updating-sudoers-task-PR-2.x
Updating-sudoers-task
2 parents 21310b3 + 3ca5619 commit ec47c79

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

roles/debian/sudo_config/tasks/main.yml

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,48 @@
55
state: directory
66
mode: "0750"
77

8-
- name: "Add {{ sudo_config.entity_name }} to sudoers."
8+
- name: Create temporary directory for sudoers validation.
9+
ansible.builtin.tempfile:
10+
state: directory
11+
suffix: sudoers
12+
register: temp_sudoers_dir
13+
when: sudo_config | default([]) | length > 0
14+
15+
- name: Create sudoers file for validation.
916
ansible.builtin.template:
1017
src: "sudoer.j2"
11-
dest: "/etc/sudoers.d/{{ sudo_config.filename }}"
18+
dest: "{{ temp_sudoers_dir.path }}/{{ item.filename }}"
1219
owner: root
1320
group: root
1421
mode: "0440"
15-
when: sudo_config.entity_name | length > 0
22+
when: item.entity_name | default('') | length > 0
23+
with_items: "{{ sudo_config if sudo_config is iterable and sudo_config is not mapping else [sudo_config] }}"
24+
register: sudo_templates
25+
26+
- name: Validate sudoers file on remote.
27+
ansible.builtin.command: "visudo -cf {{ temp_sudoers_dir.path }}/{{ item.filename }}"
28+
register: visudo_check
29+
failed_when: visudo_check.rc != 0
30+
changed_when: false
31+
with_items: "{{ sudo_config if sudo_config is iterable and sudo_config is not mapping else [sudo_config] }}"
32+
when: item.entity_name | default('') | length > 0
33+
loop_control:
34+
label: "{{ item.filename }}"
35+
delegate_to: "{{ inventory_hostname }}"
36+
37+
- name: Install validated sudoers file.
38+
ansible.builtin.copy:
39+
src: "{{ temp_sudoers_dir.path }}/{{ item.filename }}"
40+
dest: "/etc/sudoers.d/{{ item.filename }}"
41+
owner: root
42+
group: root
43+
mode: "0440"
44+
remote_src: true
45+
when: item.entity_name | default('') | length > 0
46+
with_items: "{{ sudo_config if sudo_config is iterable and sudo_config is not mapping else [sudo_config] }}"
47+
48+
- name: Clean up temporary files.
49+
ansible.builtin.file:
50+
path: "{{ temp_sudoers_dir.path }}"
51+
state: absent
52+
when: temp_sudoers_dir.path is defined
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{{ sudo_config.entity_name }} {{ sudo_config.hosts }}={{ sudo_config.operators }} {{ sudo_config.tags }} {{ sudo_config.commands }}
1+
{{ item.entity_name }} {{ item.hosts | default('ALL') }}={{ item.operators | default('ALL') }} {{ item.tags | default('') }} {{ item.commands | default('ALL') }}

0 commit comments

Comments
 (0)