Idempotency for net_get and net_put modules (#42307)

* Idempotency for net_get and net_put modules

* pep8 warnings fix

* remove import q
This commit is contained in:
Deepak Agrawal 2018-07-05 20:15:25 +05:30 committed by GitHub
parent de40ac02a5
commit 30688fecf3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 155 additions and 19 deletions

View file

@ -21,8 +21,10 @@ import copy
import os
import time
import re
import uuid
import hashlib
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
@ -75,6 +77,16 @@ class ActionModule(ActionBase):
conn = Connection(socket_path)
try:
changed = self._handle_existing_file(conn, src, dest, proto, sock_timeout)
if changed is False:
result['changed'] = False
result['destination'] = dest
return result
except Exception as exc:
result['msg'] = ('Warning: exception %s idempotency check failed. Check '
'dest' % exc)
try:
out = conn.get_file(
source=src, destination=dest,
@ -128,3 +140,41 @@ class ActionModule(ActionBase):
raise AnsibleError('ansible_network_os must be specified on this host to use platform agnostic modules')
return network_os
def _handle_existing_file(self, conn, source, dest, proto, timeout):
if not os.path.exists(dest):
return True
cwd = self._loader.get_basedir()
filename = str(uuid.uuid4())
tmp_dest_file = os.path.join(cwd, filename)
try:
out = conn.get_file(
source=source, destination=tmp_dest_file,
proto=proto, timeout=timeout
)
except Exception as exc:
os.remove(tmp_dest_file)
raise Exception(exc)
try:
with open(tmp_dest_file, 'r') as f:
new_content = f.read()
with open(dest, '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(tmp_dest_file)
if checksum_old == checksum_new:
return False
else:
return True