community.general/lib/ansible/plugins/action/template.py

209 lines
8.2 KiB
Python
Raw Normal View History

# (c) 2015, Michael DeHaan <michael.dehaan@gmail.com>
#
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
2015-04-13 20:28:01 +00:00
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import datetime
import os
import pwd
import time
from ansible import constants as C
2016-12-13 17:14:47 +00:00
from ansible.compat.six import string_types
from ansible.errors import AnsibleError
from ansible.module_utils._text import to_bytes, to_native, to_text
Report detailed error when internal remote functions fail This is a redesign in how plugins call _remote_checksum(). - _remote_stat() has been modified to report the real error as AnsiblError - Action plugin **unarchive** calls _remote_stat() directly instead of _remote_checksum() - Action plugin **unarchive** also handles the exceptions directly - Ensure get_exception() returns native text Two other action plugins, **template** and **fetch**, also do a remote checksum. In **template** we already call _remote_stat(), just like we now do for unarchive, in **fetch** we do call _remote_checksum() and we make the exact same mistake as the unarchive plugin. So that one could use a redesign as well. This fixes #19494 Before: ``` [dag@moria ansible.testing]$ ansible-playbook -v test137.yml Using /home/dag/home-made/ansible.testing/ansible.cfg as config file PLAY [localhost] ****************************************************************************************************** TASK [unarchive] ****************************************************************************************************** fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "python isn't present on the system. Unable to compute checksum"} PLAY RECAP ****************************************************************************************************** localhost : ok=0 changed=0 unreachable=0 failed=1 ``` After: ``` [dag@moria ansible.testing]$ ansible-playbook -v test137.yml Using /home/dag/home-made/ansible.testing/ansible.cfg as config file PLAY [localhost] ************************************************************************************************************* TASK [unarchive] ************************************************************************************************************* fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Failed to get information on remote file (/tmp/): sudo: unknown user: foobar\nsudo: unable to initialize policy plugin\n"} PLAY RECAP ******************************************************************************************************************* localhost : ok=0 changed=0 unreachable=0 failed=1 ```
2016-12-18 18:55:21 +00:00
from ansible.module_utils.pycompat24 import get_exception
from ansible.plugins.action import ActionBase
from ansible.utils.hashing import checksum_s
boolean = C.mk_boolean
class ActionModule(ActionBase):
TRANSFERS_FILES = True
def get_checksum(self, dest, all_vars, try_directory=False, source=None, tmp=None):
try:
dest_stat = self._execute_remote_stat(dest, all_vars=all_vars, follow=False, tmp=tmp)
2015-02-25 19:26:07 +00:00
if dest_stat['exists'] and dest_stat['isdir'] and try_directory and source:
2015-02-25 19:26:07 +00:00
base = os.path.basename(source)
dest = os.path.join(dest, base)
dest_stat = self._execute_remote_stat(dest, all_vars=all_vars, follow=False, tmp=tmp)
2015-02-25 19:26:07 +00:00
Report detailed error when internal remote functions fail This is a redesign in how plugins call _remote_checksum(). - _remote_stat() has been modified to report the real error as AnsiblError - Action plugin **unarchive** calls _remote_stat() directly instead of _remote_checksum() - Action plugin **unarchive** also handles the exceptions directly - Ensure get_exception() returns native text Two other action plugins, **template** and **fetch**, also do a remote checksum. In **template** we already call _remote_stat(), just like we now do for unarchive, in **fetch** we do call _remote_checksum() and we make the exact same mistake as the unarchive plugin. So that one could use a redesign as well. This fixes #19494 Before: ``` [dag@moria ansible.testing]$ ansible-playbook -v test137.yml Using /home/dag/home-made/ansible.testing/ansible.cfg as config file PLAY [localhost] ****************************************************************************************************** TASK [unarchive] ****************************************************************************************************** fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "python isn't present on the system. Unable to compute checksum"} PLAY RECAP ****************************************************************************************************** localhost : ok=0 changed=0 unreachable=0 failed=1 ``` After: ``` [dag@moria ansible.testing]$ ansible-playbook -v test137.yml Using /home/dag/home-made/ansible.testing/ansible.cfg as config file PLAY [localhost] ************************************************************************************************************* TASK [unarchive] ************************************************************************************************************* fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Failed to get information on remote file (/tmp/): sudo: unknown user: foobar\nsudo: unable to initialize policy plugin\n"} PLAY RECAP ******************************************************************************************************************* localhost : ok=0 changed=0 unreachable=0 failed=1 ```
2016-12-18 18:55:21 +00:00
except AnsibleError:
return dict(failed=True, msg=to_native(get_exception()))
2015-02-25 19:26:07 +00:00
return dest_stat['checksum']
2015-02-25 19:26:07 +00:00
def run(self, tmp=None, task_vars=None):
''' handler for template operations '''
if task_vars is None:
task_vars = dict()
result = super(ActionModule, self).run(tmp, task_vars)
source = self._task.args.get('src', None)
dest = self._task.args.get('dest', None)
2015-11-07 00:06:29 +00:00
force = boolean(self._task.args.get('force', True))
state = self._task.args.get('state', None)
if state is not None:
result['failed'] = True
result['msg'] = "'state' cannot be specified on a template"
elif source is None or dest is None:
result['failed'] = True
result['msg'] = "src and dest are required"
2015-06-13 04:34:15 +00:00
else:
try:
source = self._find_needle('templates', source)
Report detailed error when internal remote functions fail This is a redesign in how plugins call _remote_checksum(). - _remote_stat() has been modified to report the real error as AnsiblError - Action plugin **unarchive** calls _remote_stat() directly instead of _remote_checksum() - Action plugin **unarchive** also handles the exceptions directly - Ensure get_exception() returns native text Two other action plugins, **template** and **fetch**, also do a remote checksum. In **template** we already call _remote_stat(), just like we now do for unarchive, in **fetch** we do call _remote_checksum() and we make the exact same mistake as the unarchive plugin. So that one could use a redesign as well. This fixes #19494 Before: ``` [dag@moria ansible.testing]$ ansible-playbook -v test137.yml Using /home/dag/home-made/ansible.testing/ansible.cfg as config file PLAY [localhost] ****************************************************************************************************** TASK [unarchive] ****************************************************************************************************** fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "python isn't present on the system. Unable to compute checksum"} PLAY RECAP ****************************************************************************************************** localhost : ok=0 changed=0 unreachable=0 failed=1 ``` After: ``` [dag@moria ansible.testing]$ ansible-playbook -v test137.yml Using /home/dag/home-made/ansible.testing/ansible.cfg as config file PLAY [localhost] ************************************************************************************************************* TASK [unarchive] ************************************************************************************************************* fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Failed to get information on remote file (/tmp/): sudo: unknown user: foobar\nsudo: unable to initialize policy plugin\n"} PLAY RECAP ******************************************************************************************************************* localhost : ok=0 changed=0 unreachable=0 failed=1 ```
2016-12-18 18:55:21 +00:00
except AnsibleError:
result['failed'] = True
Report detailed error when internal remote functions fail This is a redesign in how plugins call _remote_checksum(). - _remote_stat() has been modified to report the real error as AnsiblError - Action plugin **unarchive** calls _remote_stat() directly instead of _remote_checksum() - Action plugin **unarchive** also handles the exceptions directly - Ensure get_exception() returns native text Two other action plugins, **template** and **fetch**, also do a remote checksum. In **template** we already call _remote_stat(), just like we now do for unarchive, in **fetch** we do call _remote_checksum() and we make the exact same mistake as the unarchive plugin. So that one could use a redesign as well. This fixes #19494 Before: ``` [dag@moria ansible.testing]$ ansible-playbook -v test137.yml Using /home/dag/home-made/ansible.testing/ansible.cfg as config file PLAY [localhost] ****************************************************************************************************** TASK [unarchive] ****************************************************************************************************** fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "python isn't present on the system. Unable to compute checksum"} PLAY RECAP ****************************************************************************************************** localhost : ok=0 changed=0 unreachable=0 failed=1 ``` After: ``` [dag@moria ansible.testing]$ ansible-playbook -v test137.yml Using /home/dag/home-made/ansible.testing/ansible.cfg as config file PLAY [localhost] ************************************************************************************************************* TASK [unarchive] ************************************************************************************************************* fatal: [localhost]: FAILED! => {"changed": false, "failed": true, "msg": "Failed to get information on remote file (/tmp/): sudo: unknown user: foobar\nsudo: unable to initialize policy plugin\n"} PLAY RECAP ******************************************************************************************************************* localhost : ok=0 changed=0 unreachable=0 failed=1 ```
2016-12-18 18:55:21 +00:00
result['msg'] = to_native(get_exception())
if 'failed' in result:
return result
# Expand any user home dir specification
dest = self._remote_expand_user(dest)
directory_prepended = False
if dest.endswith(os.sep):
directory_prepended = True
base = os.path.basename(source)
dest = os.path.join(dest, base)
# template the source data locally & get ready to transfer
b_source = to_bytes(source)
try:
with open(b_source, 'r') as f:
template_data = to_text(f.read())
try:
template_uid = pwd.getpwuid(os.stat(b_source).st_uid).pw_name
except:
template_uid = os.stat(b_source).st_uid
temp_vars = task_vars.copy()
temp_vars['template_host'] = os.uname()[1]
temp_vars['template_path'] = source
temp_vars['template_mtime'] = datetime.datetime.fromtimestamp(os.path.getmtime(b_source))
temp_vars['template_uid'] = template_uid
temp_vars['template_fullpath'] = os.path.abspath(source)
temp_vars['template_run_date'] = datetime.datetime.now()
managed_default = C.DEFAULT_MANAGED_STR
managed_str = managed_default.format(
host = temp_vars['template_host'],
uid = temp_vars['template_uid'],
file = to_bytes(temp_vars['template_path'])
)
temp_vars['ansible_managed'] = time.strftime(
managed_str,
time.localtime(os.path.getmtime(b_source))
)
searchpath = []
# set jinja2 internal search path for includes
if 'ansible_search_path' in task_vars:
searchpath = task_vars['ansible_search_path']
# our search paths aren't actually the proper ones for jinja includes.
searchpath.extend([self._loader._basedir, os.path.dirname(source)])
# We want to search into the 'templates' subdir of each search path in
# addition to our original search paths.
newsearchpath = []
for p in searchpath:
newsearchpath.append(os.path.join(p, 'templates'))
newsearchpath.append(p)
searchpath = newsearchpath
self._templar.environment.loader.searchpath = searchpath
old_vars = self._templar._available_variables
self._templar.set_available_variables(temp_vars)
resultant = self._templar.do_template(template_data, preserve_trailing_newlines=True, escape_backslashes=False)
self._templar.set_available_variables(old_vars)
2015-04-13 16:35:20 +00:00
except Exception as e:
result['failed'] = True
result['msg'] = type(e).__name__ + ": " + str(e)
return result
remote_user = task_vars.get('ansible_ssh_user') or self._play_context.remote_user
if not tmp:
tmp = self._make_tmp_path(remote_user)
self._cleanup_remote_tmp = True
local_checksum = checksum_s(resultant)
remote_checksum = self.get_checksum(dest, task_vars, not directory_prepended, source=source, tmp=tmp)
2015-02-25 19:26:07 +00:00
if isinstance(remote_checksum, dict):
# Error from remote_checksum is a dict. Valid return is a str
result.update(remote_checksum)
return result
diff = {}
new_module_args = self._task.args.copy()
if (remote_checksum == '1') or (force and local_checksum != remote_checksum):
result['changed'] = True
# if showing diffs, we need to get the remote value
if self._play_context.diff:
diff = self._get_diff_data(dest, resultant, task_vars, source_file=False)
if not self._play_context.check_mode: # do actual work through copy
xfered = self._transfer_data(self._connection._shell.join_path(tmp, 'source'), resultant)
# fix file permissions when the copy is done as a different user
self._fixup_perms2((tmp, xfered), remote_user)
# run the copy module
new_module_args.update(
dict(
src=xfered,
dest=dest,
original_basename=os.path.basename(source),
follow=True,
),
)
result.update(self._execute_module(module_name='copy', module_args=new_module_args, task_vars=task_vars, tmp=tmp, delete_remote_tmp=False))
if result.get('changed', False) and self._play_context.diff:
result['diff'] = diff
else:
# 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. When doing that, we have to set
# original_basename to the template just in case the dest is
# a directory.
new_module_args.update(
dict(
src=None,
original_basename=os.path.basename(source),
follow=True,
),
)
result.update(self._execute_module(module_name='file', module_args=new_module_args, task_vars=task_vars, tmp=tmp, delete_remote_tmp=False))
self._remove_tmp_path(tmp)
return result