-
Notifications
You must be signed in to change notification settings - Fork 34
Add role for configuring sudoers groups #709
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
MoteHue
wants to merge
1
commit into
main
Choose a base branch
from
sudoers-role
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+127
−0
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,3 +90,5 @@ roles/* | |
!roles/gateway/** | ||
!roles/alertmanager/ | ||
!roles/alertmanager/** | ||
!roles/sudoers/ | ||
!roles/sudoers/** |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# sudoers | ||
|
||
Manage sudoers configuration by creating files in `/etc/sudoers.d/`. | ||
|
||
## Role Variables | ||
|
||
- `sudoers_groups`: Required list. A list of dictionaries defining sudo group configurations. Each dictionary should contain: | ||
- `group`: Required string. The group name to grant sudo privileges to. | ||
- `commands`: Required string. The sudo commands specification (e.g., "ALL=(ALL) ALL"). | ||
- `state`: Optional string. Either "present" (default) or "absent" to remove the configuration. | ||
|
||
## Features | ||
|
||
- Creates individual sudoers files for each group in `/etc/sudoers.d/` | ||
- Validates sudoers syntax using `visudo -cf` before applying | ||
- Sanitizes group names by replacing spaces and slashes with underscores for filename safety | ||
- Supports removing sudoers configurations by setting `state: absent` | ||
- Sets proper permissions (0440) and ownership (root:root) on sudoers files | ||
|
||
## Dependencies | ||
|
||
None. | ||
|
||
## Example Playbook | ||
|
||
```yaml | ||
- hosts: servers | ||
become: yes | ||
roles: | ||
- role: sudoers | ||
vars: | ||
sudoers_groups: | ||
- group: SITE_Admins | ||
commands: "ALL=(ALL) ALL" | ||
- group: SITE_Users | ||
commands: "ALL=(ALL) ALL" | ||
- group: developers | ||
commands: "ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart myapp" | ||
- group: old_group | ||
state: absent | ||
``` | ||
|
||
## Example Variables | ||
|
||
```yaml | ||
# Merge your existing groups like this: | ||
sudoers_groups: "{{ sudo_groups + management_sudo_groups }}" | ||
|
||
# Where you have: | ||
sudo_groups: | ||
- group: SITE_Users | ||
commands: "ALL=(ALL) ALL" | ||
|
||
management_sudo_groups: | ||
- group: SITE_Admins | ||
commands: "ALL=(ALL) ALL" | ||
``` | ||
|
||
## License | ||
|
||
Apache v2 | ||
|
||
## Author Information | ||
|
||
StackHPC Ltd. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
# Default variables for sudoers role | ||
|
||
# List of sudo groups to configure | ||
# Each item should have: | ||
# - group: the group name (required) | ||
# - commands: the sudo commands specification (required) | ||
# - state: present (default) or absent (optional) | ||
sudoers_groups: [] | ||
|
||
# Example: | ||
# sudoers_groups: | ||
# - group: SITE_Admins | ||
# commands: "ALL=(ALL) ALL" | ||
# - group: SITE_Users | ||
# commands: "ALL=(ALL) ALL" | ||
# - group: developers | ||
# commands: "ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart myapp" | ||
# - group: old_group | ||
# state: absent |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--- | ||
- name: Configure sudoers files | ||
template: | ||
src: sudoers.j2 | ||
dest: /etc/sudoers.d/{{ item.group | replace(' ', '_') | replace('/', '_') }} | ||
owner: root | ||
group: root | ||
mode: '0440' | ||
validate: 'visudo -cf %s' | ||
become: true | ||
with_items: "{{ sudoers_groups }}" | ||
when: | ||
- sudoers_groups is defined | ||
- sudoers_groups | length > 0 | ||
|
||
- name: Remove sudoers files for absent groups | ||
file: | ||
path: /etc/sudoers.d/{{ item.group | replace(' ', '_') | replace('/', '_') }} | ||
state: absent | ||
become: true | ||
with_items: "{{ sudoers_groups }}" | ||
when: | ||
- sudoers_groups is defined | ||
- item.state | default('present') == 'absent' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
%{{ item.group }} {{ item.commands }} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add more indentation to these lines.