Overhaul local user management

Fix user deletion on removal/reassignment
Improve efficiency and cross platform support
This commit is contained in:
Ethan Paul 2019-09-27 02:59:11 -04:00
parent 9e646c3308
commit dca25832c4
4 changed files with 110 additions and 74 deletions

View File

@ -2,3 +2,7 @@
ansible_user: ansible ansible_user: ansible
disable_gnome_user_list: True disable_gnome_user_list: True
protected_users:
- root
- ansible

View File

@ -13,6 +13,7 @@
state: directory state: directory
path: /home/{{ item.name }}/.ssh path: /home/{{ item.name }}/.ssh
loop: "{{ local_users | difference([None]) }}" loop: "{{ local_users | difference([None]) }}"
- name: Put keys on remote - name: Put keys on remote
when: item.keys != [] when: item.keys != []
authorized_key: authorized_key:

View File

@ -1,7 +1,7 @@
--- ---
- import_playbook: dependencies.yml - import_playbook: dependencies.yml
- hosts: all - hosts: all:!network
name: Update local user accounts and access controls name: Update local user accounts and access controls
tasks: tasks:
- import_tasks: tasks/users-preprocessing.yml - import_tasks: tasks/users-preprocessing.yml
@ -21,56 +21,35 @@
name: "{{ item.name }}" name: "{{ item.name }}"
comment: "{{ item.fullname | default('') }}" comment: "{{ item.fullname | default('') }}"
shell: /bin/bash shell: /bin/bash
groups: "{{ item.targets | intersect(targets) }} + {{ [ 'omni' ] if item.name != 'root' else [] }}" groups: "{{ item.targets | intersect(targets) + ['omni'] }}"
system: "{{ item.svc | default('no') }}" system: "{{ item.svc | default(False) }}"
state: present state: present
generate_ssh_key: "{{ 'yes' if generate_keys|bool == true else 'no' }}" generate_ssh_key: "{{ True if generate_keys | bool == true else False }}"
ssh_key_comment: "{{ item.name }}@{{ inventory_hostname }}" ssh_key_comment: "{{ item.name }}@{{ inventory_hostname }}"
ssh_key_bits: 4096 ssh_key_bits: 4096
ssh_key_type: ed25519 ssh_key_type: ed25519
password: "{{ item.password }}" password: "{{ item.password }}"
loop: "{{ local_users | difference([None]) }}" loop: "{{ local_users }}"
- name: Delete users that have been removed
tags: users_delete
block:
- name: Determine existing users
shell: 'grep omni /etc/group | cut -d: -f4 | tr "," "\n"'
changed_when: false
register: existing_users
- name: Coallate user names
set_fact:
user_names: "{{ user_names | default([]) + [item.name] }}"
loop: "{{ users }}"
- name: Determine removed users
set_fact:
removed_users: "{{ existing_users.stdout_lines | difference(local_users) | difference([None]) }}"
- name: Delete removed user accounts - name: Delete removed user accounts
become: true become: true
user: user:
name: "{{ item }}" name: "{{ item }}"
state: absent state: absent
loop: "{{ removed_users }}" loop: "{{ local_removed_users | difference(protected_users) }}"
- name: Grant sudo permissions - name: Grant sudo permissions to admin user accounts
tags: users_sudo
block:
- name: Add users to sudo group on Fedora/CentOS/RHEL
when: ansible_distribution == "Fedora" or ansible_distribution == "Red Hat Enterprise Linux" or ansible_distribution == "CentOS"
become: true become: true
user: user:
name: "{{ item }}" name: "{{ item.name }}"
groups: wheel groups: "{{ 'wheel' if ansible_os_family | lower == 'redhat' else 'sudo' }}"
state: present state: present
loop: "{{ local_admin_users | difference([None]) }}" loop: "{{ local_admin_users }}"
- name: Disable sudo password for ansible - name: Disable sudo password for ansible
become: true become: true
lineinfile: lineinfile:
create: yes create: true
path: /etc/sudoers.d/30-ansible path: /etc/sudoers.d/30-ansible
line: "ansible ALL=(ALL) NOPASSWD:ALL" line: "ansible ALL=(ALL) NOPASSWD:ALL"
mode: 0644 mode: 0644
@ -78,12 +57,12 @@
- name: Disable sudo password for admin users - name: Disable sudo password for admin users
become: true become: true
lineinfile: lineinfile:
create: yes create: true
path: /etc/sudoers.d/40-admin path: /etc/sudoers.d/40-admin
line: "{{ item }} ALL=(ALL) NOPASSWD:ALL" line: "{{ item.name }} ALL=(ALL) NOPASSWD:ALL"
mode: 0644 mode: 0644
state: "{{ 'absent' if disable_sudo_password | bool == false else 'present' }}" state: "{{ 'absent' if disable_sudo_password | bool == false else 'present' }}"
loop: "{{ local_admin_users | difference([None] )}}" loop: "{{ local_admin_users }}"
- name: Configure GNOME - name: Configure GNOME
tags: users_gnome tags: users_gnome
@ -92,7 +71,8 @@
block: block:
- name: Configure GDM profile - name: Configure GDM profile
blockinfile: blockinfile:
path: /etc/ssh/sshd_config create: true
path: /etc/dconf/profile/gdm
block: | block: |
user-db:user user-db:user
system-db:gdm system-db:gdm
@ -118,8 +98,35 @@
group: "{{ item.name }}" group: "{{ item.name }}"
owner: "{{ item.name }}" owner: "{{ item.name }}"
path: /home/{{ item.name }} path: /home/{{ item.name }}
recurse: yes recurse: true
state: directory state: directory
loop: "{{ local_users | difference([None]) }}" loop: "{{ local_users }}"
- import_playbook: deploy-sshkeys.yml # - hosts: router.net.enp.one
# name: Configure users on router
# connection: network_cli
# vars:
# ansible_network_os: edgeos
# tasks:
# - import_tasks: tasks/users-preprocessing.yml
#
# - name: Create users
# edgeos_config:
# lines:
# - set system login user {{ item.name }} authentication encrypted-password "{{ item.password }}"
# - set system login user {{ item.name }} full-name "{{ item.fullname if item.fullname is defined else "" }}"
# - set system login user {{ item.name }} level {{ 'operator' if item.name != 'ansible' else 'admin' }}
# loop: "{{ local_users | difference([None]) }}"
#
# - name: Grant administrative access to admin users
# edgeos_config:
# lines:
# - set system login user {{ item.name }} level admin
# loop: "{{ local_admin_users | difference([None]) }}"
#
# - name: Assemble key files for loadkey usage
# edgeos_command:
# commands: sudo tee /tmp/{{ item.name }}.keys<<<"{{ item.sshkeys | join('\n') }}"
# loop: "{{ local_admin_users | difference([None]) }}"
#
# - import_playbook: deploy-sshkeys.yml

