WIP Implement declarative intent arguments on eos_vlan (#28270)

Implement declarative intent arguments on eos_vlan
pull/26558/merge
Ricardo Carrillo Cruz 7 years ago committed by GitHub
parent 2e211078ce
commit 5a6f3ebed1

@ -43,8 +43,11 @@ options:
required: true required: true
interfaces: interfaces:
description: description:
- List of interfaces to check the VLAN has been - List of interfaces that should be associated to the VLAN.
configured correctly. delay:
description:
- Delay the play should wait to check for declaratie intent params values.
default: 10
aggregate: aggregate:
description: List of VLANs definitions description: List of VLANs definitions
purge: purge:
@ -76,88 +79,169 @@ from ansible.module_utils.eos import eos_argument_spec, check_args
from ansible.module_utils.six import iteritems from ansible.module_utils.six import iteritems
import re import re
import time
def search_obj_in_list(vlan_id, lst):
for o in lst:
if o['vlan_id'] == vlan_id:
return o
def map_obj_to_commands(updates, module): def map_obj_to_commands(updates, module):
commands = list() commands = list()
want, have = updates want, have = updates
state = module.params['state'] purge = module.params['purge']
if state == 'absent': for w in want:
if have: vlan_id = w['vlan_id']
commands.append('no vlan %s' % want['vlan_id']) name = w['name']
elif state == 'present': state = w['state']
if not have or want['name'] != have['name']: interfaces = w['interfaces']
commands.append('vlan %s' % want['vlan_id'])
commands.append('name %s' % want['name']) obj_in_have = search_obj_in_list(vlan_id, have)
else:
if not have: if state == 'absent':
commands.append('vlan %s' % want['vlan_id']) if obj_in_have:
commands.append('name %s' % want['name']) commands.append('no vlan %s' % w['vlan_id'])
commands.append('state %s' % want['state']) elif state == 'present':
elif have['name'] != want['name'] or have['state'] != want['state']: if not obj_in_have:
commands.append('vlan %s' % want['vlan_id']) commands.append('vlan %s' % w['vlan_id'])
commands.append('name %s' % w['name'])
if have['name'] != want['name']:
commands.append('name %s' % want['name']) if w['interfaces']:
for i in w['interfaces']:
if have['state'] != want['state']: commands.append('interface %s' % i)
commands.append('state %s' % want['state']) commands.append('switchport access vlan %s' % w['vlan_id'])
else:
if w['name'] and w['name'] != obj_in_have['name']:
commands.append('vlan %s' % w['vlan_id'])
commands.append('name %s' % w['name'])
if w['interfaces']:
if not obj_in_have['interfaces']:
for i in w['interfaces']:
commands.append('vlan %s' % w['vlan_id'])
commands.append('interface %s' % i)
commands.append('switchport access vlan %s' % w['vlan_id'])
elif set(w['interfaces']) != obj_in_have['interfaces']:
missing_interfaces = list(set(w['interfaces']) - set(obj_in_have['interfaces']))
for i in missing_interfaces:
commands.append('vlan %s' % w['vlan_id'])
commands.append('interface %s' % i)
commands.append('switchport access vlan %s' % w['vlan_id'])
superfluous_interfaces = list(set(obj_in_have['interfaces']) - set(w['interfaces']))
for i in superfluous_interfaces:
commands.append('vlan %s' % w['vlan_id'])
commands.append('interface %s' % i)
commands.append('no switchport access vlan %s' % w['vlan_id'])
else:
if not obj_in_have:
commands.append('vlan %s' % w['vlan_id'])
commands.append('name %s' % w['name'])
commands.append('state %s' % w['state'])
elif obj_in_have['name'] != w['name'] or obj_in_have['state'] != w['state']:
commands.append('vlan %s' % w['vlan_id'])
if obj_in_have['name'] != w['name']:
commands.append('name %s' % w['name'])
if obj_in_have['state'] != w['state']:
commands.append('state %s' % w['state'])
if purge:
for h in have:
obj_in_want = search_obj_in_list(h['vlan_id'], want)
if not obj_in_want and h['vlan_id'] != '1':
commands.append('no vlan %s' % h['vlan_id'])
return commands return commands
def map_config_to_obj(module): def map_config_to_obj(module):
obj = {} objs = []
output = run_commands(module, ['show vlan']) output = run_commands(module, ['show vlan'])
lines = output[0].strip().splitlines()[2:]
for l in lines:
splitted_line = re.split(r'\s{2,}', l.strip())
obj = {}
obj['vlan_id'] = splitted_line[0]
obj['name'] = splitted_line[1]
obj['state'] = splitted_line[2]
if obj['state'] == 'suspended':
obj['state'] = 'suspend'
obj['interfaces'] = []
if len(splitted_line) > 3:
for i in splitted_line[3].split(','):
obj['interfaces'].append(i.strip().replace('Et', 'Ethernet'))
objs.append(obj)
return objs
if isinstance(output[0], str):
for l in output[0].strip().splitlines()[2:]: def map_params_to_obj(module):
split_line = l.split() obj = []
vlan_id = split_line[0]
name = split_line[1] if 'aggregate' in module.params and module.params['aggregate']:
status = split_line[2] for v in module.params['aggregate']:
d = v.copy()
if vlan_id == str(module.params['vlan_id']):
obj['vlan_id'] = vlan_id d['vlan_id'] = str(d['vlan_id'])
obj['name'] = name
obj['state'] = status if 'state' not in d:
if obj['state'] == 'suspended': d['state'] = module.params['state']
obj['state'] = 'suspend'
break if 'name' not in d:
d['name'] = None
if 'interfaces' not in d:
d['interfaces'] = []
obj.append(d)
else: else:
for k, v in iteritems(output[0]['vlans']): vlan_id = str(module.params['vlan_id'])
vlan_id = k name = module.params['name']
name = v['name'] state = module.params['state']
status = v['status'] interfaces = module.params['interfaces']
if vlan_id == str(module.params['vlan_id']): obj.append({
obj['vlan_id'] = vlan_id 'vlan_id': vlan_id,
obj['name'] = name 'name': name,
obj['state'] = status 'state': state,
if obj['state'] == 'suspended': 'interfaces': interfaces
obj['state'] = 'suspend' })
break
return obj return obj
def map_params_to_obj(module): def check_declarative_intent_params(want, module):
return { if module.params['interfaces']:
'vlan_id': str(module.params['vlan_id']), time.sleep(module.params['delay'])
'name': module.params['name'], have = map_config_to_obj(module)
'state': module.params['state']
} for w in want:
for i in w['interfaces']:
obj_in_have = search_obj_in_list(w['vlan_id'], have)
if obj_in_have and 'interfaces' in obj_in_have and i not in obj_in_have['interfaces']:
module.fail_json(msg="Interface %s not configured on vlan %s" % (i, w['vlan_id']))
def main(): def main():
""" main entry point for module execution """ main entry point for module execution
""" """
argument_spec = dict( argument_spec = dict(
vlan_id=dict(required=True, type='int'), vlan_id=dict(type='int'),
name=dict(), name=dict(),
interfaces=dict(), interfaces=dict(type='list'),
aggregate=dict(), delay=dict(default=10, type='int'),
aggregate=dict(type='list'),
purge=dict(default=False, type='bool'), purge=dict(default=False, type='bool'),
state=dict(default='present', state=dict(default='present',
choices=['present', 'absent', 'active', 'suspend']) choices=['present', 'absent', 'active', 'suspend'])
@ -165,6 +249,8 @@ def main():
argument_spec.update(eos_argument_spec) argument_spec.update(eos_argument_spec)
required_one_of = [['vlan_id', 'aggregate']]
mutually_exclusive = [['vlan_id', 'aggregate']]
module = AnsibleModule(argument_spec=argument_spec, module = AnsibleModule(argument_spec=argument_spec,
supports_check_mode=True) supports_check_mode=True)
@ -190,6 +276,9 @@ def main():
result['session_name'] = response.get('session') result['session_name'] = response.get('session')
result['changed'] = True result['changed'] = True
if result['changed']:
check_declarative_intent_params(want, module)
module.exit_json(**result) module.exit_json(**result)
if __name__ == '__main__': if __name__ == '__main__':

@ -1,37 +1,53 @@
--- ---
- name: setup - remove vlan - name: setup - remove vlans used in test
eos_vlan: eos_config:
vlan_id: 4000 lines:
name: test-vlan - no vlan 4000
state: absent - no vlan 4001
- no vlan 4002
authorize: yes
provider: "{{ cli }}"
- name: setup - remove switchport settings on interface Ethernet1 used in test
eos_config:
lines:
- switchport
- no switchport access vlan 4000
parents: interface Ethernet1
authorize: yes
provider: "{{ cli }}"
- name: setup - remove switchport settings on interface Ethernet2 used in test
eos_config:
lines:
- switchport
- no switchport access vlan 4000
parents: interface Ethernet2
authorize: yes authorize: yes
provider: "{{ cli }}" provider: "{{ cli }}"
- name: Create vlan - name: Create vlan
eos_vlan: eos_vlan:
vlan_id: 4000 vlan_id: 4000
name: test-vlan name: vlan-4000
state: present state: present
authorize: yes authorize: yes
provider: "{{ cli }}" provider: "{{ cli }}"
register: result register: result
- debug:
msg: "{{ result }}"
- assert: - assert:
that: that:
- "result.changed == true" - "result.changed == true"
- "'vlan 4000' in result.commands" - "'vlan 4000' in result.commands"
- "'name test-vlan' in result.commands" - "'name vlan-4000' in result.commands"
# Ensure sessions contains epoc. Will fail after 18th May 2033 # Ensure sessions contains epoc. Will fail after 18th May 2033
- "'ansible_1' in result.session_name" - "'ansible_1' in result.session_name"
- name: Create vlan again (idempotent) - name: Create vlan again (idempotent)
eos_vlan: eos_vlan:
vlan_id: 4000 vlan_id: 4000
name: test-vlan name: vlan-4000
state: present state: present
authorize: yes authorize: yes
provider: "{{ cli }}" provider: "{{ cli }}"
@ -47,7 +63,7 @@
- name: Change vlan name and state - name: Change vlan name and state
eos_vlan: eos_vlan:
vlan_id: 4000 vlan_id: 4000
name: test-vlan2 name: vlan-4000-new
state: suspend state: suspend
authorize: yes authorize: yes
provider: "{{ cli }}" provider: "{{ cli }}"
@ -57,12 +73,192 @@
that: that:
- "result.changed == true" - "result.changed == true"
- "'vlan 4000' in result.commands" - "'vlan 4000' in result.commands"
- "'name test-vlan2' in result.commands" - "'name vlan-4000-new' in result.commands"
- "'state suspend' in result.commands" - "'state suspend' in result.commands"
# Ensure sessions contains epoc. Will fail after 18th May 2033 # Ensure sessions contains epoc. Will fail after 18th May 2033
- "'ansible_1' in result.session_name" - "'ansible_1' in result.session_name"
- name: Change vlan name and state again (idempotent)
eos_vlan:
vlan_id: 4000
name: vlan-4000-new
state: suspend
authorize: yes
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.changed == false"
- "result.commands | length == 0"
# Ensure sessions contains epoc. Will fail after 18th May 2033
- "result.session_name is not defined"
- name: Unsuspend vlan
eos_vlan:
vlan_id: 4000
state: active
authorize: yes
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.changed == true"
- "'vlan 4000' in result.commands"
- "'state active' in result.commands"
# Ensure sessions contains epoc. Will fail after 18th May 2033
- "'ansible_1' in result.session_name"
- name: Add interfaces to vlan
eos_vlan:
vlan_id: 4000
state: present
interfaces:
- Ethernet1
- Ethernet2
authorize: yes
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.changed == true"
- "'vlan 4000' in result.commands"
- "'interface Ethernet1' in result.commands"
- "'switchport access vlan 4000' in result.commands"
- "'interface Ethernet2' in result.commands"
- "'switchport access vlan 4000' in result.commands"
# Ensure sessions contains epoc. Will fail after 18th May 2033
- "'ansible_1' in result.session_name"
- name: Add interfaces to vlan again (idempotent)
eos_vlan:
vlan_id: 4000
state: present
interfaces:
- Ethernet1
- Ethernet2
authorize: yes
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.changed == false"
- "result.commands | length == 0"
# Ensure sessions contains epoc. Will fail after 18th May 2033
- "result.session_name is not defined"
- name: Remove interface from vlan
eos_vlan:
vlan_id: 4000
state: present
interfaces:
- Ethernet1
authorize: yes
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.changed == true"
- "'vlan 4000' in result.commands"
- "'interface Ethernet2' in result.commands"
- "'no switchport access vlan 4000' in result.commands"
# Ensure sessions contains epoc. Will fail after 18th May 2033
- "'ansible_1' in result.session_name"
- name: Remove interface from vlan again (idempotent)
eos_vlan:
vlan_id: 4000
state: present
interfaces:
- Ethernet1
authorize: yes
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.changed == false"
- "result.commands | length == 0"
# Ensure sessions contains epoc. Will fail after 18th May 2033
- "result.session_name is not defined"
- name: Create aggregate of vlans
eos_vlan:
aggregate:
- {vlan_id: 4000, state: absent}
- {vlan_id: 4001, name: vlan-4001}
state: present
authorize: yes
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.changed == true"
- "'no vlan 4000' in result.commands"
- "'vlan 4001' in result.commands"
- "'name vlan-4001' in result.commands"
# Ensure sessions contains epoc. Will fail after 18th May 2033
- "'ansible_1' in result.session_name"
- name: Create aggregate of vlans again (idempotent)
eos_vlan:
aggregate:
- {vlan_id: 4000, state: absent}
- {vlan_id: 4001, name: vlan-4001}
state: present
authorize: yes
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.changed == false"
- "result.commands | length == 0"
# Ensure sessions contains epoc. Will fail after 18th May 2033
- "result.session_name is not defined"
- name: Create vlan with purge
eos_vlan:
aggregate:
- {vlan_id: 4002, name: vlan-4002}
name: vlan-4002
state: present
purge: yes
authorize: yes
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.changed == true"
- "'no vlan 4001' in result.commands"
- "'vlan 4002' in result.commands"
- "'name vlan-4002' in result.commands"
# Ensure sessions contains epoc. Will fail after 18th May 2033
- "'ansible_1' in result.session_name"
- name: Create vlan with purge
eos_vlan:
aggregate:
- {vlan_id: 4002, name: vlan-4002}
name: vlan-4002
state: present
purge: yes
authorize: yes
provider: "{{ cli }}"
register: result
- assert:
that:
- "result.changed == false"
- "result.commands | length == 0"
# Ensure sessions contains epoc. Will fail after 18th May 2033
- "result.session_name is not defined"
# FIXME add in tests for everything defined in docs # FIXME add in tests for everything defined in docs
# FIXME Test state:absent + test: # FIXME Test state:absent + test:
# FIXME Without powers ensure "privileged mode required" # FIXME Without powers ensure "privileged mode required"

Loading…
Cancel
Save