|
|
@ -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__':
|
|
|
|