updates vyos modules to use socket connection (#21228)

* updates all vyos modules to use socket connection
* adds vyos local action handler
* adds exec_command() to vyos
* updates vyos_config local action
* update unit test cases
* add base class for testing vyos modules
This commit is contained in:
Peter Sprygada 2017-02-13 10:41:22 -05:00 committed by Nathaniel Case
commit 8adb108aa9
14 changed files with 426 additions and 547 deletions

View file

@ -130,32 +130,15 @@ warnings:
returned: always
type: list
sample: ['...', '...']
start:
description: The time the job started
returned: always
type: str
sample: "2016-11-16 10:38:15.126146"
end:
description: The time the job ended
returned: always
type: str
sample: "2016-11-16 10:38:25.595612"
delta:
description: The time elapsed to perform all operations
returned: always
type: str
sample: "0:00:10.469466"
"""
import time
from ansible.module_utils.local import LocalAnsibleModule
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcli import Conditional
from ansible.module_utils.network_common import ComplexList
from ansible.module_utils.six import string_types
from ansible.module_utils.vyos import run_commands
VALID_KEYS = ['command', 'output', 'prompt', 'response']
from ansible.module_utils.vyos import vyos_argument_spec, check_args
def to_lines(stdout):
for item in stdout:
@ -170,17 +153,13 @@ def parse_commands(module, warnings):
prompt=dict(),
response=dict(),
))
commands = command(module.params['commands'])
for index, cmd in enumerate(commands):
if module.check_mode and not cmd['command'].startswith('show'):
warnings.append('only show commands are supported when using '
'check mode, not executing `%s`' % cmd['command'])
else:
if cmd['command'].startswith('conf'):
module.fail_json(msg='vyos_command does not support running '
'config mode commands. Please use '
'vyos_config instead')
commands[index] = module.jsonify(cmd)
return commands
@ -188,7 +167,6 @@ def parse_commands(module, warnings):
def main():
spec = dict(
# { command: <str>, output: <str>, prompt: <str>, response: <str> }
commands=dict(type='list', required=True),
wait_for=dict(type='list', aliases=['waitfor']),
@ -198,10 +176,13 @@ def main():
interval=dict(default=1, type='int')
)
module = LocalAnsibleModule(argument_spec=spec, supports_check_mode=True)
spec.update(vyos_argument_spec)
module = AnsibleModule(argument_spec=spec, supports_check_mode=True)
warnings = list()
check_args(module, warnings)
commands = parse_commands(module, warnings)
wait_for = module.params['wait_for'] or list()

View file

@ -121,27 +121,13 @@ filtered:
returned: always
type: list
sample: ['...', '...']
start:
description: The time the job started
returned: always
type: str
sample: "2016-11-16 10:38:15.126146"
end:
description: The time the job ended
returned: always
type: str
sample: "2016-11-16 10:38:25.595612"
delta:
description: The time elapsed to perform all operations
returned: always
type: str
sample: "0:00:10.469466"
"""
import re
from ansible.module_utils.local import LocalAnsibleModule
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import NetworkConfig
from ansible.module_utils.vyos import load_config, get_config, run_commands
from ansible.module_utils.vyos import vyos_argument_spec, check_args
DEFAULT_COMMENT = 'configured by vyos_config'
@ -262,15 +248,20 @@ def main():
save=dict(type='bool', default=False),
)
argument_spec.update(vyos_argument_spec)
mutually_exclusive = [('lines', 'src')]
module = LocalAnsibleModule(
module = AnsibleModule(
argument_spec=argument_spec,
mutually_exclusive=mutually_exclusive,
supports_check_mode=True
)
result = dict(changed=False, warnings=[])
warnings = list()
check_args(module, warnings)
result = dict(changed=False, warnings=warnings)
if module.params['backup']:
result['__backup__'] = get_config(module=module)

View file

@ -96,9 +96,10 @@ ansible_net_gather_subset:
"""
import re
from ansible.module_utils.local import LocalAnsibleModule
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.six import iteritems
from ansible.module_utils.vyos import run_commands
from ansible.module_utils.vyos import vyos_argument_spec, check_args
class FactsBase(object):
@ -251,7 +252,13 @@ def main():
gather_subset=dict(default=['!config'], type='list')
)
module = LocalAnsibleModule(argument_spec=argument_spec, supports_check_mode=True)
argument_spec.update(vyos_argument_spec)
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)
warnings = list()
check_args(module, warnings)
gather_subset = module.params['gather_subset']
@ -303,7 +310,7 @@ def main():
key = 'ansible_net_%s' % key
ansible_facts[key] = value
module.exit_json(ansible_facts=ansible_facts)
module.exit_json(ansible_facts=ansible_facts, warnings=warnings)
if __name__ == '__main__':

View file

@ -72,21 +72,6 @@ commands:
sample:
- set system hostname vyos01
- set system domain-name foo.example.com
start:
description: The time the job started
returned: always
type: str
sample: "2016-11-16 10:38:15.126146"
end:
description: The time the job ended
returned: always
type: str
sample: "2016-11-16 10:38:25.595612"
delta:
description: The time elapsed to perform all operations
returned: always
type: str
sample: "0:00:10.469466"
"""
EXAMPLES = """
@ -112,8 +97,9 @@ EXAMPLES = """
- sub2.example.com
"""
from ansible.module_utils.local import LocalAnsibleModule
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.vyos import get_config, load_config
from ansible.module_utils.vyos import vyos_argument_spec, check_args
def spec_key_to_device_key(key):
@ -181,6 +167,15 @@ def spec_to_commands(want, have):
return commands
def map_param_to_obj(module):
return {
'host_name': module.params['host_name'],
'domain_name': module.params['domain_name'],
'domain_search': module.params['domain_search'],
'name_server': module.params['name_server'],
'state': module.params['state']
}
def main():
argument_spec = dict(
@ -191,14 +186,20 @@ def main():
state=dict(type='str', default='present', choices=['present', 'absent']),
)
module = LocalAnsibleModule(
argument_spec.update(vyos_argument_spec)
module = AnsibleModule(
argument_spec=argument_spec,
supports_check_mode=True,
mutually_exclusive=[('domain_name', 'domain_search')],
)
result = {'changed': False}
want = dict(module.params)
warnings = list()
check_args(module, warnings)
result = {'changed': False, 'warnings': warnings}
want = map_param_to_obj(module)
have = config_to_dict(module)
commands = spec_to_commands(want, have)