diff --git a/.zuul.yaml b/.zuul.yaml new file mode 100644 index 0000000..439cc66 --- /dev/null +++ b/.zuul.yaml @@ -0,0 +1,7 @@ +- project: + check: + jobs: + - ansible-tox-linters + gate: + jobs: + - ansible-tox-linters diff --git a/plugins/module_utils/network/vyos/facts/facts.py b/plugins/module_utils/network/vyos/facts/facts.py index fcb6bf0..eae9489 100644 --- a/plugins/module_utils/network/vyos/facts/facts.py +++ b/plugins/module_utils/network/vyos/facts/facts.py @@ -1,83 +1,78 @@ # Copyright 2019 Red Hat # GNU General Public License v3.0+ # (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) """ The facts class for vyos this file validates each subset of facts and selectively calls the appropriate facts gathering function """ from __future__ import absolute_import, division, print_function __metaclass__ = type from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.argspec.facts.facts import ( FactsArgs, ) from ansible.module_utils.network.common.facts.facts import FactsBase from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.interfaces.interfaces import ( InterfacesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.l3_interfaces.l3_interfaces import ( L3_interfacesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.legacy.base import ( Default, Neighbors, Config, ) -from ansible.module_utils.network.vyos.vyos import ( - run_commands, - get_capabilities, -) - FACT_LEGACY_SUBSETS = dict(default=Default, neighbors=Neighbors, config=Config) FACT_RESOURCE_SUBSETS = dict( interfaces=InterfacesFacts, l3_interfaces=L3_interfacesFacts ) class Facts(FactsBase): """ The fact class for vyos """ VALID_LEGACY_GATHER_SUBSETS = frozenset(FACT_LEGACY_SUBSETS.keys()) VALID_RESOURCE_SUBSETS = frozenset(FACT_RESOURCE_SUBSETS.keys()) def __init__(self, module): super(Facts, self).__init__(module) def get_facts( self, legacy_facts_type=None, resource_facts_type=None, data=None ): """ Collect the facts for vyos :param legacy_facts_type: List of legacy facts types :param resource_facts_type: List of resource fact types :param data: previously collected conf :rtype: dict :return: the facts gathered """ netres_choices = FactsArgs.argument_spec[ "gather_network_resources" ].get("choices", []) if self.VALID_RESOURCE_SUBSETS: self.get_network_resources_facts( netres_choices, FACT_RESOURCE_SUBSETS, resource_facts_type, data, ) if self.VALID_LEGACY_GATHER_SUBSETS: self.get_network_legacy_facts( FACT_LEGACY_SUBSETS, legacy_facts_type ) return self.ansible_facts, self._warnings diff --git a/plugins/modules/vyos_system.py b/plugins/modules/vyos_system.py index 4f0d5db..3f306f8 100644 --- a/plugins/modules/vyos_system.py +++ b/plugins/modules/vyos_system.py @@ -1,225 +1,225 @@ #!/usr/bin/python # -*- coding: utf-8 -*- # # This file is part of Ansible # # 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 . # ANSIBLE_METADATA = { "metadata_version": "1.1", "status": ["preview"], "supported_by": "network", } DOCUMENTATION = """ --- module: "vyos_system" version_added: "2.3" author: "Nathaniel Case (@Qalthos)" short_description: Run `set system` commands on VyOS devices description: - Runs one or more commands on remote devices running VyOS. This module can also be introspected to validate key parameters before returning successfully. extends_documentation_fragment: vyos notes: - Tested against VYOS 1.1.7 options: host_name: description: - Configure the device hostname parameter. This option takes an ASCII string value. domain_name: description: - The new domain name to apply to the device. name_servers: description: - A list of name servers to use with the device. Mutually exclusive with I(domain_search) aliases: ['name_server'] domain_search: description: - A list of domain names to search. Mutually exclusive with I(name_server) state: description: - Whether to apply (C(present)) or remove (C(absent)) the settings. default: present choices: ['present', 'absent'] """ RETURN = """ commands: description: The list of configuration mode commands to send to the device returned: always type: list sample: - set system hostname vyos01 - set system domain-name foo.example.com """ EXAMPLES = """ - name: configure hostname and domain-name vyos_system: host_name: vyos01 domain_name: test.example.com - name: remove all configuration vyos_system: state: absent - name: configure name servers vyos_system: name_servers - 8.8.8.8 - 8.8.4.4 - name: configure domain search suffixes vyos_system: domain_search: - sub1.example.com - sub2.example.com """ from ansible.module_utils.basic import AnsibleModule 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, ) def spec_key_to_device_key(key): device_key = key.replace("_", "-") # domain-search is longer than just it's key if device_key == "domain-search": device_key += " domain" return device_key def config_to_dict(module): data = get_config(module) config = {"domain_search": [], "name_server": []} for line in data.split("\n"): if line.startswith("set system host-name"): config["host_name"] = line[22:-1] elif line.startswith("set system domain-name"): config["domain_name"] = line[24:-1] elif line.startswith("set system domain-search domain"): config["domain_search"].append(line[33:-1]) elif line.startswith("set system name-server"): config["name_server"].append(line[24:-1]) return config def spec_to_commands(want, have): commands = [] state = want.pop("state") # state='absent' by itself has special meaning if state == "absent" and all(v is None for v in want.values()): # Clear everything for key in have: commands.append("delete system %s" % spec_key_to_device_key(key)) for key in want: if want[key] is None: continue current = have.get(key) proposed = want[key] device_key = spec_key_to_device_key(key) # These keys are lists which may need to be reconciled with the device if key in ["domain_search", "name_server"]: if not proposed: # Empty list was passed, delete all values commands.append("delete system %s" % device_key) for config in proposed: if state == "absent" and config in current: commands.append( "delete system %s '%s'" % (device_key, config) ) elif state == "present" and config not in current: commands.append( "set system %s '%s'" % (device_key, config) ) else: if state == "absent" and current and proposed: commands.append("delete system %s" % device_key) elif state == "present" and proposed and proposed != current: commands.append("set system %s '%s'" % (device_key, proposed)) return commands def map_param_to_obj(module): return { "host_name": module.params["host_name"], "domain_name": module.params["domain_name"], "domain_search": module.params["domain_search"], "name_server": module.params["name_server"], "state": module.params["state"], } def main(): argument_spec = dict( host_name=dict(type="str"), domain_name=dict(type="str"), domain_search=dict(type="list"), name_server=dict(type="list", aliases=["name_servers"]), state=dict( type="str", default="present", choices=["present", "absent"] ), ) argument_spec.update(vyos_argument_spec) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, mutually_exclusive=[("domain_name", "domain_search")], ) warnings = list() result = {"changed": False, "warnings": warnings} want = map_param_to_obj(module) have = config_to_dict(module) commands = spec_to_commands(want, have) result["commands"] = commands if commands: commit = not module.check_mode - response = load_config(module, commands, commit=commit) + load_config(module, commands, commit=commit) result["changed"] = True module.exit_json(**result) if __name__ == "__main__": main() diff --git a/plugins/modules/vyos_user.py b/plugins/modules/vyos_user.py index a309d2a..2bccd49 100644 --- a/plugins/modules/vyos_user.py +++ b/plugins/modules/vyos_user.py @@ -1,361 +1,360 @@ #!/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 . # ANSIBLE_METADATA = { "metadata_version": "1.1", "status": ["preview"], "supported_by": "network", } DOCUMENTATION = """ --- module: vyos_user version_added: "2.4" author: "Trishna Guha (@trishnaguha)" short_description: Manage the collection of local users on VyOS device description: - This module provides declarative management of the local usernames configured on network devices. It allows playbooks to manage either individual usernames or the collection of usernames in the current running config. It also supports purging usernames from the configuration that are not explicitly defined. notes: - Tested against VYOS 1.1.7 options: aggregate: description: - The set of username objects to be configured on the remote VyOS device. The list entries can either be the username or a hash of username and properties. This argument is mutually exclusive with the C(name) argument. aliases: ['users', 'collection'] name: description: - The username to be configured on the VyOS device. This argument accepts a string value and is mutually exclusive with the C(aggregate) argument. Please note that this option is not same as C(provider username). full_name: description: - The C(full_name) argument provides the full name of the user account to be created on the remote device. This argument accepts any text string value. configured_password: description: - The password to be configured on the VyOS device. The password needs to be provided in clear and it will be encrypted on the device. Please note that this option is not same as C(provider password). update_password: description: - Since passwords are encrypted in the device running config, this argument will instruct the module when to change the password. When set to C(always), the password will always be updated in the device and when set to C(on_create) the password will be updated only if the username is created. default: always choices: ['on_create', 'always'] level: description: - The C(level) argument configures the level of the user when logged into the system. This argument accepts string values admin or operator. aliases: ['role'] purge: description: - Instructs the module to consider the resource definition absolute. It will remove any previously configured usernames on the device with the exception of the `admin` user (the current defined set of users). type: bool default: false state: description: - Configures the state of the username definition as it relates to the device operational configuration. When set to I(present), the username(s) should be configured in the device active configuration and when set to I(absent) the username(s) should not be in the device active configuration default: present choices: ['present', 'absent'] extends_documentation_fragment: vyos """ EXAMPLES = """ - name: create a new user vyos_user: name: ansible configured_password: password state: present - name: remove all users except admin vyos_user: purge: yes - name: set multiple users to level operator vyos_user: aggregate: - name: netop - name: netend level: operator state: present - name: Change Password for User netop vyos_user: name: netop configured_password: "{{ new_password }}" update_password: always state: present """ RETURN = """ commands: description: The list of configuration mode commands to send to the device returned: always type: list sample: - set system login user test level operator - set system login user authentication plaintext-password password """ import re from copy import deepcopy from functools import partial 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.module_utils.six import iteritems from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.vyos import ( vyos_argument_spec, ) def validate_level(value, module): if value not in ("admin", "operator"): module.fail_json( msg="level must be either admin or operator, got %s" % value ) def spec_to_commands(updates, module): commands = list() - state = module.params["state"] update_password = module.params["update_password"] def needs_update(want, have, x): return want.get(x) and (want.get(x) != have.get(x)) def add(command, want, x): command.append("set system login user %s %s" % (want["name"], x)) for update in updates: want, have = update if want["state"] == "absent": commands.append("delete system login user %s" % want["name"]) continue if needs_update(want, have, "level"): add(commands, want, "level %s" % want["level"]) if needs_update(want, have, "full_name"): add(commands, want, "full-name %s" % want["full_name"]) if needs_update(want, have, "configured_password"): if update_password == "always" or not have: add( commands, want, "authentication plaintext-password %s" % want["configured_password"], ) return commands def parse_level(data): match = re.search(r"level (\S+)", data, re.M) if match: level = match.group(1)[1:-1] return level def parse_full_name(data): match = re.search(r"full-name (\S+)", data, re.M) if match: full_name = match.group(1)[1:-1] return full_name def config_to_dict(module): data = get_config(module) match = re.findall(r"^set system login user (\S+)", data, re.M) if not match: return list() instances = list() for user in set(match): regex = r" %s .+$" % user cfg = re.findall(regex, data, re.M) cfg = "\n".join(cfg) obj = { "name": user, "state": "present", "configured_password": None, "level": parse_level(cfg), "full_name": parse_full_name(cfg), } instances.append(obj) return instances def get_param_value(key, item, module): # if key doesn't exist in the item, get it from module.params if not item.get(key): value = module.params[key] # validate the param value (if validator func exists) validator = globals().get("validate_%s" % key) if all((value, validator)): validator(value, module) return value def map_params_to_obj(module): aggregate = module.params["aggregate"] if not aggregate: if not module.params["name"] and module.params["purge"]: return list() else: users = [{"name": module.params["name"]}] else: users = list() for item in aggregate: if not isinstance(item, dict): users.append({"name": item}) else: users.append(item) objects = list() for item in users: get_value = partial(get_param_value, item=item, module=module) item["configured_password"] = get_value("configured_password") item["full_name"] = get_value("full_name") item["level"] = get_value("level") item["state"] = get_value("state") objects.append(item) return objects def update_objects(want, have): updates = list() for entry in want: item = next((i for i in have if i["name"] == entry["name"]), None) if item is None: updates.append((entry, {})) elif item: for key, value in iteritems(entry): if value and value != item[key]: updates.append((entry, item)) return updates def main(): """ main entry point for module execution """ element_spec = dict( name=dict(), full_name=dict(), level=dict(aliases=["role"]), configured_password=dict(no_log=True), update_password=dict( default="always", choices=["on_create", "always"] ), state=dict(default="present", choices=["present", "absent"]), ) aggregate_spec = deepcopy(element_spec) aggregate_spec["name"] = 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, aliases=["users", "collection"], ), purge=dict(type="bool", default=False), ) argument_spec.update(element_spec) argument_spec.update(vyos_argument_spec) mutually_exclusive = [("name", "aggregate")] module = AnsibleModule( argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True, ) warnings = list() if module.params["password"] and not module.params["configured_password"]: warnings.append( 'The "password" argument is used to authenticate the current connection. ' + 'To set a user password use "configured_password" instead.' ) result = {"changed": False} if warnings: result["warnings"] = warnings want = map_params_to_obj(module) have = config_to_dict(module) commands = spec_to_commands(update_objects(want, have), module) if module.params["purge"]: want_users = [x["name"] for x in want] have_users = [x["name"] for x in have] for item in set(have_users).difference(want_users): commands.append("delete system login user %s" % item) 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() diff --git a/plugins/modules/vyos_vlan.py b/plugins/modules/vyos_vlan.py index 7c3fa69..6c0fad8 100644 --- a/plugins/modules/vyos_vlan.py +++ b/plugins/modules/vyos_vlan.py @@ -1,368 +1,366 @@ #!/usr/bin/python # -*- coding: utf-8 -*- # Copyright: (c) 2017, Ansible by Red Hat, inc # 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", } DOCUMENTATION = """ --- module: vyos_vlan version_added: "2.5" author: "Trishna Guha (@trishnaguha)" short_description: Manage VLANs on VyOS network devices description: - This module provides declarative management of VLANs on VyOS network devices. notes: - Tested against VYOS 1.1.7 options: name: description: - Name of the VLAN. address: description: - Configure Virtual interface address. vlan_id: description: - ID of the VLAN. Range 0-4094. required: true interfaces: description: - List of interfaces that should be associated to the VLAN. required: true associated_interfaces: description: - This is a intent option and checks the operational state of the for given vlan C(name) for associated interfaces. If the value in the C(associated_interfaces) does not match with the operational state of vlan on device it will result in failure. version_added: "2.5" delay: description: - Delay the play should wait to check for declarative intent params values. default: 10 aggregate: description: List of VLANs definitions. purge: description: - Purge VLANs not defined in the I(aggregate) parameter. default: no type: bool state: description: - State of the VLAN configuration. default: present choices: ['present', 'absent'] extends_documentation_fragment: vyos """ EXAMPLES = """ - name: Create vlan vyos_vlan: vlan_id: 100 name: vlan-100 interfaces: eth1 state: present - name: Add interfaces to VLAN vyos_vlan: vlan_id: 100 interfaces: - eth1 - eth2 - name: Configure virtual interface address vyos_vlan: vlan_id: 100 interfaces: eth1 address: 172.26.100.37/24 - name: vlan interface config + intent vyos_vlan: vlan_id: 100 interfaces: eth0 associated_interfaces: - eth0 - name: vlan intent check vyos_vlan: vlan_id: 100 associated_interfaces: - eth3 - eth4 - name: Delete vlan vyos_vlan: vlan_id: 100 interfaces: eth1 state: absent """ RETURN = """ commands: description: The list of configuration mode commands to send to the device returned: always type: list sample: - set interfaces ethernet eth1 vif 100 description VLAN 100 - set interfaces ethernet eth1 vif 100 address 172.26.100.37/24 - delete interfaces ethernet eth1 vif 100 """ import re import time 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, ) def search_obj_in_list(vlan_id, lst): obj = list() for o in lst: if o["vlan_id"] == vlan_id: obj.append(o) return obj def map_obj_to_commands(updates, module): commands = list() want, have = updates 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"] obj_in_have = search_obj_in_list(vlan_id, have) 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 ) ) 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) ) else: commands.append(cmd) if purge: for h in have: 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"] ) ) return commands def map_params_to_obj(module): obj = [] aggregate = module.params.get("aggregate") if aggregate: for item in aggregate: for key in item: if item.get(key) is None: item[key] = module.params[key] d = item.copy() if not d["vlan_id"]: module.fail_json(msg="vlan_id is required") 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" ], } ) return obj def map_config_to_obj(module): objs = [] - interfaces = list() 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()) 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] else: obj["interfaces"].append(eth) obj["vlan_id"] = None 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" objs.append(obj) return objs def check_declarative_intent_params(want, module, result): have = None obj_interface = list() is_delay = False for w in want: if w.get("associated_interfaces") is None: continue 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) if obj_in_have: for obj in obj_in_have: obj_interface.extend(obj["interfaces"]) for w in want: 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"] ) ) def main(): """ main entry point for module execution """ element_spec = dict( 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"]), ) aggregate_spec = deepcopy(element_spec) # 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), 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, ) warnings = list() 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 check_declarative_intent_params(want, module, result) module.exit_json(**result) if __name__ == "__main__": main() diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e69de29 diff --git a/test-requirements.txt b/test-requirements.txt new file mode 100644 index 0000000..4e92b9d --- /dev/null +++ b/test-requirements.txt @@ -0,0 +1,2 @@ +black +flake8 diff --git a/tox.ini b/tox.ini new file mode 100644 index 0000000..87f00fa --- /dev/null +++ b/tox.ini @@ -0,0 +1,23 @@ +[tox] +minversion = 1.4.2 +envlist = linters +skipsdist = True + +[testenv] +deps = -r{toxinidir}/requirements.txt + -r{toxinidir}/test-requirements.txt + +[testenv:linters] +install_command = pip install {opts} {packages} +commands = + black -v -l79 --check {toxinidir} + flake8 {posargs} + +[flake8] +# E123, E125 skipped as they are invalid PEP-8. + +show-source = True +ignore = E123,E125,E402,W503 +max-line-length = 160 +builtins = _ +exclude = .git,.tox