diff --git a/lib/ansible/modules/network/nxos/nxos_lldp.py b/lib/ansible/modules/network/nxos/nxos_lldp.py new file mode 100644 index 0000000000..bc254a6d7a --- /dev/null +++ b/lib/ansible/modules/network/nxos/nxos_lldp.py @@ -0,0 +1,110 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- + +# (c) 2017, Ansible by Red Hat, inc +# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +from __future__ import absolute_import, division, print_function +__metaclass__ = type + + +ANSIBLE_METADATA = {'metadata_version': '1.1', + 'status': ['preview'], + 'supported_by': 'network'} + + +DOCUMENTATION = """ +--- +module: nxos_lldp +version_added: "2.5" +author: "Ganesh Nalawade (@ganeshrn)" +short_description: Manage LLDP configuration on Cisco NXOS network devices. +description: + - This module provides declarative management of LLDP service + on Cisco NXOS network devices. +notes: + - Tested against NXOSv 7.0(3)I5(1). +options: + state: + description: + - State of the LLDP configuration. If value is I(present) lldp will be enabled + else if it is I(absent) it will be disabled. + default: present + choices: ['present', 'absent'] +""" + +EXAMPLES = """ +- name: Enable LLDP service + nxos_lldp: + state: present + +- name: Disable LLDP service + nxos_lldp: + state: absent +""" + +RETURN = """ +commands: + description: The list of configuration mode commands to send to the device + returned: always, except for the platforms that use Netconf transport to manage the device. + type: list + sample: + - feature lldp +""" +from ansible.module_utils.basic import AnsibleModule +from ansible.module_utils.network.nxos.nxos import get_config, load_config +from ansible.module_utils.network.nxos.nxos import nxos_argument_spec + + +def has_lldp(module): + output = get_config(module, ['| section lldp']) + is_lldp_enable = False + if output and "feature lldp" in output: + is_lldp_enable = True + + return is_lldp_enable + + +def main(): + """ main entry point for module execution + """ + argument_spec = dict( + state=dict(default='present', + choices=['present', 'absent', + 'enabled', 'disabled']) + ) + + argument_spec.update(nxos_argument_spec) + + module = AnsibleModule(argument_spec=argument_spec, + supports_check_mode=True) + + warnings = list() + + result = {'changed': False} + + if warnings: + result['warnings'] = warnings + + HAS_LLDP = has_lldp(module) + + commands = [] + + if module.params['state'] == 'absent' and HAS_LLDP: + commands.append('no feature lldp') + elif module.params['state'] == 'present' and not HAS_LLDP: + commands.append('feature lldp') + + result['commands'] = commands + + if commands: + if not module.check_mode: + load_config(module, commands) + + result['changed'] = True + + module.exit_json(**result) + + +if __name__ == '__main__': + main() diff --git a/test/integration/nxos.yaml b/test/integration/nxos.yaml index 37333ad3cb..04be74b422 100644 --- a/test/integration/nxos.yaml +++ b/test/integration/nxos.yaml @@ -572,6 +572,15 @@ failed_modules: "{{ failed_modules }} + [ 'nxos_linkagg' ]" test_failed: true + - block: + - include_role: + name: nxos_lldp + when: "limit_to in ['*', 'nxos_lldp']" + rescue: + - set_fact: + failed_modules: "{{ failed_modules }} + [ 'nxos_lldp' ]" + test_failed: true + ########### - debug: var=failed_modules when: test_failed diff --git a/test/integration/targets/nxos_lldp/defaults/main.yaml b/test/integration/targets/nxos_lldp/defaults/main.yaml new file mode 100644 index 0000000000..5f709c5aac --- /dev/null +++ b/test/integration/targets/nxos_lldp/defaults/main.yaml @@ -0,0 +1,2 @@ +--- +testcase: "*" diff --git a/test/integration/targets/nxos_lldp/meta/main.yml b/test/integration/targets/nxos_lldp/meta/main.yml new file mode 100644 index 0000000000..ae741cbdc7 --- /dev/null +++ b/test/integration/targets/nxos_lldp/meta/main.yml @@ -0,0 +1,2 @@ +dependencies: + - prepare_nxos_tests diff --git a/test/integration/targets/nxos_lldp/tasks/cli.yaml b/test/integration/targets/nxos_lldp/tasks/cli.yaml new file mode 100644 index 0000000000..0ab3f8f908 --- /dev/null +++ b/test/integration/targets/nxos_lldp/tasks/cli.yaml @@ -0,0 +1,25 @@ +--- +- name: collect common cli test cases + find: + paths: "{{ role_path }}/tests/common" + patterns: "{{ testcase }}.yaml" + register: test_cases + +- name: collect cli test cases + find: + paths: "{{ role_path }}/tests/cli" + patterns: "{{ testcase }}.yaml" + register: cli_cases + +- set_fact: + test_cases: + files: "{{ test_cases.files }} + {{ cli_cases.files }}" + +- name: set test_items + set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}" + +- name: run test case + include: "{{ test_case_to_run }} connection={{ cli }}" + with_items: "{{ test_items }}" + loop_control: + loop_var: test_case_to_run diff --git a/test/integration/targets/nxos_lldp/tasks/main.yaml b/test/integration/targets/nxos_lldp/tasks/main.yaml new file mode 100644 index 0000000000..4b0f8c64d9 --- /dev/null +++ b/test/integration/targets/nxos_lldp/tasks/main.yaml @@ -0,0 +1,3 @@ +--- +- { include: cli.yaml, tags: ['cli'] } +- { include: nxapi.yaml, tags: ['nxapi'] } diff --git a/test/integration/targets/nxos_lldp/tasks/nxapi.yaml b/test/integration/targets/nxos_lldp/tasks/nxapi.yaml new file mode 100644 index 0000000000..378db2f016 --- /dev/null +++ b/test/integration/targets/nxos_lldp/tasks/nxapi.yaml @@ -0,0 +1,25 @@ +--- +- name: collect common nxapi test cases + find: + paths: "{{ role_path }}/tests/common" + patterns: "{{ testcase }}.yaml" + register: test_cases + +- name: collect nxapi test cases + find: + paths: "{{ role_path }}/tests/nxapi" + patterns: "{{ testcase }}.yaml" + register: nxapi_cases + +- set_fact: + test_cases: + files: "{{ test_cases.files }} + {{ nxapi_cases.files }}" + +- name: set test_items + set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}" + +- name: run test case + include: "{{ test_case_to_run }} connection={{ nxapi }}" + with_items: "{{ test_items }}" + loop_control: + loop_var: test_case_to_run diff --git a/test/integration/targets/nxos_lldp/tests/cli/sanity.yaml b/test/integration/targets/nxos_lldp/tests/cli/sanity.yaml new file mode 100644 index 0000000000..94291edd49 --- /dev/null +++ b/test/integration/targets/nxos_lldp/tests/cli/sanity.yaml @@ -0,0 +1,51 @@ +--- +- debug: msg="START TRANSPORT:CLI nxos_lldp sanity test" + +- name: Make sure LLDP is not running before tests + nxos_config: + lines: no feature lldp + provider: "{{ connection }}" + +- name: Enable LLDP service + nxos_lldp: + state: present + provider: "{{ connection }}" + register: result + +- assert: + that: + - 'result.changed == true' + - '"feature lldp" in result.commands' + +- name: Enable LLDP service again (idempotent) + nxos_lldp: + state: present + provider: "{{ connection }}" + register: result + +- assert: + that: + - 'result.changed == false' + +- name: Disable LLDP service + nxos_lldp: + state: absent + provider: "{{ connection }}" + register: result + +- assert: + that: + - 'result.changed == true' + - '"no feature lldp" in result.commands' + +- name: Disable LLDP service (idempotent) + nxos_lldp: + state: absent + provider: "{{ connection }}" + register: result + +- assert: + that: + - 'result.changed == false' + +- debug: msg="END TRANSPORT:CLI nxos_lldp sanity test" diff --git a/test/integration/targets/nxos_lldp/tests/nxapi/sanity.yaml b/test/integration/targets/nxos_lldp/tests/nxapi/sanity.yaml new file mode 100644 index 0000000000..a0df7811f4 --- /dev/null +++ b/test/integration/targets/nxos_lldp/tests/nxapi/sanity.yaml @@ -0,0 +1,51 @@ +--- +- debug: msg="START TRANSPORT:NXAPI nxos_lldp sanity test" + +- name: Make sure LLDP is not running before tests + nxos_config: + lines: no feature lldp + provider: "{{ connection }}" + +- name: Enable LLDP service + nxos_lldp: + state: present + provider: "{{ connection }}" + register: result + +- assert: + that: + - 'result.changed == true' + - '"feature lldp" in result.commands' + +- name: Enable LLDP service again (idempotent) + nxos_lldp: + state: present + provider: "{{ connection }}" + register: result + +- assert: + that: + - 'result.changed == false' + +- name: Disable LLDP service + nxos_lldp: + state: absent + provider: "{{ connection }}" + register: result + +- assert: + that: + - 'result.changed == true' + - '"no feature lldp" in result.commands' + +- name: Disable LLDP service (idempotent) + nxos_lldp: + state: absent + provider: "{{ connection }}" + register: result + +- assert: + that: + - 'result.changed == false' + +- debug: msg="END TRANSPORT:NXAPI nxos_lldp sanity test"