- user – Manage user accounts¶
- Notes¶
- See Also¶
- Examples¶
- Return Values¶
- community.network.pn_user module – CLI command to create/modify/delete user
- Synopsis
- Parameters
- Examples
- Return Values
- Authors
- Collection links
- Remove user account — Ansible module user
- How to remove an example user removing home directory and spool files.
- How to remove a user account with Ansible?
- Ansible remove user account
- Main Parameters
- The Best Resources For Ansible
- Video Course
- Printed Book
- eBooks
- demo
- Recap
- Academy
- Donate
- See Also
user – Manage user accounts¶
If no , user will only be added to the groups specified in groups , removing them from all other groups.
Unless set to no , a home directory will be made for the user when the account is created or if the home directory does not exist.
Since Ansible 2.6 you can remove the expiry time specify a negative value. Currently supported on GNU/Linux and FreeBSD.
This only affects state=absent , it forces removal of the user and associated directories on supported platforms.
The behavior is the same as userdel —force , check the man page for userdel on your system for details and support.
List of groups user will be added to. When set to an empty string » , the user is removed from all groups except the primary group.
This is useful in environments that use centralized authentification when you want to manipulate the local users (i.e. it uses luseradd instead of useradd ).
This will check /etc/passwd for an existing account before invoking commands. If the local account database exists somewhere other than /etc/passwd , this setting will not work properly.
This requires that the above commands as well as /etc/passwd must exist on the target host, otherwise it will be a fatal error.
If set to yes when used with home: , attempt to move the user’s old home directory to the specified directory if it isn’t there already and the old home exists.
Optionally when used with the -u option, this option allows to change the user ID to a non-unique value.
Implementation differs by platform. This option does not always mean the user cannot login using other methods.
This must be set to False in order to unlock a currently locked password. The absence of this parameter will not unlock a password.
On macOS, before Ansible 2.5, the default shell for non-system users was /usr/bin/false . Since Ansible 2.5, the default shell for non-system users on macOS is /bin/bash .
On other operating systems, the default shell is determined by the underlying tool being used. See Notes for details.
Whether the account should exist or not, taking action if the state is different from what is stated.
Notes¶
- There are specific requirements per platform on user management utilities. However they generally come pre-installed with the system and Ansible will require they are present at runtime. If they are not, a descriptive error message will be shown.
- On SunOS platforms, the shadow file is backed up automatically since this module edits it directly. On other platforms, the shadow file is backed up by the underlying tools used by this module.
- On macOS, this module uses dscl to create, modify, and delete accounts. dseditgroup is used to modify group membership. Accounts are hidden from the login window by modifying /Library/Preferences/com.apple.loginwindow.plist .
- On FreeBSD, this module uses pw useradd and chpass to create, pw usermod and chpass to modify, pw userdel remove, pw lock to lock, and pw unlock to unlock accounts.
- On all other platforms, this module uses useradd to create, usermod to modify, and userdel to remove accounts.
See Also¶
The official documentation on the authorized_key module.
The official documentation on the group module.
The official documentation on the win_user module.
Examples¶
- name: Add the user 'johnd' with a specific uid and a primary group of 'admin' user: name: johnd comment: John Doe uid: 1040 group: admin - name: Add the user 'james' with a bash shell, appending the group 'admins' and 'developers' to the user's groups user: name: james shell: /bin/bash groups: admins,developers append: yes - name: Remove the user 'johnd' user: name: johnd state: absent remove: yes - name: Create a 2048-bit SSH key for user jsmith in ~jsmith/.ssh/id_rsa user: name: jsmith generate_ssh_key: yes ssh_key_bits: 2048 ssh_key_file: .ssh/id_rsa - name: Added a consultant whose account you want to expire user: name: james18 shell: /bin/zsh groups: developers expires: 1422403387 - name: Starting at Ansible 2.6, modify user, remove expiry time user: name: james18 expires: -1
Return Values¶
Common return values are documented here , the following are the fields unique to this module:
community.network.pn_user module – CLI command to create/modify/delete user
This module is part of the community.network collection (version 5.0.0).
You might already have this collection installed if you are using the ansible package. It is not included in ansible-core . To check whether it is installed, run ansible-galaxy collection list .
To install it, use: ansible-galaxy collection install community.network .
To use it in a playbook, specify: community.network.pn_user .
Synopsis
Parameters
pn_cliswitch
Target switch to run the CLI on.
pn_initial_role
pn_password
State the action to perform. Use present to create user and absent to delete user update to update user.
Examples
- name: Create user community.network.pn_user: pn_cliswitch: "sw01" state: "present" pn_scope: "fabric" pn_password: "foo123" pn_name: "foo" - name: Delete user community.network.pn_user: pn_cliswitch: "sw01" state: "absent" pn_name: "foo" - name: Modify user community.network.pn_user: pn_cliswitch: "sw01" state: "update" pn_password: "test1234" pn_name: "foo"
Return Values
Common return values are documented here , the following are the fields unique to this module:
indicates whether the CLI caused changes on the target.
the CLI command run on the target node.
set of error responses from the user command.
set of responses from the user command.
Authors
Collection links
© Copyright Ansible project contributors. Last updated on Jul 10, 2023.
Remove user account — Ansible module user
How to remove an example user removing home directory and spool files.
How to remove a user account with Ansible?
I’m going to show you a live demo with some simple Ansible code. I’m Luca Berton and welcome to today’s episode of Ansible Pilot.
Ansible remove user account
Today we’re talking about the Ansible module user . The full name is ansible.builtin.user , which means that is part of the collection of modules “builtin” with ansible and shipped with it. It’s a module pretty stable and out for years, it manages user accounts. It supports a huge variety of Linux distributions, SunOS and macOS, and FreeBSD. This module uses Linux distributions userdel to delete, on FreeBSD, this module uses pw userdel , on macOS, this module uses dscl . For Windows, use the ansible.windows.win_user module instead.
Main Parameters
This module has many parameters to perform any task. The only required is “name”, which is the username. “state” allows us to create or delete a user, in the use case we need to specify “absent” to delete a user. If we would like to try to remove the directories associated with the user, we need to set the parameter “remove”. The behavior is the same as userdel —remove . Files in the user’s home directory will be removed along with the home directory itself and the user’s mail spool. Files in other parts of the file system will have to be searched for and deleted manually.
The Best Resources For Ansible
Video Course
Printed Book
eBooks
demo
Let’s jump into a real-life Ansible Playbook to delete a user.
--- - name: user module demo hosts: all become: true tasks: - name: user example not present ansible.builtin.user: name: example state: "absent" remove: true
Recap
Now you know how to remove a user account with Ansible. Subscribe to the YouTube channel, Medium, Website, Twitter, and Substack to not miss the next episode of the Ansible Pilot.
Academy
Learn the Ansible automation technology with some real-life examples in my
My book Ansible By Examples: 200+ Automation Examples For Linux and Windows System Administrator and DevOps
Donate
Want to keep this project going? Please donate
See Also
The best way of talking about Ansible troubleshooting is to jump in a live demo to show you practically the missing module parameter and how to solve it!
How to create an example user with home directory, groups, password, and SSH key file with Ansible Playbook.
How to check if a file exists in Ansible? I’m going to show you a live demo and some simple Ansible code. I’m going to show you how to combine the «stat» module with conditional to verify an existing and not existing path.
Deep dive into the Ansible module win_ping in the collection ansible.windows to test the access to a managed Windows host and that that there is a shell usually PowerShell available. Live demo and Ansible playbook included.
The best way of talking about Ansible troubleshooting is to jump in a live demo to show you practically the failure downloading error and how to solve it!