roll up of fixes for eos modules (#21406)

* fixes issue with load_provider() not checking for an existing key
* adds updates to eos_config results key
* lots of minor syntax fixes in eos shared module
* adds eos_argument_spec to eos_eapi

fixes #21402
pull/21423/head
Peter Sprygada 8 years ago committed by John R Barker
parent e1761d7724
commit e1a2c6e1d3

@ -27,6 +27,7 @@
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
# USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# #
import os
import re import re
import time import time
@ -166,7 +167,7 @@ class Cli:
def configure(self, commands): def configure(self, commands):
"""Sends configuration commands to the remote device """Sends configuration commands to the remote device
""" """
if not check_authorization(self): if not self.check_authorization():
self._module.fail_json(msg='configuration operations require privilege escalation') self._module.fail_json(msg='configuration operations require privilege escalation')
conn = get_connection(self) conn = get_connection(self)
@ -175,7 +176,7 @@ class Cli:
if rc != 0: if rc != 0:
self._module.fail_json(msg='unable to enter configuration mode', output=err) self._module.fail_json(msg='unable to enter configuration mode', output=err)
rc, out, err = send_config(self, commands) rc, out, err = self.send_config(commands)
if rc != 0: if rc != 0:
self._module.fail_json(msg=err) self._module.fail_json(msg=err)
@ -185,7 +186,7 @@ class Cli:
def load_config(self, commands, commit=False, replace=False): def load_config(self, commands, commit=False, replace=False):
"""Loads the config commands onto the remote device """Loads the config commands onto the remote device
""" """
if not check_authorization(self): if not self.check_authorization():
self._module.fail_json(msg='configuration operations require privilege escalation') self._module.fail_json(msg='configuration operations require privilege escalation')
use_session = os.getenv('ANSIBLE_EOS_USE_SESSIONS', True) use_session = os.getenv('ANSIBLE_EOS_USE_SESSIONS', True)
@ -194,7 +195,7 @@ class Cli:
except ValueError: except ValueError:
pass pass
if not all((bool(use_session), supports_sessions(self))): if not all((bool(use_session), self.supports_sessions())):
return configure(self, commands) return configure(self, commands)
conn = get_connection(self) conn = get_connection(self)
@ -208,10 +209,10 @@ class Cli:
if replace: if replace:
self.exec_command('rollback clean-config', check_rc=True) self.exec_command('rollback clean-config', check_rc=True)
rc, out, err = send_config(self, commands) rc, out, err = self.send_config(commands)
if rc != 0: if rc != 0:
self.exec_command('abort') self.exec_command('abort')
conn.fail_json(msg=err, commands=commands) self._module.fail_json(msg=err, commands=commands)
rc, out, err = self.exec_command('show session-config diffs') rc, out, err = self.exec_command('show session-config diffs')
if rc == 0: if rc == 0:
@ -230,7 +231,7 @@ class Eapi:
self._module = module self._module = module
self._enable = None self._enable = None
self._session_support = None self._session_support = None
self._device_config = {} self._device_configs = {}
host = module.params['host'] host = module.params['host']
port = module.params['port'] port = module.params['port']

@ -125,6 +125,7 @@ failed_conditions:
import time import time
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.pycompat24 import get_exception
from ansible.module_utils.six import string_types from ansible.module_utils.six import string_types
from ansible.module_utils.netcli import Conditional from ansible.module_utils.netcli import Conditional
from ansible.module_utils.network_common import ComplexList from ansible.module_utils.network_common import ComplexList
@ -193,7 +194,12 @@ def main():
result['warnings'] = warnings result['warnings'] = warnings
wait_for = module.params['wait_for'] or list() wait_for = module.params['wait_for'] or list()
conditionals = [Conditional(c) for c in wait_for]
try:
conditionals = [Conditional(c) for c in wait_for]
except AttributeError:
exc = get_exception()
module.fail_json(msg=str(exc))
retries = module.params['retries'] retries = module.params['retries']
interval = module.params['interval'] interval = module.params['interval']

@ -250,6 +250,7 @@ def run(module, result):
commands.extend(module.params['after']) commands.extend(module.params['after'])
result['commands'] = commands result['commands'] = commands
result['updates'] = commands
replace = module.params['replace'] == 'config' replace = module.params['replace'] == 'config'
commit = not module.check_mode commit = not module.check_mode

@ -184,6 +184,7 @@ import re
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.eos import run_commands, load_config from ansible.module_utils.eos import run_commands, load_config
from ansible.module_utils.six import iteritems from ansible.module_utils.six import iteritems
from ansible.module_utils.eos import eos_argument_spec, check_args
def validate_http_port(value, module): def validate_http_port(value, module):
if not 1 <= value <= 65535: if not 1 <= value <= 65535:
@ -198,8 +199,8 @@ def validate_local_http_port(value, module):
module.fail_json(msg='http_port must be between 1 and 65535') module.fail_json(msg='http_port must be between 1 and 65535')
def validate_vrf(value, module): def validate_vrf(value, module):
rc, out, err = run_commands(module, ['show vrf']) out = run_commands(module, ['show vrf'])
configured_vrfs = re.findall('^\s+(\w+)(?=\s)', out[0],re.M) configured_vrfs = re.findall('^\s+(\w+)(?=\s)', out[0], re.M)
configured_vrfs.append('default') configured_vrfs.append('default')
if value not in configured_vrfs: if value not in configured_vrfs:
module.fail_json(msg='vrf `%s` is not configured on the system' % value) module.fail_json(msg='vrf `%s` is not configured on the system' % value)
@ -255,7 +256,7 @@ def parse_state(data):
def map_config_to_obj(module): def map_config_to_obj(module):
rc, out, err = run_commands(module, ['show management api http-commands | json']) out = run_commands(module, ['show management api http-commands | json'])
return { return {
'http': out[0]['httpServer']['configured'], 'http': out[0]['httpServer']['configured'],
'http_port': out[0]['httpServer']['port'], 'http_port': out[0]['httpServer']['port'],
@ -290,7 +291,7 @@ def map_params_to_obj(module):
return obj return obj
def collect_facts(module, result): def collect_facts(module, result):
rc, out, err = run_commands(module, ['show management api http-commands | json']) out = run_commands(module, ['show management api http-commands | json'])
facts = dict(eos_eapi_urls=dict()) facts = dict(eos_eapi_urls=dict())
for each in out[0]['urls']: for each in out[0]['urls']:
intf, url = each.split(' : ') intf, url = each.split(' : ')
@ -320,6 +321,8 @@ def main():
state=dict(default='started', choices=['stopped', 'started']), state=dict(default='started', choices=['stopped', 'started']),
) )
argument_spec.update(eos_argument_spec)
module = AnsibleModule(argument_spec=argument_spec, module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True) supports_check_mode=True)

@ -143,6 +143,7 @@ import re
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.network_common import ComplexList from ansible.module_utils.network_common import ComplexList
from ansible.module_utils.eos import load_config, get_config from ansible.module_utils.eos import load_config, get_config
from ansible.module_utils.eos import eos_argument_spec
_CONFIGURED_VRFS = None _CONFIGURED_VRFS = None
@ -304,6 +305,8 @@ def main():
state=dict(default='present', choices=['present', 'absent']) state=dict(default='present', choices=['present', 'absent'])
) )
argument_spec.update(eos_argument_spec)
module = AnsibleModule(argument_spec=argument_spec, module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True) supports_check_mode=True)

@ -85,14 +85,13 @@ class ActionModule(_ActionModule):
def load_provider(self): def load_provider(self):
provider = self._task.args.get('provider', {}) provider = self._task.args.get('provider', {})
for key, value in iteritems(eos_argument_spec): for key, value in iteritems(eos_argument_spec):
if key == 'provider': if key != 'provider' and key not in provider:
continue if key in self._task.args:
elif key in self._task.args: provider[key] = self._task.args[key]
provider[key] = self._task.args[key] elif 'fallback' in value:
elif 'fallback' in value: provider[key] = self._fallback(value['fallback'])
provider[key] = self._fallback(value['fallback']) elif key not in provider:
elif key not in provider: provider[key] = None
provider[key] = None
return provider return provider
def _fallback(self, fallback): def _fallback(self, fallback):

@ -50,7 +50,7 @@ class TestEosEapiModule(TestEosModule):
output = list() output = list()
for cmd in commands: for cmd in commands:
output.append(load_fixture(self.command_fixtures[cmd])) output.append(load_fixture(self.command_fixtures[cmd]))
return (0, output, '') return output
self.run_commands.side_effect = run_commands self.run_commands.side_effect = run_commands
self.load_config.return_value = dict(diff=None, session='session') self.load_config.return_value = dict(diff=None, session='session')
@ -86,9 +86,9 @@ class TestEosEapiModule(TestEosModule):
self.start_configured(changed=True, commands=commands) self.start_configured(changed=True, commands=commands)
def test_eos_eapi_http_invalid(self): def test_eos_eapi_http_invalid(self):
set_module_args(dict(port=80000)) set_module_args(dict(http_port=80000))
commands = [] commands = []
self.start_unconfigured(failed=True) self.start_unconfigured(failed=True, commands=commands)
def test_eos_eapi_https_enable(self): def test_eos_eapi_https_enable(self):
set_module_args(dict(https=True)) set_module_args(dict(https=True))

Loading…
Cancel
Save