Skip to content
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

Revoke sudo permissions #11

Merged
merged 4 commits into from
Oct 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion molecule/deletion/molecule.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ provisioner:
all:
linux_accounts_additional_users: { "bob": "bobssshkey" }

linux_accounts_default_users: { "alice": "alicessshkey" }
linux_accounts_default_users: { "alice": "alicessshkey", "erwin": "erwinsshkey" }

linux_accounts_additional_sudo_users:
- "bob"
Expand Down
7 changes: 7 additions & 0 deletions molecule/deletion/prepare.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,10 @@
loop:
- "charlie"
- "dave"
- "erwin"

- name: Grant sudo privileges to erwin
user:
name: "erwin"
groups: sudo
append: yes
13 changes: 13 additions & 0 deletions molecule/deletion/verify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,16 @@
database: passwd
fail_key: true
key: dave

- name: Get sudo group members
getent:
database: group
register: group_data
changed_when: false

- name: Assert 'erwin' is in the correct groups
assert:
that:
- "'erwin' in group_data['ansible_facts']['getent_group']['erwin'][2].split(',')"
- "'erwin' in group_data['ansible_facts']['getent_group']['accounts'][2].split(',')"
- "'erwin' not in group_data['ansible_facts']['getent_group']['sudo'][2].split(',')" # Index 2 typically holds the user list for the group.
21 changes: 20 additions & 1 deletion tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@
append: yes
loop: "{{ linux_accounts_sudo_users }}"

- name: Get sudo group informations
getent:
database: group
key: sudo

- name: "Get users in sudo group"
set_fact:
users_in_sudo_group: "{{ ansible_facts.getent_group['sudo'][2] | split(',') }}"

- name: "Set accounts to revoke sudo permissions"
set_fact:
sudo_to_be_removed: "{{ users_in_sudo_group | difference(linux_accounts_sudo_users) }}"

- name: "Revoke sudo permissions"
include_tasks: revoke-sudo.yml
loop: "{{ sudo_to_be_removed | difference(linux_accounts_sudo_users) }}"
Zinggi marked this conversation as resolved.
Show resolved Hide resolved
loop_control:
loop_var: user

- name: "Create .ssh directory for user accounts"
file:
path: "~{{ item.key }}/.ssh"
Expand Down Expand Up @@ -66,7 +85,7 @@

- name: "Set accounts to be removed"
set_fact:
accounts_to_be_removed: "{{ users_in_group | reject('in', (linux_accounts_users.keys()|list)) }}"
accounts_to_be_removed: "{{ users_in_group | difference(linux_accounts_users.keys()|list) }}"

- name: "Remove accounts"
user:
Expand Down
10 changes: 10 additions & 0 deletions tasks/revoke-sudo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- name: Get groups for user '{{ user }}'
command: "id -nG {{ user }}"
register: current_groups
changed_when: false

- name: Revoke 'sudo' for '{{ user }}'
user:
name: "{{ user }}"
groups: "{{ current_groups.stdout | replace('sudo', '') | split }}"
append: no