accept + and - modifiers for file attributes (#40061)

* accept + and - modifiers for file attributes
* tests for adding/removing file attributes
This commit is contained in:
Andreas Calminder 2018-06-14 15:31:14 +02:00 committed by Toshio Kuratomi
parent 8ab0d654f3
commit 75e9b76c15
2 changed files with 27 additions and 6 deletions

View file

@ -1355,10 +1355,15 @@ class AnsibleModule(object):
existing = self.get_file_attributes(b_path)
if existing.get('attr_flags', '') != attributes:
attr_mod = '='
if attributes.startswith(('-', '+')):
attr_mod = attributes[0]
attributes = attributes[1:]
if existing.get('attr_flags', '') != attributes or attr_mod == '-':
attrcmd = self.get_bin_path('chattr')
if attrcmd:
attrcmd = [attrcmd, '=%s' % attributes, b_path]
attrcmd = [attrcmd, '%s%s' % (attr_mod, attributes), b_path]
changed = True
if diff is not None:
@ -1367,7 +1372,7 @@ class AnsibleModule(object):
diff['before']['attributes'] = existing.get('attr_flags')
if 'after' not in diff:
diff['after'] = {}
diff['after']['attributes'] = attributes
diff['after']['attributes'] = '%s%s' % (attr_mod, attributes)
if not self.check_mode:
try: