From 75e9b76c15b2a6e195532468c0c5137dffffcdbf Mon Sep 17 00:00:00 2001 From: Andreas Calminder Date: Thu, 14 Jun 2018 15:31:14 +0200 Subject: [PATCH] accept + and - modifiers for file attributes (#40061) * accept + and - modifiers for file attributes * tests for adding/removing file attributes --- lib/ansible/module_utils/basic.py | 11 +++++++--- test/integration/targets/file/tasks/main.yml | 22 +++++++++++++++++--- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/lib/ansible/module_utils/basic.py b/lib/ansible/module_utils/basic.py index 1191416a1c2..c6a346fd241 100644 --- a/lib/ansible/module_utils/basic.py +++ b/lib/ansible/module_utils/basic.py @@ -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: diff --git a/test/integration/targets/file/tasks/main.yml b/test/integration/targets/file/tasks/main.yml index 51e3590dad4..c27a90b5120 100644 --- a/test/integration/targets/file/tasks/main.yml +++ b/test/integration/targets/file/tasks/main.yml @@ -82,13 +82,13 @@ - "file4_result.changed == true" - "file4_result.mode == '0600'" -- name: change file attribute "A" +- name: explicitly set file attribute "A" file: path={{output_dir}}/baz.txt attributes=A register: file_attributes_result ignore_errors: True -- name: reapply file attribute "A" - file: path={{output_dir}}/baz.txt attributes=A +- name: add file attribute "A" + file: path={{output_dir}}/baz.txt attributes=+A register: file_attributes_result_2 when: file_attributes_result is changed @@ -98,6 +98,22 @@ - "file_attributes_result_2 is not changed" when: file_attributes_result is changed +- name: remove file attribute "A" + file: path={{output_dir}}/baz.txt attributes=-A + register: file_attributes_result_3 + ignore_errors: True + +- name: explicitly remove file attributes + file: path={{output_dir}}/baz.txt attributes="" + register: file_attributes_result_4 + when: file_attributes_result_3 is changed + +- name: verify that the file was not marked as changed + assert: + that: + - "file_attributes_result_4 is not changed" + when: file_attributes_result_4 is changed + - name: change ownership and group file: path={{output_dir}}/baz.txt owner=1234 group=1234