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:
|
||||
description:
|
||||
- Authentication key identifier to use with
|
||||
given NTP server or peer.
|
||||
given NTP server or peer or keyword 'default'.
|
||||
prefer:
|
||||
description:
|
||||
- Makes given NTP server or peer the preferred
|
||||
|
@ -40,14 +40,16 @@ options:
|
|||
vrf_name:
|
||||
description:
|
||||
- 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:
|
||||
description:
|
||||
- Local source address from which NTP messages are sent.
|
||||
- Local source address from which NTP messages are sent
|
||||
or keyword 'default'
|
||||
source_int:
|
||||
description:
|
||||
- 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:
|
||||
description:
|
||||
- Manage the state of the resource.
|
||||
|
@ -171,10 +173,12 @@ def get_ntp_peer(module):
|
|||
|
||||
split_ntp = ntp.splitlines()
|
||||
for peer_line in split_ntp:
|
||||
if 'access-group' in peer_line:
|
||||
continue
|
||||
ntp_peer = {}
|
||||
try:
|
||||
peer_address = None
|
||||
vrf_name = None
|
||||
vrf_name = 'default'
|
||||
prefer = None
|
||||
key_id = None
|
||||
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):
|
||||
address = delta.get('address', existing.get('address'))
|
||||
peer_type = delta.get('peer_type', existing.get('peer_type'))
|
||||
vrf_name = delta.get('vrf_name', existing.get('vrf_name'))
|
||||
key_id = delta.get('key_id', existing.get('key_id'))
|
||||
prefer = delta.get('prefer', existing.get('prefer'))
|
||||
if (delta.get('address') or delta.get('peer_type') or delta.get('vrf_name') or
|
||||
delta.get('key_id') or delta.get('prefer')):
|
||||
address = delta.get('address', existing.get('address'))
|
||||
peer_type = delta.get('peer_type', existing.get('peer_type'))
|
||||
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 = delta.get('source')
|
||||
|
@ -266,6 +277,9 @@ def config_ntp(delta, existing):
|
|||
|
||||
ntp_cmds = []
|
||||
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(
|
||||
peer_type, address, prefer, key_id, vrf_name))
|
||||
if source:
|
||||
|
@ -273,7 +287,11 @@ def config_ntp(delta, existing):
|
|||
existing_source = existing.get('source')
|
||||
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('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
|
||||
|
||||
|
@ -309,6 +327,7 @@ def main():
|
|||
source_addr = module.params['source_addr']
|
||||
source_int = module.params['source_int']
|
||||
state = module.params['state']
|
||||
|
||||
if source_int is not None:
|
||||
source_int = source_int.lower()
|
||||
|
||||
|
@ -350,6 +369,9 @@ def main():
|
|||
|
||||
if state == 'present':
|
||||
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:
|
||||
command = config_ntp(delta, existing)
|
||||
if command:
|
||||
|
|
|
@ -39,6 +39,25 @@
|
|||
that:
|
||||
- "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
|
||||
nxos_ntp: *remove
|
||||
register: result
|
||||
|
@ -51,6 +70,50 @@
|
|||
|
||||
- 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:
|
||||
- name: Remove ntp config
|
||||
nxos_ntp: *remove
|
||||
|
|
Loading…
Reference in a new issue