The home directory for the given user will never be created. Use files.directory() to do this.
When a user is deleted, it's primary group will also be deleted if no other user has the same primary group. This is a quirk of the userdel tool, and applies when USERGROUPS_ENAB is set to yes in /etc/login.defs (which is the case on most distributions).
Similarly, when a user is created and no primary group is specified, a new primary group with the same name as the user will be created for it. As for deletion, this only applies when USERGROUPS_ENAB is set to yes in /etc/login.defs.
You can generate a password hash by using the following code:
Examples
Parameters
user: The name of the user.
present: Whether the given user should exists. If False any existing user with that name will be deleted and all other parameters ignored.
uid: The uid for the user. Automatically determined if not specified.
group: The primary group (name or gid) for the user. If given, the group must already exists. Otherwise, a group will be created with the same name as the user, if USERGROUPS_ENAB is set in /etc/login.defs.
groups: Secondary groups for the user.
append_groups: Only applies when groups were given. If False, the user will be removed from any groups other than the given ones. Otherwise, the user will be appended to the given groups.
system: If True the user will be created as a system user. This doesn't affect existing users.
password_hash: The password hash for the user as given by crypt(3). Defaults to '!' if not given but a user needs to be created.
home: The home directory for the user. Defaults to /dev/null if not given but a user needs to be created.
shell: Specifies the shell for the user. Defaults to /sbin/nologin if not given but a user needs to be created.
comment: Specifies the GECOS comment for the user. Will be left empty if not given but a user needs to be created.
name: The name for the operation.
check: If True, returning op.failure() will raise an OperationError. All manually raised OperationErrors will be propagated. When False, any manually raised OperationError will be caught and op.failure() will be returned with the given message while continuing execution.
op: The operation wrapper. Must not be supplied by the user.
defsystem.group()
Creates, modifies or deletes a unix group.
Examples
Parameters
group: The name of the group.
present: Whether the given group should exists. If False any existing group with that name will be deleted and all other parameters ignored.
gid: The gid for the group. Automatically determined if not specified.
system: If True the group will be created as a system group. This doesn't affect existing groups.
name: The name for the operation.
check: If True, returning op.failure() will raise an OperationError. All manually raised OperationErrors will be propagated. When False, any manually raised OperationError will be caught and op.failure() will be returned with the given message while continuing execution.
op: The operation wrapper. Must not be supplied by the user.
defsystem.package()
Adds or removes system packages by detecting a supported init system to execute the operation.
Examples
Parameters
packages: The packages to modify.
present: Whether the given package should be installed or uninstalled.
name: The name for the operation.
check: If True, returning op.failure() will raise an OperationError. All manually raised OperationErrors will be propagated. When False, any manually raised OperationError will be caught and op.failure() will be returned with the given message while continuing execution.
defsystem.service()
Manages a system service by detecting a supported init system to execute the operation.
Examples
Parameters
service: The unit to manage.
state: The desired state of the unit. Valid options are started, restarted, reloaded and stopped. If None, the service's current state will not be changed.
enabled: Whether the unit should be started on boot.
name: The name for the operation.
check: If True, returning op.failure() will raise an OperationError. All manually raised OperationErrors will be propagated. When False, any manually raised OperationError will be caught and op.failure() will be returned with the given message while continuing execution.
system.user(
name="Create a new user for some service including a group of the same name",
user="testuser")
system.user(
name="Create a new user with an existing primary group",
user="testuser",
group="users")
system.user(
name="Add myuser to the video group",
user="myuser",
groups=["video"],
append_groups=True)
system.user(
name="Delete testuser. Will also delete the corresponding primary group if it isn't used for anything else",
user="testuser",
present=False)
system.service(
name="Enable sshd to start on boot, and ensure it is started now",
service="sshd",
state="started",
enable=True)
system.service(
name="Just enable sshd to start on boot, but don't change anything about its current state",
service="sshd",
enable=True)
system.service(
name="Restart the nginx service now",
service="nginx",
state="restarted")