Skip to content

Commit 7c299f9

Browse files
MoteHuetechnowhizz
andcommitted
Add role for configuring sudoers groups
Co-Authored-By: Dawud Mehmood <[email protected]>
1 parent 13fa5c2 commit 7c299f9

File tree

8 files changed

+127
-0
lines changed

8 files changed

+127
-0
lines changed

ansible/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,5 @@ roles/*
9090
!roles/gateway/**
9191
!roles/alertmanager/
9292
!roles/alertmanager/**
93+
!roles/sudoers/
94+
!roles/sudoers/**

ansible/extras.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@
2828
- import_role:
2929
name: basic_users
3030

31+
- hosts: sudoers:!builder
32+
become: true
33+
tags: sudoers
34+
gather_facts: yes
35+
tasks:
36+
- name: Configure sudoers
37+
import_role:
38+
name: sudoers
39+
3140
- name: Setup EESSI
3241
hosts: eessi
3342
tags: eessi

ansible/roles/sudoers/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# sudoers
2+
3+
Manage sudoers configuration by creating files in `/etc/sudoers.d/`.
4+
5+
## Role Variables
6+
7+
- `sudoers_groups`: Required list. A list of dictionaries defining sudo group configurations. Each dictionary should contain:
8+
- `group`: Required string. The group name to grant sudo privileges to.
9+
- `commands`: Required string. The sudo commands specification (e.g., "ALL=(ALL) ALL").
10+
- `state`: Optional string. Either "present" (default) or "absent" to remove the configuration.
11+
12+
## Features
13+
14+
- Creates individual sudoers files for each group in `/etc/sudoers.d/`
15+
- Validates sudoers syntax using `visudo -cf` before applying
16+
- Sanitizes group names by replacing spaces and slashes with underscores for filename safety
17+
- Supports removing sudoers configurations by setting `state: absent`
18+
- Sets proper permissions (0440) and ownership (root:root) on sudoers files
19+
20+
## Dependencies
21+
22+
None.
23+
24+
## Example Playbook
25+
26+
```yaml
27+
- hosts: servers
28+
become: yes
29+
roles:
30+
- role: sudoers
31+
vars:
32+
sudoers_groups:
33+
- group: SITE_Admins
34+
commands: "ALL=(ALL) ALL"
35+
- group: SITE_Users
36+
commands: "ALL=(ALL) ALL"
37+
- group: developers
38+
commands: "ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart myapp"
39+
- group: old_group
40+
state: absent
41+
```
42+
43+
## Example Variables
44+
45+
```yaml
46+
# Merge your existing groups like this:
47+
sudoers_groups: "{{ sudo_groups + management_sudo_groups }}"
48+
49+
# Where you have:
50+
sudo_groups:
51+
- group: SITE_Users
52+
commands: "ALL=(ALL) ALL"
53+
54+
management_sudo_groups:
55+
- group: SITE_Admins
56+
commands: "ALL=(ALL) ALL"
57+
```
58+
59+
## License
60+
61+
Apache v2
62+
63+
## Author Information
64+
65+
StackHPC Ltd.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
# Default variables for sudoers role
3+
4+
# List of sudo groups to configure
5+
# Each item should have:
6+
# - group: the group name (required)
7+
# - commands: the sudo commands specification (required)
8+
# - state: present (default) or absent (optional)
9+
sudoers_groups: []
10+
11+
# Example:
12+
# sudoers_groups:
13+
# - group: SITE_Admins
14+
# commands: "ALL=(ALL) ALL"
15+
# - group: SITE_Users
16+
# commands: "ALL=(ALL) ALL"
17+
# - group: developers
18+
# commands: "ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart myapp"
19+
# - group: old_group
20+
# state: absent
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
- name: Configure sudoers files
3+
template:
4+
src: sudoers.j2
5+
dest: /etc/sudoers.d/{{ item.group | replace(' ', '_') | replace('/', '_') }}
6+
owner: root
7+
group: root
8+
mode: '0440'
9+
validate: 'visudo -cf %s'
10+
become: true
11+
with_items: "{{ sudoers_groups }}"
12+
when:
13+
- sudoers_groups is defined
14+
- sudoers_groups | length > 0
15+
16+
- name: Remove sudoers files for absent groups
17+
file:
18+
path: /etc/sudoers.d/{{ item.group | replace(' ', '_') | replace('/', '_') }}
19+
state: absent
20+
become: true
21+
with_items: "{{ sudoers_groups }}"
22+
when:
23+
- sudoers_groups is defined
24+
- item.state | default('present') == 'absent'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
%{{ item.group }} {{ item.commands }}

environments/common/inventory/groups

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,6 @@ extra_packages
182182

183183
[gateway]
184184
# Add builder to this group to install gateway ansible-init playbook into image
185+
186+
[sudoers]
187+
# Hosts to configure sudoers groups

environments/common/layouts/everything

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,3 +125,6 @@ builder
125125
[gateway:children]
126126
# Add builder to this group to install gateway ansible-init playbook into image
127127
builder
128+
129+
[sudoers]
130+
# Hosts to configure sudoers groups

0 commit comments

Comments
 (0)