diff options
author | Bradley A. Thornton <bthornto@thethorntons.net> | 2019-08-09 10:48:27 -0700 |
---|---|---|
committer | Bradley A. Thornton <bthornto@thethorntons.net> | 2019-08-09 10:48:27 -0700 |
commit | 5fb9df4e907a6ab2da7a6c2dafdec9c1971e8d44 (patch) | |
tree | 4459ed61d06c8d8caabbcc3ab36d84e27a919ee7 /plugins/modules/vyos_static_route.py | |
parent | 7b9a33a29007ed302c3001566061e22c514cde64 (diff) | |
download | vyos-ansible-old-5fb9df4e907a6ab2da7a6c2dafdec9c1971e8d44.tar.gz vyos-ansible-old-5fb9df4e907a6ab2da7a6c2dafdec9c1971e8d44.zip |
bt_blackked
Diffstat (limited to 'plugins/modules/vyos_static_route.py')
-rw-r--r-- | plugins/modules/vyos_static_route.py | 189 |
1 files changed, 102 insertions, 87 deletions
diff --git a/plugins/modules/vyos_static_route.py b/plugins/modules/vyos_static_route.py index 777706c..dfb1d21 100644 --- a/plugins/modules/vyos_static_route.py +++ b/plugins/modules/vyos_static_route.py @@ -20,11 +20,12 @@ # ANSIBLE_METADATA = { - 'metadata_version': '1.1', - 'status': ['preview'], - 'supported_by': 'network' + "metadata_version": "1.1", + "status": ["preview"], + "supported_by": "network", } + DOCUMENTATION = """ --- module: vyos_static_route @@ -109,32 +110,37 @@ 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 get_config, load_config +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.vyos import ( + get_config, + load_config, +) -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 ( + vyos_argument_spec, +) def spec_to_commands(updates, module): commands = list() want, have = updates for w in want: - prefix = w['prefix'] - mask = w['mask'] - next_hop = w['next_hop'] - admin_distance = w['admin_distance'] - state = w['state'] - del w['state'] - - if state == 'absent' and w in have: - commands.append('delete protocols static route %s/%s' % - (prefix, mask)) - elif state == 'present' and w not in have: - cmd = 'set protocols static route %s/%s next-hop %s' % ( - prefix, mask, next_hop) - if admin_distance != 'None': - cmd += ' distance %s' % (admin_distance) + prefix = w["prefix"] + mask = w["mask"] + next_hop = w["next_hop"] + admin_distance = w["admin_distance"] + state = w["state"] + del w["state"] + + if state == "absent" and w in have: + commands.append("delete protocols static route %s/%s" % (prefix, mask)) + elif state == "present" and w not in have: + cmd = "set protocols static route %s/%s next-hop %s" % ( + prefix, + mask, + next_hop, + ) + if admin_distance != "None": + cmd += " distance %s" % (admin_distance) commands.append(cmd) return commands @@ -144,42 +150,46 @@ def config_to_dict(module): data = get_config(module) obj = [] - for line in data.split('\n'): - if line.startswith('set protocols static route'): - match = re.search(r'static route (\S+)', line, re.M) - prefix = match.group(1).split('/')[0] - mask = match.group(1).split('/')[1] - if 'next-hop' in line: - match_hop = re.search(r'next-hop (\S+)', line, re.M) + for line in data.split("\n"): + if line.startswith("set protocols static route"): + match = re.search(r"static route (\S+)", line, re.M) + prefix = match.group(1).split("/")[0] + mask = match.group(1).split("/")[1] + if "next-hop" in line: + match_hop = re.search(r"next-hop (\S+)", line, re.M) next_hop = match_hop.group(1).strip("'") - match_distance = re.search(r'distance (\S+)', line, re.M) + match_distance = re.search(r"distance (\S+)", line, re.M) if match_distance is not None: admin_distance = match_distance.group(1)[1:-1] else: admin_distance = None if admin_distance is not None: - obj.append({ - 'prefix': prefix, - 'mask': mask, - 'next_hop': next_hop, - 'admin_distance': admin_distance - }) + obj.append( + { + "prefix": prefix, + "mask": mask, + "next_hop": next_hop, + "admin_distance": admin_distance, + } + ) else: - obj.append({ - 'prefix': prefix, - 'mask': mask, - 'next_hop': next_hop, - 'admin_distance': 'None' - }) + obj.append( + { + "prefix": prefix, + "mask": mask, + "next_hop": next_hop, + "admin_distance": "None", + } + ) return obj def map_params_to_obj(module, required_together=None): obj = [] - aggregate = module.params.get('aggregate') + aggregate = module.params.get("aggregate") if aggregate: for item in aggregate: for key in item: @@ -188,32 +198,34 @@ def map_params_to_obj(module, required_together=None): module._check_required_together(required_together, item) d = item.copy() - if '/' in d['prefix']: - d['mask'] = d['prefix'].split('/')[1] - d['prefix'] = d['prefix'].split('/')[0] + if "/" in d["prefix"]: + d["mask"] = d["prefix"].split("/")[1] + d["prefix"] = d["prefix"].split("/")[0] - if 'admin_distance' in d: - d['admin_distance'] = str(d['admin_distance']) + if "admin_distance" in d: + d["admin_distance"] = str(d["admin_distance"]) obj.append(d) else: - prefix = module.params['prefix'].strip() - if '/' in prefix: - mask = prefix.split('/')[1] - prefix = prefix.split('/')[0] + prefix = module.params["prefix"].strip() + if "/" in prefix: + mask = prefix.split("/")[1] + prefix = prefix.split("/")[0] else: - mask = module.params['mask'].strip() - next_hop = module.params['next_hop'].strip() - admin_distance = str(module.params['admin_distance']) - state = module.params['state'] - - obj.append({ - 'prefix': prefix, - 'mask': mask, - 'next_hop': next_hop, - 'admin_distance': admin_distance, - 'state': state - }) + mask = module.params["mask"].strip() + next_hop = module.params["next_hop"].strip() + admin_distance = str(module.params["admin_distance"]) + state = module.params["state"] + + obj.append( + { + "prefix": prefix, + "mask": mask, + "next_hop": next_hop, + "admin_distance": admin_distance, + "state": state, + } + ) return obj @@ -221,54 +233,57 @@ def map_params_to_obj(module, required_together=None): def main(): """ main entry point for module execution """ - element_spec = dict(prefix=dict(type='str'), - mask=dict(type='str'), - next_hop=dict(type='str'), - admin_distance=dict(type='int'), - state=dict(default='present', - choices=['present', 'absent'])) + element_spec = dict( + prefix=dict(type="str"), + mask=dict(type="str"), + next_hop=dict(type="str"), + admin_distance=dict(type="int"), + state=dict(default="present", choices=["present", "absent"]), + ) aggregate_spec = deepcopy(element_spec) - aggregate_spec['prefix'] = dict(required=True) + aggregate_spec["prefix"] = dict(required=True) # remove default in aggregate spec, to handle common arguments remove_default_spec(aggregate_spec) - argument_spec = dict(aggregate=dict(type='list', - elements='dict', - options=aggregate_spec), ) + argument_spec = dict( + aggregate=dict(type="list", elements="dict", options=aggregate_spec) + ) argument_spec.update(element_spec) argument_spec.update(vyos_argument_spec) - required_one_of = [['aggregate', 'prefix']] - required_together = [['prefix', 'next_hop']] - mutually_exclusive = [['aggregate', 'prefix']] + required_one_of = [["aggregate", "prefix"]] + required_together = [["prefix", "next_hop"]] + mutually_exclusive = [["aggregate", "prefix"]] - module = AnsibleModule(argument_spec=argument_spec, - required_one_of=required_one_of, - required_together=required_together, - mutually_exclusive=mutually_exclusive, - supports_check_mode=True) + module = AnsibleModule( + argument_spec=argument_spec, + required_one_of=required_one_of, + required_together=required_together, + mutually_exclusive=mutually_exclusive, + supports_check_mode=True, + ) warnings = list() - result = {'changed': False} + result = {"changed": False} if warnings: - result['warnings'] = warnings + result["warnings"] = warnings want = map_params_to_obj(module, required_together=required_together) have = config_to_dict(module) commands = spec_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 module.exit_json(**result) -if __name__ == '__main__': +if __name__ == "__main__": main() |