diff options
Diffstat (limited to 'plugins/modules/vyos_vlan.py')
-rw-r--r-- | plugins/modules/vyos_vlan.py | 188 |
1 files changed, 103 insertions, 85 deletions
diff --git a/plugins/modules/vyos_vlan.py b/plugins/modules/vyos_vlan.py index 88ab71b..983a50a 100644 --- a/plugins/modules/vyos_vlan.py +++ b/plugins/modules/vyos_vlan.py @@ -5,12 +5,15 @@ # 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'} +ANSIBLE_METADATA = { + "metadata_version": "1.1", + "status": ["preview"], + "supported_by": "network", +} DOCUMENTATION = """ --- @@ -122,18 +125,20 @@ from copy import deepcopy from ansible.module_utils.basic import AnsibleModule from ansible.module_utils.network.common.utils import remove_default_spec -from ansible_collections.vyos.vyos.plugins.module_utils.network. \ - vyos.vyos import load_config, run_commands - -from ansible_collections.vyos.vyos.plugins.module_utils.network. \ - vyos.vyos import vyos_argument_spec +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.vyos import ( + load_config, + run_commands, +) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.vyos import ( + vyos_argument_spec, +) def search_obj_in_list(vlan_id, lst): obj = list() for o in lst: - if o['vlan_id'] == vlan_id: + if o["vlan_id"] == vlan_id: obj.append(o) return obj @@ -141,48 +146,52 @@ def search_obj_in_list(vlan_id, lst): def map_obj_to_commands(updates, module): commands = list() want, have = updates - purge = module.params['purge'] + purge = module.params["purge"] for w in want: - vlan_id = w['vlan_id'] - name = w['name'] - address = w['address'] - state = w['state'] - interfaces = w['interfaces'] + vlan_id = w["vlan_id"] + name = w["name"] + address = w["address"] + state = w["state"] + interfaces = w["interfaces"] obj_in_have = search_obj_in_list(vlan_id, have) - if state == 'absent': + if state == "absent": if obj_in_have: for obj in obj_in_have: - for i in obj['interfaces']: - commands.append('delete interfaces ethernet {0} vif {1}'.format(i, vlan_id)) + for i in obj["interfaces"]: + commands.append( + "delete interfaces ethernet {0} vif {1}".format(i, vlan_id) + ) - elif state == 'present': + elif state == "present": if not obj_in_have: - if w['interfaces'] and w['vlan_id']: - for i in w['interfaces']: - cmd = 'set interfaces ethernet {0} vif {1}'.format(i, vlan_id) - if w['name']: - commands.append(cmd + ' description {0}'.format(name)) - elif w['address']: - commands.append(cmd + ' address {0}'.format(address)) + if w["interfaces"] and w["vlan_id"]: + for i in w["interfaces"]: + cmd = "set interfaces ethernet {0} vif {1}".format(i, vlan_id) + if w["name"]: + commands.append(cmd + " description {0}".format(name)) + elif w["address"]: + commands.append(cmd + " address {0}".format(address)) else: commands.append(cmd) if purge: for h in have: - obj_in_want = search_obj_in_list(h['vlan_id'], want) + obj_in_want = search_obj_in_list(h["vlan_id"], want) if not obj_in_want: - for i in h['interfaces']: - commands.append('delete interfaces ethernet {0} vif {1}'.format(i, h['vlan_id'])) + for i in h["interfaces"]: + commands.append( + "delete interfaces ethernet {0} vif {1}".format(i, h["vlan_id"]) + ) return commands def map_params_to_obj(module): obj = [] - aggregate = module.params.get('aggregate') + aggregate = module.params.get("aggregate") if aggregate: for item in aggregate: for key in item: @@ -191,22 +200,24 @@ def map_params_to_obj(module): d = item.copy() - if not d['vlan_id']: - module.fail_json(msg='vlan_id is required') + if not d["vlan_id"]: + module.fail_json(msg="vlan_id is required") - d['vlan_id'] = str(d['vlan_id']) + d["vlan_id"] = str(d["vlan_id"]) module._check_required_one_of(module.required_one_of, item) obj.append(d) else: - obj.append({ - 'vlan_id': str(module.params['vlan_id']), - 'name': module.params['name'], - 'address': module.params['address'], - 'state': module.params['state'], - 'interfaces': module.params['interfaces'], - 'associated_interfaces': module.params['associated_interfaces'] - }) + obj.append( + { + "vlan_id": str(module.params["vlan_id"]), + "name": module.params["name"], + "address": module.params["address"], + "state": module.params["state"], + "interfaces": module.params["interfaces"], + "associated_interfaces": module.params["associated_interfaces"], + } + ) return obj @@ -215,30 +226,30 @@ def map_config_to_obj(module): objs = [] interfaces = list() - output = run_commands(module, 'show interfaces') + output = run_commands(module, "show interfaces") lines = output[0].strip().splitlines()[3:] for l in lines: - splitted_line = re.split(r'\s{2,}', l.strip()) + splitted_line = re.split(r"\s{2,}", l.strip()) obj = {} eth = splitted_line[0].strip("'") - if eth.startswith('eth'): - obj['interfaces'] = [] - if '.' in eth: - interface = eth.split('.')[0] - obj['interfaces'].append(interface) - obj['vlan_id'] = eth.split('.')[-1] + if eth.startswith("eth"): + obj["interfaces"] = [] + if "." in eth: + interface = eth.split(".")[0] + obj["interfaces"].append(interface) + obj["vlan_id"] = eth.split(".")[-1] else: - obj['interfaces'].append(eth) - obj['vlan_id'] = None + obj["interfaces"].append(eth) + obj["vlan_id"] = None - if splitted_line[1].strip("'") != '-': - obj['address'] = splitted_line[1].strip("'") + if splitted_line[1].strip("'") != "-": + obj["address"] = splitted_line[1].strip("'") if len(splitted_line) > 3: - obj['name'] = splitted_line[3].strip("'") - obj['state'] = 'present' + obj["name"] = splitted_line[3].strip("'") + obj["state"] = "present" objs.append(obj) return objs @@ -251,41 +262,44 @@ def check_declarative_intent_params(want, module, result): is_delay = False for w in want: - if w.get('associated_interfaces') is None: + if w.get("associated_interfaces") is None: continue - if result['changed'] and not is_delay: - time.sleep(module.params['delay']) + if result["changed"] and not is_delay: + time.sleep(module.params["delay"]) is_delay = True if have is None: have = map_config_to_obj(module) - obj_in_have = search_obj_in_list(w['vlan_id'], have) + obj_in_have = search_obj_in_list(w["vlan_id"], have) if obj_in_have: for obj in obj_in_have: - obj_interface.extend(obj['interfaces']) + obj_interface.extend(obj["interfaces"]) for w in want: - if w.get('associated_interfaces') is None: + if w.get("associated_interfaces") is None: continue - for i in w['associated_interfaces']: - if (set(obj_interface) - set(w['associated_interfaces'])) != set([]): - module.fail_json(msg='Interface {0} not configured on vlan {1}'.format(i, w['vlan_id'])) + for i in w["associated_interfaces"]: + if (set(obj_interface) - set(w["associated_interfaces"])) != set([]): + module.fail_json( + msg="Interface {0} not configured on vlan {1}".format( + i, w["vlan_id"] + ) + ) def main(): """ main entry point for module execution """ element_spec = dict( - vlan_id=dict(type='int'), + vlan_id=dict(type="int"), name=dict(), address=dict(), - interfaces=dict(type='list'), - associated_interfaces=dict(type='list'), - delay=dict(default=10, type='int'), - state=dict(default='present', - choices=['present', 'absent']) + interfaces=dict(type="list"), + associated_interfaces=dict(type="list"), + delay=dict(default=10, type="int"), + state=dict(default="present", choices=["present", "absent"]), ) aggregate_spec = deepcopy(element_spec) @@ -294,43 +308,47 @@ def main(): remove_default_spec(aggregate_spec) argument_spec = dict( - aggregate=dict(type='list', elements='dict', options=aggregate_spec), - purge=dict(default=False, type='bool') + aggregate=dict(type="list", elements="dict", options=aggregate_spec), + purge=dict(default=False, type="bool"), ) argument_spec.update(element_spec) argument_spec.update(vyos_argument_spec) - required_one_of = [['vlan_id', 'aggregate'], - ['aggregate', 'interfaces', 'associated_interfaces']] - - mutually_exclusive = [['vlan_id', 'aggregate']] - module = AnsibleModule(argument_spec=argument_spec, - supports_check_mode=True, - required_one_of=required_one_of, - mutually_exclusive=mutually_exclusive) + required_one_of = [ + ["vlan_id", "aggregate"], + ["aggregate", "interfaces", "associated_interfaces"], + ] + + mutually_exclusive = [["vlan_id", "aggregate"]] + module = AnsibleModule( + argument_spec=argument_spec, + supports_check_mode=True, + required_one_of=required_one_of, + mutually_exclusive=mutually_exclusive, + ) warnings = list() - result = {'changed': False} + result = {"changed": False} if warnings: - result['warnings'] = 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 + result["commands"] = commands if commands: commit = not module.check_mode load_config(module, commands, commit=commit) - result['changed'] = True + result["changed"] = True check_declarative_intent_params(want, module, result) module.exit_json(**result) -if __name__ == '__main__': +if __name__ == "__main__": main() |