From 2f99a1785629758cf78ce2532515f045637d2e7a Mon Sep 17 00:00:00 2001 From: saichint Date: Wed, 25 Apr 2018 23:39:46 -0700 Subject: [PATCH] fix nxos_ntp issues (#39178) * fix nxos_ntp issues * review comments * fix idempotent fail case --- lib/ansible/modules/network/nxos/nxos_ntp.py | 44 +++++++++---- .../targets/nxos_ntp/tests/common/sanity.yaml | 63 +++++++++++++++++++ 2 files changed, 96 insertions(+), 11 deletions(-) diff --git a/lib/ansible/modules/network/nxos/nxos_ntp.py b/lib/ansible/modules/network/nxos/nxos_ntp.py index 581fa666bed..3ba30576614 100644 --- a/lib/ansible/modules/network/nxos/nxos_ntp.py +++ b/lib/ansible/modules/network/nxos/nxos_ntp.py @@ -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: diff --git a/test/integration/targets/nxos_ntp/tests/common/sanity.yaml b/test/integration/targets/nxos_ntp/tests/common/sanity.yaml index 0ac117513dd..838359a7c87 100644 --- a/test/integration/targets/nxos_ntp/tests/common/sanity.yaml +++ b/test/integration/targets/nxos_ntp/tests/common/sanity.yaml @@ -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