|
|
|
@ -21,8 +21,10 @@ import copy
|
|
|
|
|
import os
|
|
|
|
|
import time
|
|
|
|
|
import uuid
|
|
|
|
|
import hashlib
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils._text import to_text
|
|
|
|
|
from ansible.module_utils._text import to_text, to_bytes
|
|
|
|
|
from ansible.module_utils.connection import Connection
|
|
|
|
|
from ansible.errors import AnsibleError
|
|
|
|
|
from ansible.plugins.action import ActionBase
|
|
|
|
@ -38,6 +40,7 @@ except ImportError:
|
|
|
|
|
class ActionModule(ActionBase):
|
|
|
|
|
|
|
|
|
|
def run(self, tmp=None, task_vars=None):
|
|
|
|
|
changed = True
|
|
|
|
|
socket_path = None
|
|
|
|
|
play_context = copy.deepcopy(self._play_context)
|
|
|
|
|
play_context.network_os = self._get_network_os(task_vars)
|
|
|
|
@ -70,7 +73,7 @@ class ActionModule(ActionBase):
|
|
|
|
|
if mode is None:
|
|
|
|
|
mode = 'binary'
|
|
|
|
|
|
|
|
|
|
if mode == 'template':
|
|
|
|
|
if mode == 'text':
|
|
|
|
|
try:
|
|
|
|
|
self._handle_template()
|
|
|
|
|
except ValueError as exc:
|
|
|
|
@ -97,6 +100,17 @@ class ActionModule(ActionBase):
|
|
|
|
|
|
|
|
|
|
if dest is None:
|
|
|
|
|
dest = src_file_path_name
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
changed = self._handle_existing_file(conn, output_file, dest, proto, sock_timeout)
|
|
|
|
|
if changed is False:
|
|
|
|
|
result['changed'] = False
|
|
|
|
|
result['destination'] = dest
|
|
|
|
|
return result
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
result['msg'] = ('Warning: Exc %s idempotency check failed. Check'
|
|
|
|
|
'dest' % exc)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
out = conn.copy_file(
|
|
|
|
|
source=output_file, destination=dest,
|
|
|
|
@ -112,13 +126,55 @@ class ActionModule(ActionBase):
|
|
|
|
|
result['failed'] = True
|
|
|
|
|
result['msg'] = ('Exception received : %s' % exc)
|
|
|
|
|
|
|
|
|
|
if mode == 'template':
|
|
|
|
|
if mode == 'text':
|
|
|
|
|
# Cleanup tmp file expanded wih ansible vars
|
|
|
|
|
os.remove(output_file)
|
|
|
|
|
|
|
|
|
|
result['changed'] = True
|
|
|
|
|
result['changed'] = changed
|
|
|
|
|
result['destination'] = dest
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
def _handle_existing_file(self, conn, source, dest, proto, timeout):
|
|
|
|
|
cwd = self._loader.get_basedir()
|
|
|
|
|
filename = str(uuid.uuid4())
|
|
|
|
|
source_file = os.path.join(cwd, filename)
|
|
|
|
|
try:
|
|
|
|
|
out = conn.get_file(
|
|
|
|
|
source=dest, destination=source_file,
|
|
|
|
|
proto=proto, timeout=timeout
|
|
|
|
|
)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
if (to_text(exc)).find("No such file or directory") > 0:
|
|
|
|
|
return True
|
|
|
|
|
else:
|
|
|
|
|
try:
|
|
|
|
|
os.remove(source_file)
|
|
|
|
|
except OSError as osex:
|
|
|
|
|
raise Exception(osex)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
with open(source, 'r') as f:
|
|
|
|
|
new_content = f.read()
|
|
|
|
|
with open(source_file, 'r') as f:
|
|
|
|
|
old_content = f.read()
|
|
|
|
|
except (IOError, OSError) as ioexc:
|
|
|
|
|
raise IOError(ioexc)
|
|
|
|
|
|
|
|
|
|
sha1 = hashlib.sha1()
|
|
|
|
|
old_content_b = to_bytes(old_content, errors='surrogate_or_strict')
|
|
|
|
|
sha1.update(old_content_b)
|
|
|
|
|
checksum_old = sha1.digest()
|
|
|
|
|
|
|
|
|
|
sha1 = hashlib.sha1()
|
|
|
|
|
new_content_b = to_bytes(new_content, errors='surrogate_or_strict')
|
|
|
|
|
sha1.update(new_content_b)
|
|
|
|
|
checksum_new = sha1.digest()
|
|
|
|
|
os.remove(source_file)
|
|
|
|
|
if checksum_old == checksum_new:
|
|
|
|
|
return False
|
|
|
|
|
else:
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def _get_working_path(self):
|
|
|
|
|
cwd = self._loader.get_basedir()
|
|
|
|
|
if self._task._role is not None:
|
|
|
|
|