1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

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
This commit is contained in:
Pilou 2018-01-05 07:06:11 +01:00 committed by Trishna Guha
parent 6da06fac3f
commit e9a5ff912a
2 changed files with 19 additions and 6 deletions

View file

@ -38,6 +38,8 @@ notes:
- If the file is already present (md5 sums match), no transfer will - If the file is already present (md5 sums match), no transfer will
take place. take place.
- Check mode will tell you if the file would be copied. - Check mode will tell you if the file would be copied.
requirements:
- paramiko
options: options:
local_file: local_file:
description: description:
@ -52,7 +54,7 @@ options:
file_system: file_system:
description: description:
- The remote file system of the device. If omitted, - 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. their default values.
required: false required: false
default: null default: null
@ -93,11 +95,17 @@ remote_file:
import os import os
import re import re
import time import time
import paramiko
from ansible.module_utils.network.nxos.nxos import run_commands 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.network.nxos.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
try:
import paramiko
HAS_PARAMIKO = True
except ImportError:
HAS_PARAMIKO = False
try: try:
from scp import SCPClient from scp import SCPClient
HAS_SCP = True HAS_SCP = True
@ -126,7 +134,7 @@ def local_file_exists(module):
def get_flash_size(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] body = run_commands(module, {'command': command, 'output': 'text'})[0]
match = re.search(r'(\d+) bytes free', body) match = re.search(r'(\d+) bytes free', body)
@ -165,7 +173,7 @@ def transfer_file(module, dest):
password=password, password=password,
port=port) 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()) scp = SCPClient(ssh.get_transport())
try: try:
scp.put(module.params['local_file'], full_remote_path) 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) 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: if not HAS_SCP:
module.fail_json( module.fail_json(
msg='library scp is required but does not appear to be ' msg='library scp is required but does not appear to be '
@ -216,7 +230,7 @@ def main():
results['file_system'] = file_system results['file_system'] = file_system
if not local_file_exists(module): 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) dest = remote_file or os.path.basename(local_file)
remote_exists = remote_file_exists(module, dest, file_system=file_system) remote_exists = remote_file_exists(module, dest, file_system=file_system)

View file

@ -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_domain.py
lib/ansible/modules/cloud/webfaction/webfaction_mailbox.py lib/ansible/modules/cloud/webfaction/webfaction_mailbox.py
lib/ansible/modules/cloud/webfaction/webfaction_site.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/packaging/os/yum_repository.py
lib/ansible/modules/system/hostname.py lib/ansible/modules/system/hostname.py