mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-24 13:50:22 -07:00
fix nxos_ntp_options (#38695)
This commit is contained in:
parent
efa7ebb8a5
commit
1d975bdc93
3 changed files with 72 additions and 54 deletions
|
@ -34,12 +34,8 @@ author:
|
||||||
- Jason Edelman (@jedelman8)
|
- Jason Edelman (@jedelman8)
|
||||||
notes:
|
notes:
|
||||||
- Tested against NXOSv 7.3.(0)D1(1) on VIRL
|
- Tested against NXOSv 7.3.(0)D1(1) on VIRL
|
||||||
- At least one of C(master) or C(logging) params must be supplied.
|
- When C(state=absent), master and logging will be set to False and
|
||||||
- When C(state=absent), boolean parameters are flipped,
|
stratum will be removed as well
|
||||||
e.g. C(master=true) will disable the authoritative server.
|
|
||||||
- When C(state=absent) and C(master=true), the stratum will be removed as well.
|
|
||||||
- When C(state=absent) and C(master=false), the stratum will be configured
|
|
||||||
to its default value, 8.
|
|
||||||
options:
|
options:
|
||||||
master:
|
master:
|
||||||
description:
|
description:
|
||||||
|
@ -75,7 +71,7 @@ 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 12"]
|
||||||
'''
|
'''
|
||||||
import re
|
import re
|
||||||
|
|
||||||
|
@ -85,20 +81,20 @@ from ansible.module_utils.basic import AnsibleModule
|
||||||
|
|
||||||
|
|
||||||
def get_current(module):
|
def get_current(module):
|
||||||
cmd = ('show running-config', 'show ntp logging')
|
cmd = ('show running-config | inc ntp')
|
||||||
|
|
||||||
output = run_commands(module, ({'command': cmd[0], 'output': 'text'},
|
master = False
|
||||||
{'command': cmd[1], 'output': 'text'}))
|
logging = False
|
||||||
|
stratum = None
|
||||||
|
|
||||||
match = re.search(r"^ntp master(?: (\d+))", output[0], re.M)
|
output = run_commands(module, ({'command': cmd, 'output': 'text'}))[0]
|
||||||
if match:
|
|
||||||
master = True
|
|
||||||
stratum = match.group(1)
|
|
||||||
else:
|
|
||||||
master = False
|
|
||||||
stratum = None
|
|
||||||
|
|
||||||
logging = 'enabled' in output[1].lower()
|
if output:
|
||||||
|
match = re.search(r"^ntp master(?: (\d+))", output, re.M)
|
||||||
|
if match:
|
||||||
|
master = True
|
||||||
|
stratum = match.group(1)
|
||||||
|
logging = 'ntp logging' in output.lower()
|
||||||
|
|
||||||
return {'master': master, 'stratum': stratum, 'logging': logging}
|
return {'master': master, 'stratum': stratum, 'logging': logging}
|
||||||
|
|
||||||
|
@ -106,7 +102,7 @@ def get_current(module):
|
||||||
def main():
|
def main():
|
||||||
argument_spec = dict(
|
argument_spec = dict(
|
||||||
master=dict(required=False, type='bool'),
|
master=dict(required=False, type='bool'),
|
||||||
stratum=dict(required=False, type='str', default='8'),
|
stratum=dict(required=False, type='str'),
|
||||||
logging=dict(required=False, type='bool'),
|
logging=dict(required=False, type='bool'),
|
||||||
state=dict(choices=['absent', 'present'], default='present'),
|
state=dict(choices=['absent', 'present'], default='present'),
|
||||||
)
|
)
|
||||||
|
@ -124,15 +120,10 @@ def main():
|
||||||
logging = module.params['logging']
|
logging = module.params['logging']
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
|
|
||||||
if stratum:
|
if stratum and master is False:
|
||||||
try:
|
if stratum != 8:
|
||||||
stratum_int = int(stratum)
|
module.fail_json(msg='master MUST be True when stratum is changed')
|
||||||
if stratum_int < 1 or stratum_int > 15:
|
|
||||||
raise ValueError
|
|
||||||
except ValueError:
|
|
||||||
module.fail_json(msg='stratum must be an integer between 1 and 15')
|
|
||||||
|
|
||||||
desired = {'master': master, 'stratum': stratum, 'logging': logging}
|
|
||||||
current = get_current(module)
|
current = get_current(module)
|
||||||
|
|
||||||
result = {'changed': False}
|
result = {'changed': False}
|
||||||
|
@ -146,19 +137,17 @@ def main():
|
||||||
commands.append('no ntp logging')
|
commands.append('no ntp logging')
|
||||||
|
|
||||||
elif state == 'present':
|
elif state == 'present':
|
||||||
if desired['master'] and desired['master'] != current['master']:
|
if master and not current['master']:
|
||||||
if desired['stratum']:
|
commands.append('ntp master')
|
||||||
commands.append('ntp master %s' % stratum)
|
elif master is False and current['master']:
|
||||||
else:
|
commands.append('no ntp master')
|
||||||
commands.append('ntp master')
|
if stratum and stratum != current['stratum']:
|
||||||
elif desired['stratum'] and desired['stratum'] != current['stratum']:
|
|
||||||
commands.append('ntp master %s' % stratum)
|
commands.append('ntp master %s' % stratum)
|
||||||
|
|
||||||
if desired['logging'] and desired['logging'] != current['logging']:
|
if logging and not current['logging']:
|
||||||
if desired['logging']:
|
commands.append('ntp logging')
|
||||||
commands.append('ntp logging')
|
elif logging is False and current['logging']:
|
||||||
else:
|
commands.append('no ntp logging')
|
||||||
commands.append('no ntp logging')
|
|
||||||
|
|
||||||
result['commands'] = commands
|
result['commands'] = commands
|
||||||
result['updates'] = commands
|
result['updates'] = commands
|
||||||
|
|
|
@ -31,20 +31,9 @@
|
||||||
that:
|
that:
|
||||||
- "result.changed == false"
|
- "result.changed == false"
|
||||||
|
|
||||||
- name: Remove ntp with master and default stratum
|
|
||||||
nxos_ntp_options: &remove_master_default_stratum
|
|
||||||
logging: true
|
|
||||||
master: true
|
|
||||||
state: absent
|
|
||||||
provider: "{{ connection }}"
|
|
||||||
register: result
|
|
||||||
|
|
||||||
- assert: *true
|
|
||||||
|
|
||||||
- name: Configure ntp with master and non-default stratum
|
- name: Configure ntp with master and non-default stratum
|
||||||
nxos_ntp_options: &configure_master_non_default_stratum
|
nxos_ntp_options: &configure_master_non_default_stratum
|
||||||
master: true
|
master: true
|
||||||
logging: true
|
|
||||||
stratum: 10
|
stratum: 10
|
||||||
state: present
|
state: present
|
||||||
provider: "{{ connection }}"
|
provider: "{{ connection }}"
|
||||||
|
@ -58,16 +47,57 @@
|
||||||
|
|
||||||
- assert: *false
|
- assert: *false
|
||||||
|
|
||||||
- name: Remove ntp with master and non-default stratum
|
- name: Configure ntp with master and no logging
|
||||||
nxos_ntp_options: &remove_master_non_default_stratum
|
nxos_ntp_options: &configure_no_log
|
||||||
logging: true
|
|
||||||
master: true
|
master: true
|
||||||
state: absent
|
stratum: 10
|
||||||
|
logging: false
|
||||||
|
state: present
|
||||||
provider: "{{ connection }}"
|
provider: "{{ connection }}"
|
||||||
register: result
|
register: result
|
||||||
|
|
||||||
- assert: *true
|
- assert: *true
|
||||||
|
|
||||||
|
- name: "Check Idempotence - Configure ntp with master and no logging"
|
||||||
|
nxos_ntp_options: *configure_no_log
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *false
|
||||||
|
|
||||||
|
- name: Configure ntp with logging and no master
|
||||||
|
nxos_ntp_options: &configure_no_master
|
||||||
|
master: false
|
||||||
|
logging: true
|
||||||
|
state: present
|
||||||
|
provider: "{{ connection }}"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *true
|
||||||
|
|
||||||
|
- name: "Check Idempotence - Configure ntp with logging and no master"
|
||||||
|
nxos_ntp_options: *configure_no_master
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *false
|
||||||
|
|
||||||
|
- name: "Configure ntp with master and non-default stratum again"
|
||||||
|
nxos_ntp_options: *configure_master_non_default_stratum
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *true
|
||||||
|
|
||||||
|
- name: Remove ntp options
|
||||||
|
nxos_ntp_options: *default
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *true
|
||||||
|
|
||||||
|
- name: "Check Idempotence - Remove"
|
||||||
|
nxos_ntp_options: *default
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *false
|
||||||
|
|
||||||
always:
|
always:
|
||||||
- name: Cleanup ntp config
|
- name: Cleanup ntp config
|
||||||
nxos_ntp_options: *default
|
nxos_ntp_options: *default
|
||||||
|
|
|
@ -1347,7 +1347,6 @@ lib/ansible/modules/network/nxos/nxos_igmp_interface.py E326
|
||||||
lib/ansible/modules/network/nxos/nxos_interface.py E324
|
lib/ansible/modules/network/nxos/nxos_interface.py E324
|
||||||
lib/ansible/modules/network/nxos/nxos_lldp.py E326
|
lib/ansible/modules/network/nxos/nxos_lldp.py E326
|
||||||
lib/ansible/modules/network/nxos/nxos_logging.py E325
|
lib/ansible/modules/network/nxos/nxos_logging.py E325
|
||||||
lib/ansible/modules/network/nxos/nxos_ntp_options.py E324
|
|
||||||
lib/ansible/modules/network/nxos/nxos_nxapi.py E324
|
lib/ansible/modules/network/nxos/nxos_nxapi.py E324
|
||||||
lib/ansible/modules/network/nxos/nxos_nxapi.py E325
|
lib/ansible/modules/network/nxos/nxos_nxapi.py E325
|
||||||
lib/ansible/modules/network/nxos/nxos_nxapi.py E326
|
lib/ansible/modules/network/nxos/nxos_nxapi.py E326
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue