adds provider argument to junos shared module

This commit adds a new argument `provider` to the junos shared module.  The
argument allows the set of common connection args to be passed to the
junos shared module.  This commit also updates the junos doc fragment
This commit is contained in:
Peter Sprygada 2016-01-19 12:57:14 -05:00
parent 5144ee226e
commit 33d390fb58
2 changed files with 26 additions and 23 deletions

View file

@ -21,7 +21,8 @@ NET_COMMON_ARGS = dict(
host=dict(required=True), host=dict(required=True),
port=dict(default=22, type='int'), port=dict(default=22, type='int'),
username=dict(required=True), username=dict(required=True),
password=dict(no_log=True) password=dict(no_log=True),
provider=dict()
) )
def to_list(val): def to_list(val):
@ -52,10 +53,10 @@ class Cli(object):
return self.shell.send(commands) return self.shell.send(commands)
class JunosModule(AnsibleModule): class NetworkModule(AnsibleModule):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(JunosModule, self).__init__(*args, **kwargs) super(NetworkModule, self).__init__(*args, **kwargs)
self.connection = None self.connection = None
self._config = None self._config = None
@ -65,14 +66,19 @@ class JunosModule(AnsibleModule):
self._config = self.get_config() self._config = self.get_config()
return self._config return self._config
def _load_params(self):
params = super(NetworkModule, self)._load_params()
provider = params.get('provider') or dict()
for key, value in provider.items():
if key in NET_COMMON_ARGS.keys():
params[key] = value
return params
def connect(self): def connect(self):
try:
self.connection = Cli(self) self.connection = Cli(self)
self.connection.connect() self.connection.connect()
self.execute('cli') self.execute('cli')
self.execute('set cli screen-length 0') self.execute('set cli screen-length 0')
except ShellErrror, exc:
self.fail_json(msg=str(exc))
def configure(self, commands): def configure(self, commands):
commands = to_list(commands) commands = to_list(commands)
@ -84,10 +90,7 @@ class JunosModule(AnsibleModule):
return responses return responses
def execute(self, commands, **kwargs): def execute(self, commands, **kwargs):
try:
return self.connection.send(commands) return self.connection.send(commands)
except ShellError, exc:
self.fail_json(msg=exc.message)
def disconnect(self): def disconnect(self):
self.connection.close() self.connection.close()
@ -100,27 +103,20 @@ class JunosModule(AnsibleModule):
return self.execute(cmd)[0] return self.execute(cmd)[0]
def get_module(**kwargs): def get_module(**kwargs):
"""Return instance of JunosModule """Return instance of NetworkModule
""" """
argument_spec = NET_COMMON_ARGS.copy() argument_spec = NET_COMMON_ARGS.copy()
if kwargs.get('argument_spec'): if kwargs.get('argument_spec'):
argument_spec.update(kwargs['argument_spec']) argument_spec.update(kwargs['argument_spec'])
kwargs['argument_spec'] = argument_spec kwargs['argument_spec'] = argument_spec
kwargs['check_invalid_arguments'] = False kwargs['check_invalid_arguments'] = False
module = JunosModule(**kwargs) module = NetworkModule(**kwargs)
# HAS_PARAMIKO is set by module_utils/shell.py # HAS_PARAMIKO is set by module_utils/shell.py
if not HAS_PARAMIKO: if not HAS_PARAMIKO:
module.fail_json(msg='paramiko is required but does not appear to be installed') module.fail_json(msg='paramiko is required but does not appear to be installed')
# copy in values from local action.
params = json_dict_unicode_to_bytes(json.loads(MODULE_COMPLEX_ARGS))
for key, value in params.iteritems():
module.params[key] = value
module.connect() module.connect()
return module return module

View file

@ -48,5 +48,12 @@ options:
the SSH session the SSH session
required: false required: false
default: null default: null
provider:
description:
- Convience method that allows all M(ios) arguments to be passed as
a dict object. All constraints (required, choices, etc) must be
met either by individual arguments or values in this dict.
required: false
default: null
""" """