pull/27904/merge
Peter Sprygada 7 years ago committed by Chris Alfonso
parent ccc68f7157
commit 0b67ef4232

@ -77,144 +77,36 @@ EXAMPLES = '''
''' '''
RETURN = ''' RETURN = '''
proposed:
description: k/v pairs of parameters passed into module
returned: always
type: dict
sample: {"logging": false, "master": true, "stratum": "11"}
existing:
description:
- k/v pairs of existing ntp options
returned: always
type: dict
sample: {"logging": true, "master": true, "stratum": "8"}
end_state:
description: k/v pairs of ntp options after module execution
returned: always
type: dict
sample: {"logging": false, "master": true, "stratum": "11"}
updates: updates:
description: command sent to the device description: command sent to the device
returned: always returned: always
type: list type: list
sample: ["no ntp logging", "ntp master 11"] sample: ["no ntp logging", "ntp master 11"]
changed:
description: check to see if a change was made on the device
returned: always
type: boolean
sample: true
''' '''
import re
from ansible.module_utils.nxos import get_config, load_config, run_commands from ansible.module_utils.nxos import get_config, load_config, run_commands
from ansible.module_utils.nxos import nxos_argument_spec, check_args from ansible.module_utils.nxos import nxos_argument_spec, check_args
from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.basic import AnsibleModule
import re def get_current(module):
cmd = ('show running-config', 'show ntp logging')
def execute_show_command(command, module, command_type='cli_show'):
if module.params['transport'] == 'cli':
if 'show run' not in command:
command += ' | json'
cmds = [command]
body = run_commands(module, cmds)
elif module.params['transport'] == 'nxapi':
cmds = [command]
body = run_commands(module, cmds)
return body
def flatten_list(command_lists):
flat_command_list = []
for command in command_lists:
if isinstance(command, list):
flat_command_list.extend(command)
else:
flat_command_list.append(command)
return flat_command_list
def apply_key_map(key_map, table):
new_dict = {}
for key, value in table.items():
new_key = key_map.get(key)
if new_key:
value = table.get(key)
if value:
new_dict[new_key] = str(value)
else:
new_dict[new_key] = value
return new_dict
output = run_commands(module, ({'command': cmd[0], 'output': 'text'},
{'command': cmd[1], 'output': 'text'}))
def get_ntp_master(module): match = re.search("^ntp master(?: (\d+))", output[0], re.M)
command = 'show run | inc ntp.master' if match:
master_string = execute_show_command(command, module, command_type='cli_show_ascii') master = True
stratum = match.group(1)
if master_string:
if master_string[0]:
master = True
else:
master = False
else: else:
master = False master = False
if master is True:
stratum = str(master_string[0].split()[2])
else:
stratum = None stratum = None
return master, stratum logging = 'Enabled' in output[1]
def get_ntp_log(module):
command = 'show ntp logging'
body = execute_show_command(command, module)[0]
logging_string = body['loggingstatus']
if 'enabled' in logging_string:
ntp_log = True
else:
ntp_log = False
return ntp_log
def get_ntp_options(module): return {'master': master, 'stratum': stratum, 'logging': logging}
existing = {}
existing['logging'] = get_ntp_log(module)
existing['master'], existing['stratum'] = get_ntp_master(module)
return existing
def config_ntp_options(delta, flip=False):
master = delta.get('master')
stratum = delta.get('stratum')
log = delta.get('logging')
ntp_cmds = []
if flip:
log = not log
master = not master
if log is not None:
if log is True:
ntp_cmds.append('ntp logging')
elif log is False:
ntp_cmds.append('no ntp logging')
if master is not None:
if master is True:
if not stratum:
stratum = ''
ntp_cmds.append('ntp master {0}'.format(stratum))
elif master is False:
ntp_cmds.append('no ntp master')
return ntp_cmds
def main(): def main():
@ -227,84 +119,69 @@ def main():
argument_spec.update(nxos_argument_spec) argument_spec.update(nxos_argument_spec)
required_together = [('master', 'stratum')]
module = AnsibleModule(argument_spec=argument_spec, module = AnsibleModule(argument_spec=argument_spec,
required_one_of=[['master', 'logging']], required_together=required_together,
supports_check_mode=True) supports_check_mode=True)
warnings = list() warnings = list()
check_args(module, warnings) check_args(module, warnings)
master = module.params['master'] master = module.params['master']
stratum = module.params['stratum'] stratum = module.params['stratum']
logging = module.params['logging'] logging = module.params['logging']
state = module.params['state'] state = module.params['state']
if stratum: if stratum:
if master is None:
module.fail_json(msg='The master param must be supplied when '
'stratum is supplied')
try: try:
stratum_int = int(stratum) stratum_int = int(stratum)
if stratum_int < 1 or stratum_int > 15: if stratum_int < 1 or stratum_int > 15:
raise ValueError raise ValueError
except ValueError: except ValueError:
module.fail_json(msg='Stratum must be an integer between 1 and 15') module.fail_json(msg='stratum must be an integer between 1 and 15')
existing = get_ntp_options(module) desired = {'master': master, 'stratum': stratum, 'logging': logging}
end_state = existing current = get_current(module)
args = dict(master=master, stratum=stratum, logging=logging) result = {'changed': False}
changed = False commands = list()
proposed = dict((k, v) for k, v in args.items() if v is not None)
if master is False: if state == 'absent':
proposed['stratum'] = None if current['master']:
stratum = None commands.append('no ntp master')
if current['logging']:
commands.append('no ntp logging')
delta = dict(set(proposed.items()).difference(existing.items())) elif state == 'present':
delta_stratum = delta.get('stratum') if desired['master'] and desired['master'] != current['master']:
if desired['stratum']:
if delta_stratum: commands.append('ntp master %s' % stratum)
delta['master'] = True else:
commands.append('ntp master')
commands = [] elif desired['stratum'] and desired['stratum'] != current['stratum']:
if state == 'present': commands.append('ntp master %s' % stratum)
if delta:
command = config_ntp_options(delta) if desired['logging'] and desired['logging'] != current['logging']:
if command: if desired['logging']:
commands.append(command) commands.append('ntp logging')
elif state == 'absent': else:
if existing: commands.append('no ntp logging')
isection = dict(set(proposed.items()).intersection(
existing.items()))
command = config_ntp_options(isection, flip=True) result['commands'] = commands
if command: result['updates'] = commands
commands.append(command)
if commands:
cmds = flatten_list(commands) if not module.check_mode:
if cmds: load_config(module, commands)
if module.check_mode: result['changed'] = True
module.exit_json(changed=True, commands=cmds)
else: result['warnings'] = warnings
changed = True
load_config(module, cmds) module.exit_json(**result)
end_state = get_ntp_options(module)
if 'configure' in cmds:
cmds.pop(0)
results = {}
results['proposed'] = proposed
results['existing'] = existing
results['updates'] = cmds
results['changed'] = changed
results['warnings'] = warnings
results['end_state'] = end_state
module.exit_json(**results)
if __name__ == '__main__': if __name__ == '__main__':
main() main()

Loading…
Cancel
Save