From e9a5ff912a1e2bc2fc42e8b2f6a82d14e6f7f6dc Mon Sep 17 00:00:00 2001 From: Pilou Date: Fri, 5 Jan 2018 07:06:11 +0100 Subject: [PATCH] nxos_file_copy: fix broken import and 2.6 compatibility (#34480) * Fix broken import: paramiko could be missing * doc: use formatting function * Use Python 2.6 compatible format string --- .../modules/network/nxos/nxos_file_copy.py | 24 +++++++++++++++---- test/sanity/import/skip.txt | 1 - 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/lib/ansible/modules/network/nxos/nxos_file_copy.py b/lib/ansible/modules/network/nxos/nxos_file_copy.py index c8c163aa10..8da6d8455a 100644 --- a/lib/ansible/modules/network/nxos/nxos_file_copy.py +++ b/lib/ansible/modules/network/nxos/nxos_file_copy.py @@ -38,6 +38,8 @@ notes: - If the file is already present (md5 sums match), no transfer will take place. - Check mode will tell you if the file would be copied. +requirements: + - paramiko options: local_file: description: @@ -52,7 +54,7 @@ options: file_system: description: - The remote file system of the device. If omitted, - devices that support a file_system parameter will use + devices that support a I(file_system) parameter will use their default values. required: false default: null @@ -93,11 +95,17 @@ remote_file: import os import re import time -import paramiko + 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 +try: + import paramiko + HAS_PARAMIKO = True +except ImportError: + HAS_PARAMIKO = False + try: from scp import SCPClient HAS_SCP = True @@ -126,7 +134,7 @@ def local_file_exists(module): def get_flash_size(module): - command = 'dir {}'.format(module.params['file_system']) + command = 'dir {0}'.format(module.params['file_system']) body = run_commands(module, {'command': command, 'output': 'text'})[0] match = re.search(r'(\d+) bytes free', body) @@ -165,7 +173,7 @@ def transfer_file(module, dest): password=password, port=port) - full_remote_path = '{}{}'.format(module.params['file_system'], dest) + full_remote_path = '{0}{1}'.format(module.params['file_system'], dest) scp = SCPClient(ssh.get_transport()) try: scp.put(module.params['local_file'], full_remote_path) @@ -197,6 +205,12 @@ def main(): module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) + if not HAS_PARAMIKO: + module.fail_json( + msg='library paramiko is required but does not appear to be ' + 'installed. It can be installed using `pip install paramiko`' + ) + if not HAS_SCP: module.fail_json( msg='library scp is required but does not appear to be ' @@ -216,7 +230,7 @@ def main(): results['file_system'] = file_system if not local_file_exists(module): - module.fail_json(msg="Local file {} not found".format(local_file)) + module.fail_json(msg="Local file {0} not found".format(local_file)) dest = remote_file or os.path.basename(local_file) remote_exists = remote_file_exists(module, dest, file_system=file_system) diff --git a/test/sanity/import/skip.txt b/test/sanity/import/skip.txt index 0eff96b68d..5dabb92dd0 100644 --- a/test/sanity/import/skip.txt +++ b/test/sanity/import/skip.txt @@ -6,6 +6,5 @@ lib/ansible/modules/cloud/webfaction/webfaction_db.py lib/ansible/modules/cloud/webfaction/webfaction_domain.py lib/ansible/modules/cloud/webfaction/webfaction_mailbox.py lib/ansible/modules/cloud/webfaction/webfaction_site.py -lib/ansible/modules/network/nxos/nxos_file_copy.py lib/ansible/modules/packaging/os/yum_repository.py lib/ansible/modules/system/hostname.py