Skip to content

[ADD] ops/server.user: allow to add user to more secondary groups #1331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pyinfra/operations/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ def user(
shell: str | None = None,
group: str | None = None,
groups: list[str] | None = None,
append=False,
public_keys: str | list[str] | None = None,
delete_keys=False,
ensure_home=True,
Expand All @@ -783,6 +784,7 @@ def user(
+ shell: the users shell
+ group: the users primary group
+ groups: the users secondary groups
+ append: whether to add `user` to `groups`, w/o losing membership of other groups
+ public_keys: list of public keys to attach to this user, ``home`` must be specified
+ delete_keys: whether to remove any keys not specified in ``public_keys``
+ ensure_home: whether to ensure the ``home`` directory exists
Expand Down Expand Up @@ -929,6 +931,8 @@ def user(

# Check secondary groups, if defined
if groups and set(existing_user["groups"]) != set(groups):
if append:
args.append("-a")
args.append("-G {0}".format(",".join(groups)))

if comment and existing_user["comment"] != comment:
Expand Down
36 changes: 36 additions & 0 deletions tests/operations/server.user/append_secondary_groups.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"args": ["someuser"],
"kwargs": {
"group": "somegroup",
"groups": [
"group3", "group4"
],
"append": true
},
"facts": {
"server.Os": "Linux",
"server.Users": {
"someuser": {
"home": "/home/someuser",
"group": "somegroup",
"groups": [
"group1", "group2"
]
}
},
"files.Directory": {
"path=/home/someuser": {
"user": "someuser",
"group": "somegroup"
},
"path=/home/someotheruser": {
"user": "someuser",
"group": "somegroup"
}
},
"server.Groups": {}
},
"commands": [
"usermod -a -G group3,group4 someuser"
]
}
Loading