roll up of fixes for sros modules (#22972)

* fixes action handlers for sros
* fixes sros_config module execution to use AnsibleModule
* fixes sros_command module to use socket connection
* adds sros to constants
This commit is contained in:
Peter Sprygada 2017-03-25 10:35:15 -04:00 committed by GitHub
commit 3169cbd493
9 changed files with 434 additions and 214 deletions

View file

@ -16,9 +16,11 @@
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['preview'],
'supported_by': 'community'}
ANSIBLE_METADATA = {
'metadata_version': '1.0',
'status': ['preview'],
'supported_by': 'community'
}
DOCUMENTATION = """
@ -203,16 +205,22 @@ updates:
description: The set of commands that will be pushed to the remote device
returned: always
type: list
sample: ['...', '...']
sample: ['config system name "sros01"']
commands:
description: The set of commands that will be pushed to the remote device
returned: always
type: list
sample: ['config system name "sros01"']
backup_path:
description: The full path to the backup file
returned: when backup is yes
type: path
sample: /playbooks/ansible/backup/sros_config.2016-07-16@22:28:34
"""
from ansible.module_utils.basic import get_exception
from ansible.module_utils.sros import NetworkModule, NetworkError
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.netcfg import NetworkConfig, dumps
from ansible.module_utils.sros import sros_argument_spec, check_args
from ansible.module_utils.sros import load_config, run_commands, get_config
def sanitize_config(lines):
commands = list()
@ -224,15 +232,17 @@ def sanitize_config(lines):
commands.append(line)
return commands
def get_config(module, result):
def get_active_config(module):
contents = module.params['config']
if not contents:
defaults = module.params['defaults']
contents = module.config.get_config(detail=defaults)
return NetworkConfig(device_os='sros', contents=contents)
flags = []
if module.params['defaults']:
flags = ['detail']
return get_config(module, flags)
return contents
def get_candidate(module):
candidate = NetworkConfig(device_os='sros')
candidate = NetworkConfig(indent=4)
if module.params['src']:
candidate.load(module.params['src'])
elif module.params['lines']:
@ -246,33 +256,23 @@ def run(module, result):
candidate = get_candidate(module)
if match != 'none':
config = get_config(module, result)
config_text = get_active_config(module)
config = NetworkConfig(indent=4, contents=config_text)
configobjs = candidate.difference(config)
else:
configobjs = candidate.items
if configobjs:
commands = dumps(configobjs, 'lines')
commands = dumps(configobjs, 'commands')
commands = sanitize_config(commands.split('\n'))
result['commands'] = commands
result['updates'] = commands
# check if creating checkpoints is possible
if not module.connection.rollback_enabled:
warn = 'Cannot create checkpoint. Please enable this feature ' \
'using the sros_rollback module. Automatic rollback ' \
'will be disabled'
result['warnings'].append(warn)
# send the configuration commands to the device and merge
# them with the current running config
if not module.check_mode:
module.config.load_config(commands)
result['changed'] = True
if module.params['save']:
if not module.check_mode:
module.config.save_config()
load_config(module, commands)
result['changed'] = True
def main():
@ -293,23 +293,30 @@ def main():
save=dict(type='bool', default=False),
)
argument_spec.update(sros_argument_spec)
mutually_exclusive = [('lines', 'src')]
module = NetworkModule(argument_spec=argument_spec,
connect_on_load=False,
module = AnsibleModule(argument_spec=argument_spec,
mutually_exclusive=mutually_exclusive,
supports_check_mode=True)
result = dict(changed=False, warnings=list())
if module.params['backup']:
result['__backup__'] = module.config.get_config()
warnings = list()
check_args(module, warnings)
if warnings:
result['warnings'] = warnings
try:
run(module, result)
except NetworkError:
exc = get_exception()
module.fail_json(msg=str(exc), **exc.kwargs)
if module.params['backup']:
result['__backup__'] = get_config(module)
run(module, result)
if module.params['save']:
if not module.check_mode:
run_commands(module, ['admin save'])
result['changed'] = True
module.exit_json(**result)