Enhance user deletion playbook to handle undefined user facts and check mode
This commit is contained in:
@@ -32,8 +32,10 @@
|
||||
block:
|
||||
- name: Get user home directory
|
||||
ansible.builtin.set_fact:
|
||||
user_home: "{{ user_info.ansible_facts.getent_passwd[username][4] }}"
|
||||
when: not user_info.failed
|
||||
user_home: "{{ user_info.ansible_facts.getent_passwd[username][4] if user_info.ansible_facts is defined and user_info.ansible_facts.getent_passwd is defined and username in user_info.ansible_facts.getent_passwd else '/home/' + username }}"
|
||||
when:
|
||||
- not user_info.failed
|
||||
- user_info.ansible_facts is defined
|
||||
|
||||
- name: Check for running processes owned by user
|
||||
ansible.builtin.shell: "ps -u {{ username }} -o pid= | wc -l"
|
||||
@@ -89,6 +91,7 @@
|
||||
- backup_home | bool
|
||||
- user_home is defined
|
||||
- user_home != ""
|
||||
- not ansible_check_mode
|
||||
|
||||
- name: Remove user from additional groups (if any)
|
||||
ansible.builtin.user:
|
||||
@@ -108,7 +111,9 @@
|
||||
ansible.builtin.stat:
|
||||
path: "{{ user_home }}"
|
||||
register: home_check
|
||||
when: user_home is defined
|
||||
when:
|
||||
- user_home is defined
|
||||
- not ansible_check_mode
|
||||
|
||||
- name: Force remove home directory if it still exists
|
||||
ansible.builtin.file:
|
||||
@@ -142,13 +147,17 @@
|
||||
patterns: "*{{ username }}*"
|
||||
file_type: file
|
||||
register: user_logs
|
||||
when: not ansible_check_mode
|
||||
|
||||
- name: Remove user-specific log files
|
||||
ansible.builtin.file:
|
||||
path: "{{ item.path }}"
|
||||
state: absent
|
||||
loop: "{{ user_logs.files }}"
|
||||
when: user_logs.files is defined
|
||||
loop: "{{ user_logs.files | default([]) }}"
|
||||
when:
|
||||
- not ansible_check_mode
|
||||
- user_logs.files is defined
|
||||
- user_logs.files | length > 0
|
||||
|
||||
- name: Remove user from sudoers if present
|
||||
ansible.builtin.file:
|
||||
|
||||
Reference in New Issue
Block a user