From feb9ed1de85e9cad6450bf61341c3a0b535124cd Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Mon, 20 Oct 2014 22:15:46 -0400 Subject: [PATCH] Fix template module incorrectly handling mode when dest is a directory Fixes #9350 --- lib/ansible/modules/core | 2 +- lib/ansible/runner/action_plugins/template.py | 5 ++- .../roles/test_template/tasks/main.yml | 39 +++++++++++++++++-- 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/lib/ansible/modules/core b/lib/ansible/modules/core index 5af8d55b036..fa6d74a9705 160000 --- a/lib/ansible/modules/core +++ b/lib/ansible/modules/core @@ -1 +1 @@ -Subproject commit 5af8d55b0365a5c3278c43b5424bf5f2ddf897b8 +Subproject commit fa6d74a97054d5d5123696d1af94b31ac1a65237 diff --git a/lib/ansible/runner/action_plugins/template.py b/lib/ansible/runner/action_plugins/template.py index 11e37b4815a..4f5a41df8a0 100644 --- a/lib/ansible/runner/action_plugins/template.py +++ b/lib/ansible/runner/action_plugins/template.py @@ -133,9 +133,12 @@ class ActionModule(object): # when running the file module based on the template data, we do # not want the source filename (the name of the template) to be used, # since this would mess up links, so we clear the src param and tell - # the module to follow links + # the module to follow links. When doing that, we have to set + # original_basename to the template just in case the dest is + # a directory. new_module_args = dict( src=None, + original_basename=os.path.basename(source), follow=True, ) # be sure to inject the check mode param into the module args and diff --git a/test/integration/roles/test_template/tasks/main.yml b/test/integration/roles/test_template/tasks/main.yml index 4784dc6ac82..0c5d3d18bf9 100644 --- a/test/integration/roles/test_template/tasks/main.yml +++ b/test/integration/roles/test_template/tasks/main.yml @@ -60,7 +60,40 @@ register: file_result - name: ensure file mode did not change - assert: - that: + assert: + that: - "file_result.changed != True" - + +# VERIFY dest as a directory does not break file attributes +# Note: expanduser is needed to go down the particular codepath that was broken before +- name: setup directory for test + file: state=directory dest={{output_dir | expanduser}}/template-dir mode=0755 owner=nobody group=nobody + +- name: set file mode when the destination is a directory + template: src=foo.j2 dest={{output_dir | expanduser}}/template-dir/ mode=0600 owner=root group=root + +- name: set file mode when the destination is a directory + template: src=foo.j2 dest={{output_dir | expanduser}}/template-dir/ mode=0600 owner=root group=root + register: file_result + +- name: check that the file has the correct attributes + stat: path={{output_dir | expanduser}}/template-dir/foo.j2 + register: file_attrs + +- assert: + that: + - "file_attrs.stat.gid == 0" + - "file_attrs.stat.uid == 0" + - "file_attrs.stat.pw_name == 'root'" + - "file_attrs.stat.mode == '0600'" + +- name: check that the containing directory did not change attributes + stat: path={{output_dir | expanduser}}/template-dir/ + register: dir_attrs + +- assert: + that: + - "dir_attrs.stat.gid != 0" + - "dir_attrs.stat.uid != 0" + - "dir_attrs.stat.pw_name == 'nobody'" + - "dir_attrs.stat.mode == '0755'"