Delete user with Ansible in Debian

This Ansible playbook deletes a user along with their home directory and removes the user from the AllowUsers directive in the SSH configuration file.

Customizing files

1.Change the remote_user in adduser.yaml

- name: Create user and add SSH key
  hosts: all
  remote_user: root
  become: yes

  tasks:
  - name: Remove user 'zakops' with home directory
    user:
      name: zakops
      state: absent
      remove: yes

  - name: Remove 'zakops' from AllowUsers in /etc/ssh/sshd_config
    replace:
      path: /etc/ssh/sshd_config
      regexp: '^(AllowUsers.*)\bzakops\b'
      replace: '\1'
    notify: Restart SSH service

  handlers:
  - name: Restart SSH service
    service:
      name: ssh
      state: restarted

2.Update the IP address and port in the inventory

[hosts]
192.168.100.20:4422
Updated on