From 981265ac8435407d04e143806a561186c4ef9150 Mon Sep 17 00:00:00 2001 From: Peter Sprygada Date: Tue, 19 Jan 2016 08:38:38 -0500 Subject: [PATCH] adds provider argument to nxos shared module The provider argument accepts the set of device common arguments as a dict object. Individual connection arguments can still be included and take priority over the provider argument. This update includes additions to the nxos doc fragment --- lib/ansible/module_utils/nxos.py | 27 ++++++++++--------- .../utils/module_docs_fragments/nxos.py | 7 +++++ 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/ansible/module_utils/nxos.py b/lib/ansible/module_utils/nxos.py index 41e4269ade..d8eb0f97de 100644 --- a/lib/ansible/module_utils/nxos.py +++ b/lib/ansible/module_utils/nxos.py @@ -24,7 +24,8 @@ NET_COMMON_ARGS = dict( username=dict(required=True), password=dict(no_log=True), transport=dict(choices=['cli', 'nxapi']), - use_ssl=dict(default=False, type='bool') + use_ssl=dict(default=False, type='bool'), + provider=dict() ) NXAPI_COMMAND_TYPES = ['cli_show', 'cli_show_ascii', 'cli_conf', 'bash'] @@ -131,10 +132,10 @@ class Cli(object): def send(self, commands, encoding='text'): return self.shell.send(commands) -class NxosModule(AnsibleModule): +class NetworkModule(AnsibleModule): def __init__(self, *args, **kwargs): - super(NxosModule, self).__init__(*args, **kwargs) + super(NetworkModule, self).__init__(*args, **kwargs) self.connection = None self._config = None @@ -144,6 +145,14 @@ class NxosModule(AnsibleModule): self._config = self.get_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): if self.params['transport'] == 'nxapi': self.connection = Nxapi(self) @@ -191,26 +200,18 @@ class NxosModule(AnsibleModule): return resp['ins_api']['outputs']['output']['body'] def get_module(**kwargs): - """Return instance of NxosModule + """Return instance of NetworkModule """ - argument_spec = NET_COMMON_ARGS.copy() if kwargs.get('argument_spec'): argument_spec.update(kwargs['argument_spec']) kwargs['argument_spec'] = argument_spec - kwargs['check_invalid_arguments'] = False - module = NxosModule(**kwargs) + module = NetworkModule(**kwargs) # HAS_PARAMIKO is set by module_utils/shell.py if module.params['transport'] == 'cli' and not HAS_PARAMIKO: 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() - return module diff --git a/lib/ansible/utils/module_docs_fragments/nxos.py b/lib/ansible/utils/module_docs_fragments/nxos.py index 37d287ea72..26312155c4 100644 --- a/lib/ansible/utils/module_docs_fragments/nxos.py +++ b/lib/ansible/utils/module_docs_fragments/nxos.py @@ -65,5 +65,12 @@ options: required: false default: false choices: BOOLEANS + provider: + description: + - Convience method that allows all M(nxos) 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 """