diff --git a/lib/ansible/modules/files/template.py b/lib/ansible/modules/files/template.py index c7d12026b08..459b8751c29 100644 --- a/lib/ansible/modules/files/template.py +++ b/lib/ansible/modules/files/template.py @@ -22,13 +22,14 @@ description: - Templates are processed by the L(Jinja2 templating language,http://jinja.pocoo.org/docs/). - Documentation on the template formatting can be found in the L(Template Designer Documentation,http://jinja.pocoo.org/docs/templates/). -- The six additional variables, listed below, can be used in template. +- Additional variables listed below can be used in templates. - C(ansible_managed) (configurable via the C(defaults) section of C(ansible.cfg)) contains a string which can be used to describe the template name, host, modification time of the template file and the owner uid. - C(template_host) contains the node name of the template's machine. - C(template_uid) is the numeric user id of the owner. - C(template_path) is the path of the template. - C(template_fullpath) is the absolute path of the template. +- C(template_destpath) is the path of the template on the remote system (added in 2.8). - C(template_run_date) is the date that the template was rendered. options: src: diff --git a/lib/ansible/modules/windows/win_template.py b/lib/ansible/modules/windows/win_template.py index 23e03c32794..6817d299159 100644 --- a/lib/ansible/modules/windows/win_template.py +++ b/lib/ansible/modules/windows/win_template.py @@ -19,15 +19,17 @@ description: (U(http://jinja.pocoo.org/docs/)) - documentation on the template formatting can be found in the Template Designer Documentation (U(http://jinja.pocoo.org/docs/templates/)). - - "Six additional variables can be used in templates: C(ansible_managed) + - "Additional variables can be used in templates: C(ansible_managed) (configurable via the C(defaults) section of C(ansible.cfg)) contains a string which can be used to describe the template name, host, modification time of the - template file and the owner uid, C(template_host) contains the node name of - the template's machine, C(template_uid) the owner, C(template_path) the - absolute path of the template, C(template_fullpath) is the absolute path of the - template, and C(template_run_date) is the date that the template was rendered. Note that including - a string that uses a date in the template will result in the template being marked 'changed' - each time." + template file and the owner uid." + - "C(template_host) contains the node name of the template's machine." + - "C(template_uid) the owner." + - "C(template_path) the absolute path of the template." + - "C(template_fullpath) is the absolute path of the template." + - "C(template_destpath) is the path of the template on the remote system (added in 2.8)." + - "C(template_run_date) is the date that the template was rendered." + - "Note that including a string that uses a date in the template will result in the template being marked 'changed' each time." - For other platforms you can use M(template) which uses '\n' as C(newline_sequence). options: src: diff --git a/lib/ansible/plugins/action/template.py b/lib/ansible/plugins/action/template.py index 111da52033e..ad21c25969a 100644 --- a/lib/ansible/plugins/action/template.py +++ b/lib/ansible/plugins/action/template.py @@ -141,7 +141,7 @@ class ActionModule(ActionBase): # add ansible 'template' vars temp_vars = task_vars.copy() - temp_vars.update(generate_ansible_template_vars(source)) + temp_vars.update(generate_ansible_template_vars(source, dest)) old_vars = self._templar._available_variables self._templar.set_available_variables(temp_vars) diff --git a/lib/ansible/template/__init__.py b/lib/ansible/template/__init__.py index a99f330dbfc..9739e1cbc3c 100644 --- a/lib/ansible/template/__init__.py +++ b/lib/ansible/template/__init__.py @@ -84,20 +84,22 @@ else: from jinja2.utils import concat as j2_concat -def generate_ansible_template_vars(path): +def generate_ansible_template_vars(path, dest_path=None): b_path = to_bytes(path) try: template_uid = pwd.getpwuid(os.stat(b_path).st_uid).pw_name except (KeyError, TypeError): template_uid = os.stat(b_path).st_uid - temp_vars = {} - temp_vars['template_host'] = to_text(os.uname()[1]) - temp_vars['template_path'] = path - temp_vars['template_mtime'] = datetime.datetime.fromtimestamp(os.path.getmtime(b_path)) - temp_vars['template_uid'] = to_text(template_uid) - temp_vars['template_fullpath'] = os.path.abspath(path) - temp_vars['template_run_date'] = datetime.datetime.now() + temp_vars = { + 'template_host': to_text(os.uname()[1]), + 'template_path': path, + 'template_mtime': datetime.datetime.fromtimestamp(os.path.getmtime(b_path)), + 'template_uid': to_text(template_uid), + 'template_fullpath': os.path.abspath(path), + 'template_run_date': datetime.datetime.now(), + 'template_destpath': to_native(dest_path) if dest_path else None, + } managed_default = C.DEFAULT_MANAGED_STR managed_str = managed_default.format( diff --git a/test/integration/targets/template/tasks/main.yml b/test/integration/targets/template/tasks/main.yml index 33e985da25a..64d08fe32c4 100644 --- a/test/integration/targets/template/tasks/main.yml +++ b/test/integration/targets/template/tasks/main.yml @@ -667,5 +667,23 @@ - list_var.1.foo is not defined - list_var.1.foo | default('DEFAULT') == 'DEFAULT' +- template: + src: template_destpath_test.j2 + dest: "{{ output_dir }}/template_destpath.templated" + +- copy: + content: "{{ output_dir}}/template_destpath.templated\n" + dest: "{{ output_dir }}/template_destpath.expected" + +- name: compare templated file to known good template_destpath + shell: diff -uw {{output_dir}}/template_destpath.templated {{output_dir}}/template_destpath.expected + register: diff_result + +- name: verify templated template_destpath matches known good + assert: + that: + - 'diff_result.stdout == ""' + - "diff_result.rc == 0" + # aliases file requires root for template tests so this should be safe - include: backup_test.yml diff --git a/test/integration/targets/template/templates/template_destpath_test.j2 b/test/integration/targets/template/templates/template_destpath_test.j2 new file mode 100644 index 00000000000..1d21d8cdb69 --- /dev/null +++ b/test/integration/targets/template/templates/template_destpath_test.j2 @@ -0,0 +1 @@ +{{ template_destpath }}