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