Skip to content

Various improvements to role#62

Open
Zugschlus wants to merge 13 commits intoweareinteractive:masterfrom
Zugschlus:zg/various_improvements
Open

Various improvements to role#62
Zugschlus wants to merge 13 commits intoweareinteractive:masterfrom
Zugschlus:zg/various_improvements

Conversation

@Zugschlus
Copy link
Copy Markdown

Hi,
this is my first attempt to improve a published ansible role. I apologize for mixing up so many different things in one PR. I tried to separate the things in different commits so you can cherry-pick at will. I would love to add unit tests, but the docs don't seem to match reality, the Makefile doesn't actually have a test target.

My changes (see detailed commit comments for more in-depth explanations and use cases):

  • update some when clauses to explicitly cast variables to bool
  • check whether the user exists before call to ansible.builtin.user. This makes some of the later changes possible
  • add random_password options to set a password for a newly created user and put the password in the home directory.
  • implement update_user variable analogous to update_password, just for the entire user
  • implement password_change_date to determine later whether user has changed password or not
  • improve task output by adding label to loop
  • add controllable debug output
  • implement user_create to be able to disable a user

Thanks for your consideration

Marc Haber added 13 commits April 13, 2026 08:37
This allows later tasks to determine whether the user was just created by
ansible.builtin.user or just modified. This allows us to overwrite the
password only for a brand new user.
We use a configurable command, defaulting to pwgen --secure 12 1, to
generate a random password that can later be poked into the user.
This will always overwrite the password with the
user_random_password_hash if user_random_password_hash is set. That will
only happen for a newly created user if a random password is asked for.
If those preocnditions are not met, behavior remains unchanged.
The new user needs to know their password. We therefore put it in a file
in the home directory, only readable for themselves. This is necessary
to allow the user to change their password.
This implements an "update_user" parameter which works like
"update_password": If set to "on_create", the user is only touched if
it already exists. If set to "always", the existing user's data is
always forced to match what is set for the user. This is an important
distinction, for example between service accounts (which should always
match what is in the playbook and local changes should be overwritten)
and user accoutns (which might allow to user to, for example, set their
own shell of desire without ansible stomping over the user's decision).
This allows to see whether a newly created user has changed the password
that was set by this role. With this, it becomes possible to first
create a new account, then have the owner change the password, and then
add them to the sudoers group only after the password was changed.
The setting is only done if a password was actually generated and set.
This is necessary because ansible.builtin.user does not expose this
setting (see ansible/ansible#86828)
This gets rid of the multiline output for the user loop. If access to
the full user dict is desired, a debug task should be added
This allows one to use "when: users_debug | bool" in debugging tasks
... disabled by default
This allows one to disable creating users that have incomplete info
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant