mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
fix nxos_ntp issues (#39178)
* fix nxos_ntp issues * review comments * fix idempotent fail case
This commit is contained in:
parent
08f071eb7a
commit
2f99a17856
2 changed files with 96 additions and 11 deletions
|
@ -31,7 +31,7 @@ options:
|
||||||
key_id:
|
key_id:
|
||||||
description:
|
description:
|
||||||
- Authentication key identifier to use with
|
- Authentication key identifier to use with
|
||||||
given NTP server or peer.
|
given NTP server or peer or keyword 'default'.
|
||||||
prefer:
|
prefer:
|
||||||
description:
|
description:
|
||||||
- Makes given NTP server or peer the preferred
|
- Makes given NTP server or peer the preferred
|
||||||
|
@ -40,14 +40,16 @@ options:
|
||||||
vrf_name:
|
vrf_name:
|
||||||
description:
|
description:
|
||||||
- Makes the device communicate with the given
|
- Makes the device communicate with the given
|
||||||
NTP server or peer over a specific VRF.
|
NTP server or peer over a specific VRF or
|
||||||
|
keyword 'default'.
|
||||||
source_addr:
|
source_addr:
|
||||||
description:
|
description:
|
||||||
- Local source address from which NTP messages are sent.
|
- Local source address from which NTP messages are sent
|
||||||
|
or keyword 'default'
|
||||||
source_int:
|
source_int:
|
||||||
description:
|
description:
|
||||||
- Local source interface from which NTP messages are sent.
|
- Local source interface from which NTP messages are sent.
|
||||||
Must be fully qualified interface name.
|
Must be fully qualified interface name or keyword 'default'
|
||||||
state:
|
state:
|
||||||
description:
|
description:
|
||||||
- Manage the state of the resource.
|
- Manage the state of the resource.
|
||||||
|
@ -171,10 +173,12 @@ def get_ntp_peer(module):
|
||||||
|
|
||||||
split_ntp = ntp.splitlines()
|
split_ntp = ntp.splitlines()
|
||||||
for peer_line in split_ntp:
|
for peer_line in split_ntp:
|
||||||
|
if 'access-group' in peer_line:
|
||||||
|
continue
|
||||||
ntp_peer = {}
|
ntp_peer = {}
|
||||||
try:
|
try:
|
||||||
peer_address = None
|
peer_address = None
|
||||||
vrf_name = None
|
vrf_name = 'default'
|
||||||
prefer = None
|
prefer = None
|
||||||
key_id = None
|
key_id = None
|
||||||
match_ntp = re.match(ntp_regex, peer_line, re.DOTALL)
|
match_ntp = re.match(ntp_regex, peer_line, re.DOTALL)
|
||||||
|
@ -246,11 +250,18 @@ def set_ntp_server_peer(peer_type, address, prefer, key_id, vrf_name):
|
||||||
|
|
||||||
|
|
||||||
def config_ntp(delta, existing):
|
def config_ntp(delta, existing):
|
||||||
address = delta.get('address', existing.get('address'))
|
if (delta.get('address') or delta.get('peer_type') or delta.get('vrf_name') or
|
||||||
peer_type = delta.get('peer_type', existing.get('peer_type'))
|
delta.get('key_id') or delta.get('prefer')):
|
||||||
vrf_name = delta.get('vrf_name', existing.get('vrf_name'))
|
address = delta.get('address', existing.get('address'))
|
||||||
key_id = delta.get('key_id', existing.get('key_id'))
|
peer_type = delta.get('peer_type', existing.get('peer_type'))
|
||||||
prefer = delta.get('prefer', existing.get('prefer'))
|
key_id = delta.get('key_id', existing.get('key_id'))
|
||||||
|
prefer = delta.get('prefer', existing.get('prefer'))
|
||||||
|
vrf_name = delta.get('vrf_name', existing.get('vrf_name'))
|
||||||
|
if delta.get('key_id') == 'default':
|
||||||
|
key_id = None
|
||||||
|
else:
|
||||||
|
peer_type = None
|
||||||
|
prefer = None
|
||||||
|
|
||||||
source_type = delta.get('source_type')
|
source_type = delta.get('source_type')
|
||||||
source = delta.get('source')
|
source = delta.get('source')
|
||||||
|
@ -266,6 +277,9 @@ def config_ntp(delta, existing):
|
||||||
|
|
||||||
ntp_cmds = []
|
ntp_cmds = []
|
||||||
if peer_type:
|
if peer_type:
|
||||||
|
if existing.get('peer_type') and existing.get('address'):
|
||||||
|
ntp_cmds.append('no ntp {0} {1}'.format(existing.get('peer_type'),
|
||||||
|
existing.get('address')))
|
||||||
ntp_cmds.append(set_ntp_server_peer(
|
ntp_cmds.append(set_ntp_server_peer(
|
||||||
peer_type, address, prefer, key_id, vrf_name))
|
peer_type, address, prefer, key_id, vrf_name))
|
||||||
if source:
|
if source:
|
||||||
|
@ -273,7 +287,11 @@ def config_ntp(delta, existing):
|
||||||
existing_source = existing.get('source')
|
existing_source = existing.get('source')
|
||||||
if existing_source_type and source_type != existing_source_type:
|
if existing_source_type and source_type != existing_source_type:
|
||||||
ntp_cmds.append('no ntp {0} {1}'.format(existing_source_type, existing_source))
|
ntp_cmds.append('no ntp {0} {1}'.format(existing_source_type, existing_source))
|
||||||
ntp_cmds.append('ntp {0} {1}'.format(source_type, source))
|
if source == 'default':
|
||||||
|
if existing_source_type and existing_source:
|
||||||
|
ntp_cmds.append('no ntp {0} {1}'.format(existing_source_type, existing_source))
|
||||||
|
else:
|
||||||
|
ntp_cmds.append('ntp {0} {1}'.format(source_type, source))
|
||||||
|
|
||||||
return ntp_cmds
|
return ntp_cmds
|
||||||
|
|
||||||
|
@ -309,6 +327,7 @@ def main():
|
||||||
source_addr = module.params['source_addr']
|
source_addr = module.params['source_addr']
|
||||||
source_int = module.params['source_int']
|
source_int = module.params['source_int']
|
||||||
state = module.params['state']
|
state = module.params['state']
|
||||||
|
|
||||||
if source_int is not None:
|
if source_int is not None:
|
||||||
source_int = source_int.lower()
|
source_int = source_int.lower()
|
||||||
|
|
||||||
|
@ -350,6 +369,9 @@ def main():
|
||||||
|
|
||||||
if state == 'present':
|
if state == 'present':
|
||||||
delta = dict(set(proposed.items()).difference(existing.items()))
|
delta = dict(set(proposed.items()).difference(existing.items()))
|
||||||
|
if delta.get('key_id') and delta.get('key_id') == 'default':
|
||||||
|
if not existing.get('key_id'):
|
||||||
|
delta.pop('key_id')
|
||||||
if delta:
|
if delta:
|
||||||
command = config_ntp(delta, existing)
|
command = config_ntp(delta, existing)
|
||||||
if command:
|
if command:
|
||||||
|
|
|
@ -39,6 +39,25 @@
|
||||||
that:
|
that:
|
||||||
- "result.changed == false"
|
- "result.changed == false"
|
||||||
|
|
||||||
|
- name: Configure ntp with some defaults
|
||||||
|
nxos_ntp: &config1
|
||||||
|
peer: 1.2.3.4
|
||||||
|
key_id: default
|
||||||
|
prefer: enabled
|
||||||
|
vrf_name: default
|
||||||
|
source_addr: default
|
||||||
|
state: present
|
||||||
|
provider: "{{ connection }}"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *true
|
||||||
|
|
||||||
|
- name: Idempotence Check
|
||||||
|
nxos_ntp: *config1
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *false
|
||||||
|
|
||||||
- name: Remove ntp config
|
- name: Remove ntp config
|
||||||
nxos_ntp: *remove
|
nxos_ntp: *remove
|
||||||
register: result
|
register: result
|
||||||
|
@ -51,6 +70,50 @@
|
||||||
|
|
||||||
- assert: *false
|
- assert: *false
|
||||||
|
|
||||||
|
- name: Configure ntp again
|
||||||
|
nxos_ntp: &config2
|
||||||
|
source_int: Ethernet1/3
|
||||||
|
peer: 1.2.3.4
|
||||||
|
prefer: enabled
|
||||||
|
state: present
|
||||||
|
provider: "{{ connection }}"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *true
|
||||||
|
|
||||||
|
- name: Idempotence Check
|
||||||
|
nxos_ntp: *config2
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *false
|
||||||
|
|
||||||
|
- name: Remove source interface
|
||||||
|
nxos_ntp: &config3
|
||||||
|
source_int: default
|
||||||
|
state: present
|
||||||
|
provider: "{{ connection }}"
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *true
|
||||||
|
|
||||||
|
- name: Idempotence Check
|
||||||
|
nxos_ntp: *config3
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *false
|
||||||
|
|
||||||
|
- name: Remove ntp
|
||||||
|
nxos_ntp: *remove
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *true
|
||||||
|
|
||||||
|
- name: Remove Idempotence Check
|
||||||
|
nxos_ntp: *remove
|
||||||
|
register: result
|
||||||
|
|
||||||
|
- assert: *false
|
||||||
|
|
||||||
always:
|
always:
|
||||||
- name: Remove ntp config
|
- name: Remove ntp config
|
||||||
nxos_ntp: *remove
|
nxos_ntp: *remove
|
||||||
|
|
Loading…
Reference in a new issue