3 Commits
Author SHA1 Message Date
CSRBot a836a63bcd Merge pull request 'chore(deps): update ansible/ansible-lint action to v26.11.1' (#46) from renovate/ansible-ansible-lint-26.x into master
Ansible Linter / ansible-lint (push) Successful in 1m45s
Lint Markdown files / markdown-lint (push) Successful in 13s
Molecule / Molecule (push) Successful in 4m26s
2026-11-25 11:27:04 +00:00
CSRBot a0d55b9f5f chore(deps): update ansible/ansible-lint action to v26.11.1
Ansible Linter / ansible-lint (push) Successful in 2m16s
Ansible Linter / ansible-lint (pull_request) Successful in 2m19s
Lint Markdown files / markdown-lint (push) Successful in 13s
Lint Markdown files / markdown-lint (pull_request) Successful in 12s
Molecule / Molecule (push) Successful in 4m40s
Molecule / Molecule (pull_request) Failing after 13m16s
2026-11-25 11:13:09 +00:00
volker.raschekandCopilot d8c825217b test: expand sudo role coverage
Molecule / Molecule (push) Successful in 1h31m0s
Lint Markdown files / markdown-lint (push) Successful in 9s
Ansible Linter / ansible-lint (push) Successful in 1m1s
Cover Ubuntu and invalid rules to verify supported configurations, and document role parameters and test dependencies.

Co-authored-by: Copilot <copilot@github.com>
2026-11-23 10:13:23 +01:00
6 changed files with 85 additions and 45 deletions

No files matched your search

+1 -1
View File
@@ -17,7 +17,7 @@ jobs:
steps: steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
- name: Run ansible-lint - name: Run ansible-lint
uses: ansible/ansible-lint@b74fb6f73d234bf92672372cb1c10768e4018d16 # v26.11.0 uses: ansible/ansible-lint@b5609548ec49df35aabb89b6cb71de8f398cd195 # v26.11.1
with: with:
args: "--config-file .ansible-lint.yml" args: "--config-file .ansible-lint.yml"
# The molecule scenario is linted as well, so its collections are required beside the ones of the role. # The molecule scenario is linted as well, so its collections are required beside the ones of the role.
+55 -36
View File
@@ -2,59 +2,78 @@
![Ansible Role](https://img.shields.io/ansible/role/d/volker-raschek/sudo) ![Ansible Role](https://img.shields.io/ansible/role/d/volker-raschek/sudo)
With following role can be created sudoers files in `/etc/sudoers.d`. For Installs sudo and manages individual rules in `/etc/sudoers.d` for users and groups. Unmanaged drop-in files are
example to grant a user special perimssions to execute a program as root. preserved.
## Supported distributions ## Requirements
- Arch Linux Install the required collection before running the role:
- Debian
- Fedora
- RHEL
- Ubuntu 20.04
## Features ```bash
ansible-galaxy collection install -r requirements.yml
```
- Installing sudo The role requires gathered facts to select distribution-specific package names and elevated privileges to install
- Configuring drop-on files in `/etc/sudoers.d` packages and configure sudo. Arch Linux, Debian, Fedora, RHEL and Ubuntu are supported.
## Parameters
| Name | Description | Default |
| --- | --- | --- |
| `sudo_users_sudoers` | List of sudoers drop-ins. Each entry must specify exactly one of `user` and `group`. | `[]` |
Each list entry accepts these keys:
| Key | Description | Default |
|--------------|----------------------------------------------------------------|---------------------|
| `user` | User to grant permissions to. Mutually exclusive with `group`. | None |
| `group` | Group to grant permissions to. Mutually exclusive with `user`. | None |
| `filename` | Name of the drop-in file. | User or group name |
| `commands` | Command or list of commands allowed by the rule. | `ALL` |
| `nopassword` | Allow execution without a password. | `false` |
| `runas` | User to run commands as. | sudo default (root) |
| `state` | `present` creates the rule; `absent` removes its drop-in. | `present` |
## Example
```yaml
- name: Configure sudo
hosts: all
become: true
roles:
- role: volker-raschek.sudo
sudo_users_sudoers:
- user: alice
commands:
- /usr/bin/systemctl restart nginx
nopassword: true
- group: operators
filename: operators-as-root
runas: root
commands:
- /usr/bin/id
- user: former-user
state: absent
```
## Tests ## Tests
The role is tested with [Molecule](https://ansible.readthedocs.io/projects/molecule/). The scenario starts one docker The [Molecule](https://ansible.readthedocs.io/projects/molecule/) scenario tests Arch Linux, Debian, Fedora and Ubuntu
container per supported distribution family, applies the role, asserts that a second run reports no change and finally containers. It checks idempotence, user and group rules, custom filenames, removal, preservation of unmanaged files,
verifies the created drop-in files, their permissions and content, that a rule declared as `absent` is gone again and permissions, invalid entries and `visudo` validation.
that `visudo` accepts the resulting configuration.
Molecule ships only its `default` driver, therefore `docker` is required beside molecule itself. The collections are Install Molecule and the Docker Python SDK, and ensure the Docker daemon is accessible:
declared in `molecule/default/collections.yml` and installed by molecule.
```bash ```bash
pip install molecule docker pip install molecule docker
```
The complete sequence creates the containers, tests them and removes them afterwards.
```bash
molecule test molecule test
``` ```
While working on the role the containers are better kept alive. For interactive testing:
```bash ```bash
# create the containers and apply the role
molecule converge molecule converge
# run the assertions of molecule/default/verify.yml against the running containers
molecule verify molecule verify
molecule login --host sudo-ubuntu
# open a shell in one of the containers
molecule login --host sudo-debian
# remove the containers
molecule destroy molecule destroy
``` ```
## Configuring
In the default directory are examples how to configure the network stack. Copy the
defaults into your `host_vars` or `group_vars` and adapt the examples.
+2 -8
View File
@@ -1,11 +1,5 @@
--- ---
## @section Sudoers
## @param sudo_users_sudoers List of sudoers drop-ins. Each entry must specify either `user` or `group`.
sudo_users_sudoers: [] sudo_users_sudoers: []
# - commands:
# - ALL
# filename: "" # Optional: Default to user or group
# group: "" # Group or User, not booth!
# nopassword: true #
# runas: "" # Optional
# state: present # Optional: absent removes this drop-in; defaults to present
# user: "markus": # Group or User, not booth!
+2
View File
@@ -2,4 +2,6 @@
collections: collections:
- name: community.docker - name: community.docker
version: '>=5.4.0'
- name: community.general - name: community.general
version: '>=4.7.0'
+2
View File
@@ -13,6 +13,8 @@ platforms:
image: docker.io/library/debian:13 image: docker.io/library/debian:13
- name: sudo-fedora - name: sudo-fedora
image: registry.fedoraproject.org/fedora:43 image: registry.fedoraproject.org/fedora:43
- name: sudo-ubuntu
image: docker.io/library/ubuntu:24.04
provisioner: provisioner:
name: ansible name: ansible
+23
View File
@@ -93,3 +93,26 @@
ansible.builtin.command: ansible.builtin.command:
cmd: visudo --check --strict cmd: visudo --check --strict
changed_when: false changed_when: false
- name: Verify that invalid sudoers entries are rejected
block:
- name: Validate invalid sudoers entries
ansible.builtin.include_tasks: ../../tasks/verify_vars.yml
vars:
sudo_users_sudoers:
- {}
- user: molecule-alice
group: molecule-ops
- name: Fail if invalid entries were accepted
ansible.builtin.fail:
msg: Invalid sudoers entries were accepted
rescue:
- name: Assert that both invalid entries failed validation
ansible.builtin.assert:
that:
- ansible_failed_result.results is defined
- ansible_failed_result.results | length == 2
- ansible_failed_result.results | selectattr('failed', 'equalto', true) | list | length == 2
- ansible_failed_result.results | selectattr('msg', 'search', "must define either 'user' or 'group'") | list | length == 2