Skip to content

Commit

Permalink
Revoke sudo permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
andyundso committed Oct 17, 2023
1 parent b5ffc51 commit c22ee29
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
19 changes: 19 additions & 0 deletions 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 | reject('in', (linux_accounts_sudo_users)) }}"

- name: "Revoke sudo permissions"
include_tasks: revoke-sudo.yml
loop: "{{ sudo_to_be_removed | difference(linux_accounts_sudo_users) }}"
loop_control:
loop_var: user

- name: "Create .ssh directory for user accounts"
file:
path: "~{{ item.key }}/.ssh"
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: "groups {{ user }}"
register: current_groups
changed_when: false

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

0 comments on commit c22ee29

Please sign in to comment.