mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
add support for diff in file settings
this allows modules to report on what specifically changed when using common file functions
This commit is contained in:
parent
5a33004b18
commit
6f2f7a79b3
1 changed files with 48 additions and 13 deletions
|
@ -751,7 +751,7 @@ class AnsibleModule(object):
|
|||
context = self.selinux_default_context(path)
|
||||
return self.set_context_if_different(path, context, False)
|
||||
|
||||
def set_context_if_different(self, path, context, changed):
|
||||
def set_context_if_different(self, path, context, changed, diff=None):
|
||||
|
||||
if not HAVE_SELINUX or not self.selinux_enabled():
|
||||
return changed
|
||||
|
@ -772,6 +772,14 @@ class AnsibleModule(object):
|
|||
new_context[i] = cur_context[i]
|
||||
|
||||
if cur_context != new_context:
|
||||
if diff is not None:
|
||||
if 'before' not in diff:
|
||||
diff['before'] = {}
|
||||
diff['before']['secontext'] = cur_context
|
||||
if 'after' not in diff:
|
||||
diff['after'] = {}
|
||||
diff['after']['secontext'] = new_context
|
||||
|
||||
try:
|
||||
if self.check_mode:
|
||||
return True
|
||||
|
@ -785,7 +793,7 @@ class AnsibleModule(object):
|
|||
changed = True
|
||||
return changed
|
||||
|
||||
def set_owner_if_different(self, path, owner, changed):
|
||||
def set_owner_if_different(self, path, owner, changed, diff=None):
|
||||
path = os.path.expanduser(path)
|
||||
if owner is None:
|
||||
return changed
|
||||
|
@ -798,6 +806,15 @@ class AnsibleModule(object):
|
|||
except KeyError:
|
||||
self.fail_json(path=path, msg='chown failed: failed to look up user %s' % owner)
|
||||
if orig_uid != uid:
|
||||
|
||||
if diff is not None:
|
||||
if 'before' not in diff:
|
||||
diff['before'] = {}
|
||||
diff['before']['owner'] = orig_uid
|
||||
if 'after' not in diff:
|
||||
diff['after'] = {}
|
||||
diff['after']['owner'] = uid
|
||||
|
||||
if self.check_mode:
|
||||
return True
|
||||
try:
|
||||
|
@ -807,7 +824,7 @@ class AnsibleModule(object):
|
|||
changed = True
|
||||
return changed
|
||||
|
||||
def set_group_if_different(self, path, group, changed):
|
||||
def set_group_if_different(self, path, group, changed, diff=None):
|
||||
path = os.path.expanduser(path)
|
||||
if group is None:
|
||||
return changed
|
||||
|
@ -820,6 +837,15 @@ class AnsibleModule(object):
|
|||
except KeyError:
|
||||
self.fail_json(path=path, msg='chgrp failed: failed to look up group %s' % group)
|
||||
if orig_gid != gid:
|
||||
|
||||
if diff is not None:
|
||||
if 'before' not in diff:
|
||||
diff['before'] = {}
|
||||
diff['before']['group'] = orig_gid
|
||||
if 'after' not in diff:
|
||||
diff['after'] = {}
|
||||
diff['after']['group'] = gid
|
||||
|
||||
if self.check_mode:
|
||||
return True
|
||||
try:
|
||||
|
@ -829,7 +855,7 @@ class AnsibleModule(object):
|
|||
changed = True
|
||||
return changed
|
||||
|
||||
def set_mode_if_different(self, path, mode, changed):
|
||||
def set_mode_if_different(self, path, mode, changed, diff=None):
|
||||
path = os.path.expanduser(path)
|
||||
path_stat = os.lstat(path)
|
||||
|
||||
|
@ -851,6 +877,15 @@ class AnsibleModule(object):
|
|||
prev_mode = stat.S_IMODE(path_stat.st_mode)
|
||||
|
||||
if prev_mode != mode:
|
||||
|
||||
if diff is not None:
|
||||
if 'before' not in diff:
|
||||
diff['before'] = {}
|
||||
diff['before']['mode'] = prev_mode
|
||||
if 'after' not in diff:
|
||||
diff['after'] = {}
|
||||
diff['after']['mode'] = mode
|
||||
|
||||
if self.check_mode:
|
||||
return True
|
||||
# FIXME: comparison against string above will cause this to be executed
|
||||
|
@ -984,27 +1019,27 @@ class AnsibleModule(object):
|
|||
or_reduce = lambda mode, perm: mode | user_perms_to_modes[user][perm]
|
||||
return reduce(or_reduce, perms, 0)
|
||||
|
||||
def set_fs_attributes_if_different(self, file_args, changed):
|
||||
def set_fs_attributes_if_different(self, file_args, changed, diff=None):
|
||||
# set modes owners and context as needed
|
||||
changed = self.set_context_if_different(
|
||||
file_args['path'], file_args['secontext'], changed
|
||||
file_args['path'], file_args['secontext'], changed, diff
|
||||
)
|
||||
changed = self.set_owner_if_different(
|
||||
file_args['path'], file_args['owner'], changed
|
||||
file_args['path'], file_args['owner'], changed, diff
|
||||
)
|
||||
changed = self.set_group_if_different(
|
||||
file_args['path'], file_args['group'], changed
|
||||
file_args['path'], file_args['group'], changed, diff
|
||||
)
|
||||
changed = self.set_mode_if_different(
|
||||
file_args['path'], file_args['mode'], changed
|
||||
file_args['path'], file_args['mode'], changed, diff
|
||||
)
|
||||
return changed
|
||||
|
||||
def set_directory_attributes_if_different(self, file_args, changed):
|
||||
return self.set_fs_attributes_if_different(file_args, changed)
|
||||
def set_directory_attributes_if_different(self, file_args, changed, diff=None):
|
||||
return self.set_fs_attributes_if_different(file_args, changed, diff)
|
||||
|
||||
def set_file_attributes_if_different(self, file_args, changed):
|
||||
return self.set_fs_attributes_if_different(file_args, changed)
|
||||
def set_file_attributes_if_different(self, file_args, changed, diff=None):
|
||||
return self.set_fs_attributes_if_different(file_args, changed, diff)
|
||||
|
||||
def add_path_info(self, kwargs):
|
||||
'''
|
||||
|
|
Loading…
Reference in a new issue