Ansible playbook to delete the user after taking backup of home directory of user

If you have user on multiple servers and want to delete them by 
taking backup of home directory. You can use below Ansible
playbook. This playbook is running by root user.
# cat delete_user.yml
## Delete user "test_user" from servers

---

- hosts: db-hosts
  remote_user: root
  gather_facts: no

  tasks:

  - name: Take backup of home directory for user test_user
    command: /bin/mv /home/test_user /home/test_user_backup

  - user:
      name: test_user
      state: absent
      remove: yes

Leave a Reply

Your email address will not be published. Required fields are marked *