Add vyos_lldp and vyos_lldp_interface modules (#26753)

* Add vyos_lldp and vyos_lldp_interface modules

* Fix module docstring issue

* Fix bogus aggregate reference

* Add vyos_lldp integration tests

* Add vyos_lldp_interface integration tests

* Remove unused import for run_commands

* Add VyOS net_lldp integration tests

* Remove junos and netconf from net_lldp integration tests

* Add net_lldp_interface integration tests

* Correct CLI filtering tests for VyOS

* Fix pep8 issues

* Fix more pep8 issues
pull/26798/head
Ricardo Carrillo Cruz 7 years ago committed by GitHub
parent 4b3d6dfa8a
commit af4dc6d0eb

@ -0,0 +1,119 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2017, Ansible by Red Hat, inc
#
# This file is part of Ansible by Red Hat
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['preview'],
'supported_by': 'core'}
DOCUMENTATION = """
---
module: vyos_lldp
version_added: "2.4"
author: "Ricardo Carrillo Cruz (@rcarrillocruz)"
short_description: Manage LLDP configuration on VyOS network devices
description:
- This module provides declarative management of LLDP service
on VyOS network devices.
options:
state:
description:
- State of the LLDP configuration.
default: present
choices: ['present', 'absent']
"""
EXAMPLES = """
- name: Enable LLDP service
vyos_lldp:
state: present
- name: Disable LLDP service
vyos_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:
- set service lldp
"""
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.vyos import get_config, load_config
from ansible.module_utils.vyos import vyos_argument_spec, check_args
def has_lldp(module):
config = get_config(module).splitlines()
if "set service 'lldp'" in config or 'set service lldp' in config:
return True
else:
return False
def main():
""" main entry point for module execution
"""
argument_spec = dict(
interfaces=dict(type='list'),
purge=dict(default=False, type='bool'),
state=dict(default='present',
choices=['present', 'absent',
'enabled', 'disabled'])
)
argument_spec.update(vyos_argument_spec)
module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True)
warnings = list()
check_args(module, warnings)
result = {'changed': False}
if warnings:
result['warnings'] = warnings
HAS_LLDP = has_lldp(module)
commands = []
if module.params['state'] == 'absent' and HAS_LLDP:
commands.append('delete service lldp')
elif module.params['state'] == 'present' and not HAS_LLDP:
commands.append('set service lldp')
result['commands'] = commands
if commands:
commit = not module.check_mode
load_config(module, commands, commit=commit)
result['changed'] = True
module.exit_json(**result)
if __name__ == '__main__':
main()

