Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updates vyos modules to use socket connection #21228

Merged
merged 1 commit into from
Feb 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 31 additions & 15 deletions lib/ansible/module_utils/vyos.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,36 @@
# 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.
#
from ansible.module_utils.basic import env_fallback
from ansible.module_utils.network_common import to_list
from ansible.module_utils.connection import exec_command

_DEVICE_CONFIGS = {}

vyos_argument_spec = {
'host': dict(),
'port': dict(type='int'),
'username': dict(fallback=(env_fallback, ['ANSIBLE_NET_USERNAME'])),
'password': dict(fallback=(env_fallback, ['ANSIBLE_NET_PASSWORD']), no_log=True),
'ssh_keyfile': dict(fallback=(env_fallback, ['ANSIBLE_NET_SSH_KEYFILE']), type='path'),
'timeout': dict(type='int', default=10),
'provider': dict(type='dict'),
}

def check_args(module, warnings):
provider = module.params['provider'] or {}
for key in vyos_argument_spec:
if key != 'provider' and module.params[key]:
warnings.append('argument %s has been deprecated and will be '
'removed in a future version' % key)

def get_config(module, target='commands'):
cmd = ' '.join(['show configuration', target])

try:
return _DEVICE_CONFIGS[cmd]
except KeyError:
rc, out, err = module.exec_command(cmd)
rc, out, err = exec_command(module, cmd)
if rc != 0:
module.fail_json(msg='unable to retrieve current config', stderr=err)
cfg = str(out).strip()
Expand All @@ -43,46 +63,42 @@ def get_config(module, target='commands'):

def run_commands(module, commands, check_rc=True):
responses = list()

for cmd in to_list(commands):
rc, out, err = module.exec_command(cmd)
rc, out, err = exec_command(module, cmd)
if check_rc and rc != 0:
module.fail_json(msg=err, rc=rc)
responses.append(out)
return responses

def load_config(module, commands, commit=False, comment=None, save=False):
rc, out, err = module.exec_command('configure')
def load_config(module, commands, commit=False, comment=None):
rc, out, err = exec_command(module, 'configure')
if rc != 0:
module.fail_json(msg='unable to enter configuration mode', output=err)

for cmd in to_list(commands):
rc, out, err = module.exec_command(cmd, check_rc=False)
rc, out, err = exec_command(module, cmd, check_rc=False)
if rc != 0:
# discard any changes in case of failure
module.exec_command('exit discard')
exec_command(module, 'exit discard')
module.fail_json(msg='configuration failed')

diff = None
if module._diff:
rc, out, err = module.exec_command('compare')
rc, out, err = exec_command(module, 'compare')
if not out.startswith('No changes'):
rc, out, err = module.exec_command('show')
rc, out, err = exec_command(module, 'show')
diff = str(out).strip()

if commit:
cmd = 'commit'
if comment:
cmd += ' comment "%s"' % comment
module.exec_command(cmd)

if save:
module.exec_command(cmd)
exec_command(module, cmd)

if not commit:
module.exec_command('exit discard')
exec_command(module, 'exit discard')
else:
module.exec_command('exit')
exec_command(module, 'exit')

if diff:
return diff
161 changes: 0 additions & 161 deletions lib/ansible/module_utils/vyos_cli.py

This file was deleted.

33 changes: 7 additions & 26 deletions lib/ansible/modules/network/vyos/vyos_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,32 +130,15 @@
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:
Expand All @@ -170,25 +153,20 @@ 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


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']),
Expand All @@ -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()
Expand Down
27 changes: 9 additions & 18 deletions lib/ansible/modules/network/vyos/vyos_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,27 +121,13 @@
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'
Expand Down Expand Up @@ -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)
Expand Down