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
Add diff support to user module #48880
base: devel
Are you sure you want to change the base?
Conversation
Hi @mgedmin, thank you for submitting this pull-request! |
This is not 100% complete (only the generic usermod-based User implementation reports changes), but it's better than nothing. Motivated by complete opaqueness of changed=True when a user's groups do not match what Ansible wants them to be.
This is a good start, but it does need some refinement. Please add support for other platforms as well as integration tests. Feel free to ask questions and I'll help in any way I can.
@@ -2609,6 +2629,10 @@ def main(): | |||
module.fail_json(name=user.name, msg=err, rc=rc) | |||
result['force'] = user.force | |||
result['remove'] = user.remove | |||
result['diff'] = { | |||
'before': 'user exists\n', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return the actual username here:
'before': 'user exists\n', | |
'before': '{0} exists\n'.format(user.name), |
@@ -2609,6 +2629,10 @@ def main(): | |||
module.fail_json(name=user.name, msg=err, rc=rc) | |||
result['force'] = user.force | |||
result['remove'] = user.remove | |||
result['diff'] = { | |||
'before': 'user exists\n', | |||
'after': 'user removed\n', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'after': 'user removed\n', | |
'after': '{0} removed\n'.format(user.name), |
@@ -2619,11 +2643,27 @@ def main(): | |||
else: | |||
result['system'] = user.system | |||
result['create_home'] = user.create_home | |||
result['diff'] = { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this up to before line 2641 so that the diff is returned in check mode.
@@ -2619,11 +2643,27 @@ def main(): | |||
else: | |||
result['system'] = user.system | |||
result['create_home'] = user.create_home | |||
result['diff'] = { | |||
'before': 'user does not exist\n', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'before': 'user does not exist\n', | |
'before': '{0} does not exist\n'.format(user.name), |
@@ -2619,11 +2643,27 @@ def main(): | |||
else: | |||
result['system'] = user.system | |||
result['create_home'] = user.create_home | |||
result['diff'] = { | |||
'before': 'user does not exist\n', | |||
'after': 'user created\n', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
'after': 'user created\n', | |
'after': '{0} created\n'.format(user.name), |
@@ -744,17 +760,21 @@ def modify_user_usermod(self): | |||
if current_expires < 0 or current_expire_date[:3] != self.expires[:3]: | |||
cmd.append('-e') | |||
cmd.append(time.strftime(self.DATE_FORMAT, self.expires)) | |||
self.changes['expires'] = (current_expires, self.expires) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Format the date string here so it's more human readable in the diff:
self.changes['expires'] = (current_expires, self.expires) | |
self.changes['expires'] = (current_expires, time.strftime(self.DATE_FORMAT, self.expires)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case I think I should also format current_expires
the same way.
You'll need to return the diff when exiting in check mode:
module.exit_json(changed=True, diff=result['diff'])
waiting_on_contributor |
SUMMARY
Make the
user
module report what was changed if you run ansible-playbook --diff.ISSUE TYPE
COMPONENT NAME
user
ADDITIONAL INFORMATION
This was motivated by me trying to manage user group memberships with Ansible and having no way to verify that my playbook is correct when the
user
module reportschanged
with no details whatsoever.What this does is produce brief diffs that lists old and new values of all user attributes that were modified, e.g.
This is not 100% complete (only the generic usermod-based User implementation reports changes, all those FreeBsdUser etc subclasses need to be updated), but it's better than nothing.
When a user is created or deleted, instead of listing all the attributes only a brief summary is shown, e.g.