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

Make host required field and minor refactor (#24534)

* Make host required field and minor refactor

Signed-off-by: Trishna Guha <trishnaguha17@gmail.com>

* ansibot pep8 legacy file

* example doc update
This commit is contained in:
Trishna Guha 2017-05-19 22:07:01 +05:30 committed by Chris Alfonso
parent 68f38c5e9d
commit 4782a4e62f
3 changed files with 48 additions and 59 deletions

View file

@ -16,10 +16,11 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>. # along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# #
ANSIBLE_METADATA = {'metadata_version': '1.0', ANSIBLE_METADATA = {
'status': ['preview'], 'metadata_version': '1.0',
'supported_by': 'community'} 'status': ['preview'],
'supported_by': 'community'
}
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
@ -28,34 +29,34 @@ extends_documentation_fragment: nxos
version_added: "2.2" version_added: "2.2"
short_description: Copy a file to a remote NXOS device over SCP. short_description: Copy a file to a remote NXOS device over SCP.
description: description:
- Copy a file to the flash (or bootflash) remote network device - Copy a file to the flash (or bootflash) remote network device
on NXOS devices. on NXOS devices.
author: author:
- Jason Edelman (@jedelman8) - Jason Edelman (@jedelman8)
- Gabriele Gerbino (@GGabriele) - Gabriele Gerbino (@GGabriele)
notes: notes:
- The feature must be enabled with feature scp-server. - The feature must be enabled with feature scp-server.
- 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.
options: options:
local_file: local_file:
description: description:
- Path to local file. Local directory must exist. - Path to local file. Local directory must exist.
required: true required: true
remote_file: remote_file:
description: description:
- Remote file path of the copy. Remote directories must exist. - Remote file path of the copy. Remote directories must exist.
If omitted, the name of the local file will be used. If omitted, the name of the local file will be used.
required: false required: false
default: null default: null
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 file_system parameter will use
their default values. their default values.
required: false required: false
default: null default: null
''' '''
EXAMPLES = ''' EXAMPLES = '''
@ -83,12 +84,11 @@ remote_file:
type: string type: string
sample: '/path/to/remote/file' sample: '/path/to/remote/file'
''' '''
import os import os
import re import re
import time import time
import paramiko import paramiko
from ansible.module_utils.nxos import run_commands from ansible.module_utils.nxos import run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
@ -100,31 +100,20 @@ except ImportError:
HAS_SCP = False HAS_SCP = False
def execute_show_command(command, module, command_type='cli_show'):
if module.params['transport'] == 'cli':
cmds = [command]
body = run_commands(module, cmds)
elif module.params['transport'] == 'nxapi':
cmds = [command]
body = run_commands(module, cmds)
return body
def remote_file_exists(module, dst, file_system='bootflash:'): def remote_file_exists(module, dst, file_system='bootflash:'):
command = 'dir {0}/{1}'.format(file_system, dst) command = 'dir {0}/{1}'.format(file_system, dst)
body = execute_show_command(command, module, command_type='cli_show_ascii') body = run_commands(module, [command])[0]
if 'No such file' in body[0]: if 'No such file' in body:
return False return False
return True return True
def verify_remote_file_exists(module, dst, file_system='bootflash:'): def verify_remote_file_exists(module, dst, file_system='bootflash:'):
command = 'dir {0}/{1}'.format(file_system, dst) command = 'dir {0}/{1}'.format(file_system, dst)
body = execute_show_command(command, module, command_type='cli_show_ascii') body = run_commands(module, [command])[0]
if 'No such file' in body[0]: if 'No such file' in body:
return 0 return 0
return body[0].split()[0].strip() return body.split()[0].strip()
def local_file_exists(module): def local_file_exists(module):
@ -133,9 +122,9 @@ def local_file_exists(module):
def get_flash_size(module): def get_flash_size(module):
command = 'dir {}'.format(module.params['file_system']) command = 'dir {}'.format(module.params['file_system'])
body = execute_show_command(command, module, command_type='cli_show_ascii') body = run_commands(module, [command])[0]
match = re.search(r'(\d+) bytes free', body[0]) match = re.search(r'(\d+) bytes free', body)
bytes_free = match.group(1) bytes_free = match.group(1)
return int(bytes_free) return int(bytes_free)
@ -199,8 +188,7 @@ def main():
argument_spec.update(nxos_argument_spec) argument_spec.update(nxos_argument_spec)
module = AnsibleModule(argument_spec=argument_spec, module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
supports_check_mode=True)
if not HAS_SCP: if not HAS_SCP:
module.fail_json( module.fail_json(
@ -210,7 +198,6 @@ def main():
warnings = list() warnings = list()
check_args(module, warnings) check_args(module, warnings)
results = dict(changed=False, warnings=warnings) results = dict(changed=False, warnings=warnings)
local_file = module.params['local_file'] local_file = module.params['local_file']

View file

@ -63,7 +63,13 @@ class ActionModule(_ActionModule):
pc.password = provider['password'] or self._play_context.password pc.password = provider['password'] or self._play_context.password
pc.private_key_file = provider['ssh_keyfile'] or self._play_context.private_key_file pc.private_key_file = provider['ssh_keyfile'] or self._play_context.private_key_file
pc.timeout = provider['timeout'] or self._play_context.timeout pc.timeout = provider['timeout'] or self._play_context.timeout
self._task.args['provider'] = provider.update(
host=pc.remote_addr,
port=pc.port,
username=pc.remote_user,
password=pc.password,
ssh_keyfile=pc.private_key_file
)
display.vvv('using connection plugin %s' % pc.connection, pc.remote_addr) display.vvv('using connection plugin %s' % pc.connection, pc.remote_addr)
connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin) connection = self._shared_loader_obj.connection_loader.get('persistent', pc, sys.stdin)
@ -76,8 +82,8 @@ class ActionModule(_ActionModule):
display.vvvv('open_shell() returned %s %s %s' % (rc, out, err)) display.vvvv('open_shell() returned %s %s %s' % (rc, out, err))
if rc != 0: if rc != 0:
return {'failed': True, return {'failed': True,
'msg': 'unable to open shell. Please see: ' 'msg': 'unable to open shell. Please see: ' +
+ 'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell', 'https://docs.ansible.com/ansible/network_debug_troubleshooting.html#unable-to-open-shell',
'rc': rc} 'rc': rc}
else: else:
# make sure we are in the right cli context which should be # make sure we are in the right cli context which should be
@ -88,12 +94,9 @@ class ActionModule(_ActionModule):
connection.exec_command('exit') connection.exec_command('exit')
rc, out, err = connection.exec_command('prompt()') rc, out, err = connection.exec_command('prompt()')
task_vars['ansible_socket'] = socket_path task_vars['ansible_socket'] = socket_path
else: else:
provider['transport'] = 'nxapi' provider['transport'] = 'nxapi'
if provider.get('host') is None: if provider.get('host') is None:
provider['host'] = self._play_context.remote_addr provider['host'] = self._play_context.remote_addr

View file

@ -713,7 +713,6 @@ lib/ansible/plugins/action/iosxr_template.py
lib/ansible/plugins/action/junos.py lib/ansible/plugins/action/junos.py
lib/ansible/plugins/action/junos_template.py lib/ansible/plugins/action/junos_template.py
lib/ansible/plugins/action/normal.py lib/ansible/plugins/action/normal.py
lib/ansible/plugins/action/nxos.py
lib/ansible/plugins/action/nxos_template.py lib/ansible/plugins/action/nxos_template.py
lib/ansible/plugins/action/ops_config.py lib/ansible/plugins/action/ops_config.py
lib/ansible/plugins/action/ops_template.py lib/ansible/plugins/action/ops_template.py