Add md5sum check in nxos_file_copy module (#43423)
* Add md5sum check in nxos_file_copy module Signed-off-by: Trishna Guha <trishnaguha17@gmail.com> * address review comment Signed-off-by: Trishna Guha <trishnaguha17@gmail.com>
This commit is contained in:
parent
12e7e9650e
commit
fee4c24ad4
1 changed files with 25 additions and 2 deletions
|
@ -146,6 +146,7 @@ remote_file:
|
|||
sample: '/path/to/remote/file'
|
||||
'''
|
||||
|
||||
import hashlib
|
||||
import os
|
||||
import re
|
||||
import time
|
||||
|
@ -154,7 +155,7 @@ import traceback
|
|||
from ansible.module_utils.network.nxos.nxos import run_commands
|
||||
from ansible.module_utils.network.nxos.nxos import nxos_argument_spec, check_args
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible.module_utils._text import to_native, to_text
|
||||
from ansible.module_utils._text import to_native, to_text, to_bytes
|
||||
|
||||
try:
|
||||
import paramiko
|
||||
|
@ -175,12 +176,34 @@ except ImportError:
|
|||
HAS_PEXPECT = False
|
||||
|
||||
|
||||
def md5sum_check(module, dst, file_system):
|
||||
command = 'show file {0}{1} md5sum'.format(file_system, dst)
|
||||
remote_filehash = run_commands(module, {'command': command, 'output': 'text'})[0]
|
||||
remote_filehash = to_bytes(remote_filehash, errors='surrogate_or_strict')
|
||||
|
||||
local_file = module.params['local_file']
|
||||
try:
|
||||
with open(local_file, 'r') as f:
|
||||
filecontent = f.read()
|
||||
except (OSError, IOError) as exc:
|
||||
module.fail_json(msg="Error reading the file: %s" % to_text(exc))
|
||||
|
||||
filecontent = to_bytes(filecontent, errors='surrogate_or_strict')
|
||||
local_filehash = hashlib.md5(filecontent).hexdigest()
|
||||
|
||||
if local_filehash == remote_filehash:
|
||||
return True
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def remote_file_exists(module, dst, file_system='bootflash:'):
|
||||
command = 'dir {0}/{1}'.format(file_system, dst)
|
||||
body = run_commands(module, {'command': command, 'output': 'text'})[0]
|
||||
if 'No such file' in body:
|
||||
return False
|
||||
return True
|
||||
else:
|
||||
return md5sum_check(module, dst, file_system)
|
||||
|
||||
|
||||
def verify_remote_file_exists(module, dst, file_system='bootflash:'):
|
||||
|
|
Loading…
Reference in a new issue