Skip to content

Commit

Permalink
updates iosxr modules to support socket (ansible#21231)
Browse files Browse the repository at this point in the history
* updates all iosxr modules to support persistent socket
* adds iosxr action plugin to connect to device
* adds exec_command() to iosxr shared module
* fixes iosxr_config and iosxr_template local action
* update all unit test cases
* adds base test module for iosxr module testing
  • Loading branch information
privateip authored Feb 15, 2017
1 parent 635e3fe commit eb1453a
Show file tree
Hide file tree
Showing 17 changed files with 465 additions and 622 deletions.
41 changes: 29 additions & 12 deletions lib/ansible/module_utils/iosxr.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,29 @@
# 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 = {}

iosxr_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 iosxr_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, flags=[]):
cmd = 'show running-config '
cmd += ' '.join(flags)
Expand All @@ -37,53 +57,50 @@ def get_config(module, flags=[]):
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()
_DEVICE_CONFIGS[cmd] = cfg
return cfg

def run_commands(module, commands, check_rc=True):
assert isinstance(commands, list), 'commands must be a list'
responses = list()

for cmd in commands:
rc, out, err = module.exec_command(cmd)
for cmd in to_list(commands):
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, replace=False, comment=None):
assert isinstance(commands, list), 'commands must be a list'

rc, out, err = module.exec_command('configure terminal')
rc, out, err = exec_command(module, 'configure terminal')
if rc != 0:
module.fail_json(msg='unable to enter configuration mode', err=err)

failed = False
for command in commands:
for command in to_list(commands):
if command == 'end':
pass

rc, out, err = module.exec_command(command)
rc, out, err = exec_command(module, command)
if rc != 0:
failed = True
break

if failed:
module.exec_command('abort')
exec_command(module, 'abort')
module.fail_json(msg=err, commands=commands, rc=rc)

rc, diff, err = module.exec_command('show commit changes diff')
rc, diff, err = exec_command(module, 'show commit changes diff')
if commit:
cmd = 'commit'
if comment:
cmd += ' comment {0}'.format(comment)
else:
cmd = 'abort'
diff = None
module.exec_command(cmd)
exec_command(module, cmd)

return diff
168 changes: 0 additions & 168 deletions lib/ansible/module_utils/iosxr_cli.py

This file was deleted.

43 changes: 7 additions & 36 deletions lib/ansible/modules/network/iosxr/_iosxr_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
commands that are not already configured. The config source can
be a set of commands or a template.
deprecated: Deprecated in 2.2. Use M(iosxr_config) instead.
extends_documentation_fragment: iosxr
options:
src:
description:
Expand Down Expand Up @@ -99,39 +98,11 @@
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"
"""
from ansible.module_utils.local import LocalAnsibleModule
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import NetworkConfig, dumps
from ansible.module_utils.iosxr import get_config, load_config
from ansible.module_utils.network import NET_TRANSPORT_ARGS, _transitional_argument_spec


def check_args(module):
warnings = list()
for key in NET_TRANSPORT_ARGS:
if module.params[key]:
warnings.append(
'network provider arguments are no longer supported. Please '
'use connection: network_cli for the task'
)
break
return warnings
from ansible.module_utils.iosxr import iosxr_argument_spec, check_args


def main():
Expand All @@ -145,17 +116,16 @@ def main():
config=dict(),
)

# Removed the use of provider arguments in 2.3 due to network_cli
# connection plugin. To be removed in 2.5
argument_spec.update(_transitional_argument_spec())
argument_spec.update(iosxr_argument_spec)

mutually_exclusive = [('config', 'backup'), ('config', 'force')]

module = LocalAnsibleModule(argument_spec=argument_spec,
module = AnsibleModule(argument_spec=argument_spec,
mutually_exclusive=mutually_exclusive,
supports_check_mode=True)

warnings = check_args(module)
warnings = list()
check_args(module, warnings)

result = dict(changed=False, warnings=warnings)

Expand All @@ -178,6 +148,7 @@ def main():
result['changed'] = not module.check_mode

result['updates'] = commands
result['commands'] = commands
module.exit_json(**result)


Expand Down
27 changes: 7 additions & 20 deletions lib/ansible/modules/network/iosxr/iosxr_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,31 +126,15 @@
returned: failed
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.iosxr import run_commands
from ansible.module_utils.network_common import ComplexList
from ansible.module_utils.netcli import Conditional
from ansible.module_utils.six import string_types

VALID_KEYS = ['command', 'output', 'prompt', 'response']
from ansible.module_utils.iosxr import iosxr_argument_spec, check_args

def to_lines(stdout):
for item in stdout:
Expand Down Expand Up @@ -182,7 +166,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']),
Expand All @@ -192,10 +175,14 @@ def main():
interval=dict(default=1, type='int')
)

module = LocalAnsibleModule(argument_spec=spec,
spec.update(iosxr_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
Loading

0 comments on commit eb1453a

Please sign in to comment.