@ -0,0 +1,202 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2017, Ansible by Red Hat, inc
#
# This file is part of Ansible by Red Hat
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
#
ANSIBLE_METADATA = {'metadata_version': '1.0',
'status': ['preview'],
'supported_by': 'core'}
DOCUMENTATION = """
---
module: vyos_lldp_interface
version_added: "2.4"
author: "Ricardo Carrillo Cruz (@rcarrillocruz)"
short_description: Manage LLDP interfaces configuration on VyOS network devices
description:
- This module provides declarative management of LLDP interfaces
configuration on VyOS network devices.
options:
name:
description:
- Name of the interface LLDP should be configured on.
aggregate:
description: List of interfaces LLDP should be configured on.
purge:
description:
- Purge interfaces not defined in the aggregate parameter.
default: no
state:
description:
- State of the LLDP configuration.
default: present
choices: ['present', 'absent', 'enabled', 'disabled']
"""
EXAMPLES = """
- name: Enable LLDP on eth1
net_lldp_interface:
state: present
- name: Enable LLDP on specific interfaces
net_lldp_interface:
interfaces:
- eth1
- eth2
state: present
- name: Disable LLDP globally
net_lldp_interface:
state: lldp
"""
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:
- set service lldp eth1
- set service lldp eth2 disable
"""
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.vyos import get_config, load_config
from ansible.module_utils.vyos import vyos_argument_spec, check_args
def search_obj_in_list(name, lst):
for o in lst:
if o['name'] == name:
return o
return None
def map_obj_to_commands(updates, module):
commands = list()
want, have = updates
for w in want:
name = w['name']
state = w['state']
obj_in_have = search_obj_in_list(name, have)
if state == 'absent' and obj_in_have:
commands.append('delete service lldp interface ' + name)
elif state in ('present', 'enabled'):
if not obj_in_have:
commands.append('set service lldp interface ' + name)
elif obj_in_have and obj_in_have['state'] == 'disabled' and state == 'enabled':
commands.append('delete service lldp interface ' + name + ' disable')
elif state == 'disabled':
if not obj_in_have:
commands.append('set service lldp interface ' + name)
commands.append('set service lldp interface ' + name + ' disable')
elif obj_in_have and obj_in_have['state'] != 'disabled':
commands.append('set service lldp interface ' + name + ' disable')
return commands
def map_config_to_obj(module):
obj = []
config = get_config(module).splitlines()
output = [c for c in config if c.startswith("set service lldp interface")]
for i in output:
splitted_line = i.split()
if len(splitted_line) > 5:
new_obj = {'name': splitted_line[4]}
if splitted_line[5] == "'disable'":
new_obj['state'] = 'disabled'
else:
new_obj = {'name': splitted_line[4][1:-1]}
new_obj['state'] = 'present'
obj.append(new_obj)
return obj
def map_params_to_obj(module):
obj = []
if module.params['aggregate']:
for i in module.params['aggregate']:
d = i.copy()
if 'state' not in d:
d['state'] = module.params['state']
obj.append(d)
else:
obj.append({'name': module.params['name'], 'state': module.params['state']})
return obj
def main():
""" main entry point for module execution
"""
argument_spec = dict(
name=dict(),
aggregate=dict(type='list'),
purge=dict(default=False, type='bool'),
state=dict(default='present',
choices=['present', 'absent',
'enabled', 'disabled'])
)
argument_spec.update(vyos_argument_spec)
required_one_of = [['name', 'aggregate']]
mutually_exclusive = [['name', 'aggregate']]
module = AnsibleModule(argument_spec=argument_spec,
required_one_of=required_one_of,
mutually_exclusive=mutually_exclusive,
supports_check_mode=True)
warnings = list()
check_args(module, warnings)
result = {'changed': False}
if warnings:
result['warnings'] = warnings
want = map_params_to_obj(module)
have = map_config_to_obj(module)
commands = map_obj_to_commands((want, have), module)
result['commands'] = commands
if commands:
commit = not module.check_mode
load_config(module, commands, commit=commit)
result['changed'] = True
module.exit_json(**result)
if __name__ == '__main__':
main()

@ -85,6 +85,20 @@
rescue:
- set_fact: test_failed=true
- block:
- include_role:
name: net_lldp
when: "limit_to in ['*', 'net_lldp']"
rescue:
- set_fact: test_failed=true
- block:
- include_role:
name: net_lldp_interface
when: "limit_to in ['*', 'net_lldp_interface']"
rescue:
- set_fact: test_failed=true
###########
- name: Has any previous test failed?
fail:

@ -0,0 +1,3 @@
---
testcase: "*"
test_items: []

@ -0,0 +1,15 @@
---
- name: collect all cli test cases
find:
paths: "{{ role_path }}/tests/cli"
patterns: "{{ testcase }}.yaml"
register: test_cases
- name: set test_items
set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}"
- name: run test case
include: "{{ test_case_to_run }}"
with_items: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run

@ -0,0 +1,3 @@
---
- { include: cli.yaml, tags: ['cli'] }
- { include: netconf.yaml, tags: ['netconf'] }

@ -0,0 +1,16 @@
---
- name: collect all netconf test cases
find:
paths: "{{ role_path }}/tests/netconf"
patterns: "{{ testcase }}.yaml"
register: test_cases
delegate_to: localhost
- name: set test_items
set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}"
- name: run test case
include: "{{ test_case_to_run }}"
with_items: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run

@ -0,0 +1,4 @@
---
- include: "{{ role_path }}/tests/vyos/basic.yaml"
when: hostvars[inventory_hostname]['ansible_network_os'] == 'vyos'

@ -0,0 +1,42 @@
---
- name: Make sure LLDP is not running before tests
vyos_config:
lines: delete service lldp
- name: Enable LLDP service
net_lldp:
state: present
register: result
- assert:
that:
- 'result.changed == true'
- '"set service lldp" in result.commands'
- name: Enable LLDP service again (idempotent)
net_lldp:
state: present
register: result
- assert:
that:
- 'result.changed == false'
- name: Disable LLDP service
net_lldp:
state: absent
register: result
- assert:
that:
- 'result.changed == true'
- '"delete service lldp" in result.commands'
- name:
net_lldp:
state: absent
register: result
- assert:
that:
- 'result.changed == false'

@ -0,0 +1,15 @@
---
- name: collect all cli test cases
find:
paths: "{{ role_path }}/tests/cli"
patterns: "{{ testcase }}.yaml"
register: test_cases
- name: set test_items
set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}"
- name: run test case
include: "{{ test_case_to_run }}"
with_items: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run

@ -0,0 +1,3 @@
---
- { include: cli.yaml, tags: ['cli'] }
- { include: netconf.yaml, tags: ['netconf'] }

@ -0,0 +1,16 @@
---
- name: collect all netconf test cases
find:
paths: "{{ role_path }}/tests/netconf"
patterns: "{{ testcase }}.yaml"
register: test_cases
delegate_to: localhost
- name: set test_items
set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}"
- name: run test case
include: "{{ test_case_to_run }}"
with_items: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run

@ -0,0 +1,4 @@
---
- include: "{{ role_path }}/tests/vyos/basic.yaml"
when: hostvars[inventory_hostname]['ansible_network_os'] == 'vyos'

@ -0,0 +1,165 @@
---
- name: Make sure LLDP is not running before tests
vyos_config:
lines: delete service lldp
- name: Create LLDP configuration
net_lldp_interface:
name: eth1
state: present
register: result
- assert:
that:
- 'result.changed == true'
- '"set service lldp interface eth1" in result.commands'
- name: Create LLDP configuration again (idempotent)
net_lldp_interface:
name: eth1
state: present
register: result
- assert:
that:
- 'result.changed == false'
- name: Disable LLDP configuration
net_lldp_interface:
name: eth1
state: disabled
register: result
- assert:
that:
- 'result.changed == true'
- '"set service lldp interface eth1 disable" in result.commands'
- name: Disable LLDP configuration again (idempotent)
net_lldp_interface:
name: eth1
state: disabled
register: result
- assert:
that:
- 'result.changed == false'
- name: Enable LLDP configuration
net_lldp_interface:
name: eth1
state: enabled
register: result
- assert:
that:
- 'result.changed == true'
- '"delete service lldp interface eth1 disable" in result.commands'
- name: Enable LLDP configuration again (idempotent)
net_lldp_interface:
name: eth1
state: enabled
register: result
- assert:
that:
- 'result.changed == false'
- name: Delete LLDP configuration
net_lldp_interface:
name: eth1
state: absent
register: result
- assert:
that:
- 'result.changed == true'
- '"delete service lldp interface eth1" in result.commands'
- name: Delete LLDP configuration again (idempotent)
net_lldp_interface:
name: eth1
state: absent
register: result
- assert:
that:
- 'result.changed == false'
- name: Create aggregate of LLDP interface configurations
net_lldp_interface:
aggregate:
- { name: eth1 }
- { name: eth2 }
state: present
register: result
- assert:
that:
- 'result.changed == true'
- '"set service lldp interface eth1" in result.commands'
- '"set service lldp interface eth2" in result.commands'
- name: Create aggregate of LLDP interface configurations again (idempotent)
net_lldp_interface:
aggregate:
- { name: eth1 }
- { name: eth2 }
state: present
register: result
- assert:
that:
- 'result.changed == false'
- name: Override LLDP interface configuration on aggregate
net_lldp_interface:
aggregate:
- { name: eth1 }
- { name: eth2, state: disabled }
state: present
register: result
- assert:
that:
- 'result.changed == true'
- '"set service lldp interface eth2 disable" in result.commands'
- name: Override LLDP interface configuration on aggregate again (idempotent)
net_lldp_interface:
aggregate:
- { name: eth1 }
- { name: eth2, state: disabled }
state: present
register: result
- assert:
that:
- 'result.changed == false'
- name: Delete aggregate of LLDP interface configurations
net_lldp_interface:
aggregate:
- { name: eth1 }
- { name: eth2 }
state: absent
register: result
- assert:
that:
- 'result.changed == true'
- '"delete service lldp interface eth1" in result.commands'
- '"delete service lldp interface eth2" in result.commands'
- name: Delete aggregate of LLDP interface configurations (idempotent)
net_lldp_interface:
aggregate:
- { name: eth1 }
- { name: eth2 }
state: absent
register: result
- assert:
that:
- 'result.changed == false'

@ -0,0 +1,3 @@
---
testcase: "*"
test_items: []

@ -0,0 +1,15 @@
---
- name: collect all cli test cases
find:
paths: "{{ role_path }}/tests/cli"
patterns: "{{ testcase }}.yaml"
register: test_cases
- name: set test_items
set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}"
- name: run test case
include: "{{ test_case_to_run }}"
with_items: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run

@ -0,0 +1,2 @@
---
- { include: cli.yaml, tags: ['cli'] }

@ -0,0 +1,42 @@
---
- name: Make sure LLDP is not running before tests
vyos_config:
lines: delete service lldp
- name: Enable LLDP service
vyos_lldp:
state: present
register: result
- assert:
that:
- 'result.changed == true'
- '"set service lldp" in result.commands'
- name: Enable LLDP service again (idempotent)
vyos_lldp:
state: present
register: result
- assert:
that:
- 'result.changed == false'
- name: Disable LLDP service
vyos_lldp:
state: absent
register: result
- assert:
that:
- 'result.changed == true'
- '"delete service lldp" in result.commands'
- name:
vyos_lldp:
state: absent
register: result
- assert:
that:
- 'result.changed == false'

@ -0,0 +1,15 @@
---
- name: collect all cli test cases
find:
paths: "{{ role_path }}/tests/cli"
patterns: "{{ testcase }}.yaml"
register: test_cases
- name: set test_items
set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}"
- name: run test case
include: "{{ test_case_to_run }}"
with_items: "{{ test_items }}"
loop_control:
loop_var: test_case_to_run

@ -0,0 +1,2 @@
---
- { include: cli.yaml, tags: ['cli'] }

@ -0,0 +1,165 @@
---
- name: Make sure LLDP is not running before tests
vyos_config:
lines: delete service lldp
- name: Create LLDP configuration
vyos_lldp_interface:
name: eth1
state: present
register: result
- assert:
that:
- 'result.changed == true'
- '"set service lldp interface eth1" in result.commands'
- name: Create LLDP configuration again (idempotent)
vyos_lldp_interface:
name: eth1
state: present
register: result
- assert:
that:
- 'result.changed == false'
- name: Disable LLDP configuration
vyos_lldp_interface:
name: eth1
state: disabled
register: result
- assert:
that:
- 'result.changed == true'
- '"set service lldp interface eth1 disable" in result.commands'
- name: Disable LLDP configuration again (idempotent)
vyos_lldp_interface:
name: eth1
state: disabled
register: result
- assert:
that:
- 'result.changed == false'
- name: Enable LLDP configuration
vyos_lldp_interface:
name: eth1
state: enabled
register: result
- assert:
that:
- 'result.changed == true'
- '"delete service lldp interface eth1 disable" in result.commands'
- name: Enable LLDP configuration again (idempotent)
vyos_lldp_interface:
name: eth1
state: enabled
register: result
- assert:
that:
- 'result.changed == false'
- name: Delete LLDP configuration
vyos_lldp_interface:
name: eth1
state: absent
register: result
- assert:
that:
- 'result.changed == true'
- '"delete service lldp interface eth1" in result.commands'
- name: Delete LLDP configuration again (idempotent)
vyos_lldp_interface:
name: eth1
state: absent
register: result
- assert:
that:
- 'result.changed == false'
- name: Create aggregate of LLDP interface configurations
vyos_lldp_interface:
aggregate:
- { name: eth1 }
- { name: eth2 }
state: present
register: result
- assert:
that:
- 'result.changed == true'
- '"set service lldp interface eth1" in result.commands'
- '"set service lldp interface eth2" in result.commands'
- name: Create aggregate of LLDP interface configurations again (idempotent)
vyos_lldp_interface:
aggregate:
- { name: eth1 }
- { name: eth2 }
state: present
register: result
- assert:
that:
- 'result.changed == false'
- name: Override LLDP interface configuration on aggregate
vyos_lldp_interface:
aggregate:
- { name: eth1 }
- { name: eth2, state: disabled }
state: present
register: result
- assert:
that:
- 'result.changed == true'
- '"set service lldp interface eth2 disable" in result.commands'
- name: Override LLDP interface configuration on aggregate again (idempotent)
vyos_lldp_interface:
aggregate:
- { name: eth1 }
- { name: eth2, state: disabled }
state: present
register: result
- assert:
that:
- 'result.changed == false'
- name: Delete aggregate of LLDP interface configurations
vyos_lldp_interface:
aggregate:
- { name: eth1 }
- { name: eth2 }
state: absent
register: result
- assert:
that:
- 'result.changed == true'
- '"delete service lldp interface eth1" in result.commands'
- '"delete service lldp interface eth2" in result.commands'
- name: Delete aggregate of LLDP interface configurations (idempotent)
vyos_lldp_interface:
aggregate:
- { name: eth1 }
- { name: eth2 }
state: absent
register: result
- assert:
that:
- 'result.changed == false'

@ -70,6 +70,20 @@
rescue:
- set_fact: test_failed=true
- block:
- include_role:
name: vyos_lldp
when: "limit_to in ['*', 'vyos_lldp']"
rescue:
- set_fact: test_failed=true
- block:
- include_role:
name: vyos_lldp_interface
when: "limit_to in ['*', 'vyos_lldp_interface']"
rescue:
- set_fact: test_failed=true
###########
- name: Has any previous test failed?

Loading…
Cancel
Save