Add nxos_lldp module (#34453)
* Add nxos_lldp module * Implemetation for nxos_lldp module * Integration test for nxos_lldp module * Minor change
This commit is contained in:
parent
2376d02f3f
commit
ab67539bef
9 changed files with 278 additions and 0 deletions
110
lib/ansible/modules/network/nxos/nxos_lldp.py
Normal file
110
lib/ansible/modules/network/nxos/nxos_lldp.py
Normal file
|
@ -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()
|
|
@ -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
|
||||
|
|
2
test/integration/targets/nxos_lldp/defaults/main.yaml
Normal file
2
test/integration/targets/nxos_lldp/defaults/main.yaml
Normal file
|
@ -0,0 +1,2 @@
|
|||
---
|
||||
testcase: "*"
|
2
test/integration/targets/nxos_lldp/meta/main.yml
Normal file
2
test/integration/targets/nxos_lldp/meta/main.yml
Normal file
|
@ -0,0 +1,2 @@
|
|||
dependencies:
|
||||
- prepare_nxos_tests
|
25
test/integration/targets/nxos_lldp/tasks/cli.yaml
Normal file
25
test/integration/targets/nxos_lldp/tasks/cli.yaml
Normal file
|
@ -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
|
3
test/integration/targets/nxos_lldp/tasks/main.yaml
Normal file
3
test/integration/targets/nxos_lldp/tasks/main.yaml
Normal file
|
@ -0,0 +1,3 @@
|
|||
---
|
||||
- { include: cli.yaml, tags: ['cli'] }
|
||||
- { include: nxapi.yaml, tags: ['nxapi'] }
|
25
test/integration/targets/nxos_lldp/tasks/nxapi.yaml
Normal file
25
test/integration/targets/nxos_lldp/tasks/nxapi.yaml
Normal file
|
@ -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
|
51
test/integration/targets/nxos_lldp/tests/cli/sanity.yaml
Normal file
51
test/integration/targets/nxos_lldp/tests/cli/sanity.yaml
Normal file
|
@ -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"
|
51
test/integration/targets/nxos_lldp/tests/nxapi/sanity.yaml
Normal file
51
test/integration/targets/nxos_lldp/tests/nxapi/sanity.yaml
Normal file
|
@ -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"
|
Loading…
Reference in a new issue