View File

@ -1,15 +1,39 @@
--- ---
- name: Pre-processing - name: Load users variables
tags: always
block:
- name: Load users
include_vars: include_vars:
file: users.yml file: users.yml
- name: Reconcile user targets with host targets to get host users - name: Reconcile user targets with host targets to get host users
set_fact: set_fact:
local_users: "{{ local_users | default([]) + [item if item.targets | intersect(targets) else None] }}" local_users: >-
with_items: "{{ users }}" {{
- name: Get administrative users local_users | default([]) + ([item] if item.targets | intersect(targets) else [])
}}
loop: "{{ users }}"
- name: Determine local user names
set_fact: set_fact:
local_admin_users: "{{ local_admin_users | default([]) + [item.name if item.admin else None] }}" local_user_names: "{{ local_user_names | default([]) + [item.name] }}"
with_items: "{{ local_users | difference([None]) }}" loop: "{{ local_users }}"
- name: Determine administrative users
set_fact:
local_admin_users: >-
{{
local_admin_users | default([]) + ([item] if item.admin | default(False) else [])
}}
loop: "{{ local_users }}"
- name: Determine existing users
shell: 'grep omni /etc/group | cut -d: -f4 | tr "," "\n"'
changed_when: false
register: local_existing_users
- name: Determine removed users
set_fact:
local_removed_users: >-
{{
local_removed_users | default([]) +
([item] if item not in local_user_names else [])
}}
loop: "{{ local_existing_users.stdout_lines }}"