diff options
20 files changed, 458 insertions, 469 deletions
@@ -7,5 +7,5 @@ license_file: LICENSE  name: vyos  namespace: vyos  readme: README.rst -repository: https://github.com/ansible-collections/vyos +repository: https://github.com/ansible-collections/vyos.vyos  tags: [vyos, networking] diff --git a/plugins/module_utils/network/vyos/argspec/lag_interfaces/lag_interfaces.py b/plugins/module_utils/network/vyos/argspec/lag_interfaces/lag_interfaces.py index 97c5d5a2..6cfdabf1 100644 --- a/plugins/module_utils/network/vyos/argspec/lag_interfaces/lag_interfaces.py +++ b/plugins/module_utils/network/vyos/argspec/lag_interfaces/lag_interfaces.py @@ -19,7 +19,6 @@  #   builder template.  #  ############################################# -  """  The arg spec for the vyos_lag_interfaces module  """ @@ -72,8 +71,17 @@ class Lag_interfacesArgs(object):  # pylint: disable=R0903              },              "type": "list",          }, +        "running_config": {"type": "str"},          "state": { -            "choices": ["merged", "replaced", "overridden", "deleted"], +            "choices": [ +                "merged", +                "replaced", +                "overridden", +                "deleted", +                "rendered", +                "gathered", +                "parsed", +            ],              "default": "merged",              "type": "str",          }, diff --git a/plugins/module_utils/network/vyos/config/lag_interfaces/lag_interfaces.py b/plugins/module_utils/network/vyos/config/lag_interfaces/lag_interfaces.py index 2a9efd99..452670f1 100644 --- a/plugins/module_utils/network/vyos/config/lag_interfaces/lag_interfaces.py +++ b/plugins/module_utils/network/vyos/config/lag_interfaces/lag_interfaces.py @@ -56,14 +56,14 @@ class Lag_interfaces(ConfigBase):      def __init__(self, module):          super(Lag_interfaces, self).__init__(module) -    def get_lag_interfaces_facts(self): +    def get_lag_interfaces_facts(self, data=None):          """ Get the 'facts' (the current configuration)          :rtype: A dictionary          :returns: The current configuration as a dictionary          """          facts, _warnings = Facts(self._module).get_facts( -            self.gather_subset, self.gather_network_resources +            self.gather_subset, self.gather_network_resources, data=data          )          lag_interfaces_facts = facts["ansible_network_resources"].get(              "lag_interfaces" @@ -79,27 +79,47 @@ class Lag_interfaces(ConfigBase):          :returns: The result from module execution          """          result = {"changed": False} -        commands = list()          warnings = list() -        existing_lag_interfaces_facts = self.get_lag_interfaces_facts() -        commands.extend(self.set_config(existing_lag_interfaces_facts)) -        if commands: -            if self._module.check_mode: -                resp = self._connection.edit_config(commands, commit=False) -            else: -                resp = self._connection.edit_config(commands) -            result["changed"] = True +        commands = list() + +        if self.state in self.ACTION_STATES: +            existing_lag_interfaces_facts = self.get_lag_interfaces_facts() +        else: +            existing_lag_interfaces_facts = [] -        result["commands"] = commands +        if self.state in self.ACTION_STATES or self.state == "rendered": +            commands.extend(self.set_config(existing_lag_interfaces_facts)) -        if self._module._diff: -            result["diff"] = resp["diff"] if result["changed"] else None +        if commands and self.state in self.ACTION_STATES: +            if not self._module.check_mode: +                self._connection.edit_config(commands) +            result["changed"] = True -        changed_lag_interfaces_facts = self.get_lag_interfaces_facts() +        if self.state in self.ACTION_STATES: +            result["commands"] = commands + +        if self.state in self.ACTION_STATES or self.state == "gathered": +            changed_lag_interfaces_facts = self.get_lag_interfaces_facts() +        elif self.state == "rendered": +            result["rendered"] = commands +        elif self.state == "parsed": +            running_config = self._module.params["running_config"] +            if not running_config: +                self._module.fail_json( +                    msg="value of running_config parameter must not be empty for state parsed" +                ) +            result["parsed"] = self.get_lag_interfaces_facts( +                data=running_config +            ) +        else: +            changed_lag_interfaces_facts = [] -        result["before"] = existing_lag_interfaces_facts -        if result["changed"]: -            result["after"] = changed_lag_interfaces_facts +        if self.state in self.ACTION_STATES: +            result["before"] = existing_lag_interfaces_facts +            if result["changed"]: +                result["after"] = changed_lag_interfaces_facts +        elif self.state == "gathered": +            result["gathered"] = changed_lag_interfaces_facts          result["warnings"] = warnings          return result @@ -127,16 +147,18 @@ class Lag_interfaces(ConfigBase):                    to the desired configuration          """          commands = [] -        state = self._module.params["state"] -        if state in ("merged", "replaced", "overridden") and not want: +        if ( +            self.state in ("merged", "replaced", "overridden", "rendered") +            and not want +        ):              self._module.fail_json(                  msg="value of config parameter must not be empty for state {0}".format( -                    state +                    self.state                  )              ) -        if state == "overridden": +        if self.state == "overridden":              commands.extend(self._state_overridden(want, have)) -        elif state == "deleted": +        elif self.state == "deleted":              if want:                  for want_item in want:                      name = want_item["name"] @@ -149,9 +171,9 @@ class Lag_interfaces(ConfigBase):              for want_item in want:                  name = want_item["name"]                  obj_in_have = search_obj_in_list(name, have) -                if state == "merged": +                if self.state in ("merged", "rendered"):                      commands.extend(self._state_merged(want_item, obj_in_have)) -                elif state == "replaced": +                elif self.state == "replaced":                      commands.extend(                          self._state_replaced(want_item, obj_in_have)                      ) diff --git a/plugins/module_utils/network/vyos/config/static_routes/static_routes.py b/plugins/module_utils/network/vyos/config/static_routes/static_routes.py index e93d4ee3..b359dbba 100644 --- a/plugins/module_utils/network/vyos/config/static_routes/static_routes.py +++ b/plugins/module_utils/network/vyos/config/static_routes/static_routes.py @@ -160,7 +160,7 @@ class Static_routes(ConfigBase):              routes = self._get_routes(want)              for r in routes:                  h_item = self.search_route_in_have(have, r["dest"]) -                if self.state == "merged" or self.state == "rendered": +                if self.state in ("merged", "rendered"):                      commands.extend(self._state_merged(want=r, have=h_item))                  elif self.state == "replaced":                      commands.extend(self._state_replaced(want=r, have=h_item)) @@ -253,12 +253,6 @@ class Static_routes(ConfigBase):                                      afi=item["afi"], remove=True                                  )                              ) -            for r in routes: -                h_route = self.search_route_in_have(have, r["dest"]) -                if h_route: -                    commands.extend( -                        self._render_updates(r, h_route, opr=False) -                    )          else:              routes = self._get_routes(have)              if self._is_ip_route_exist(routes): diff --git a/plugins/module_utils/network/vyos/facts/lag_interfaces/lag_interfaces.py b/plugins/module_utils/network/vyos/facts/lag_interfaces/lag_interfaces.py index 9201e5c6..90562947 100644 --- a/plugins/module_utils/network/vyos/facts/lag_interfaces/lag_interfaces.py +++ b/plugins/module_utils/network/vyos/facts/lag_interfaces/lag_interfaces.py @@ -12,6 +12,7 @@ based on the configuration.  from __future__ import absolute_import, division, print_function  __metaclass__ = type +  from re import findall, search, M  from copy import deepcopy @@ -59,30 +60,21 @@ class Lag_interfacesFacts(object):                  lag_regex = r" %s .+$" % lag                  cfg = findall(lag_regex, data, M)                  obj = self.render_config(cfg) - -                output = connection.run_commands( -                    ["show interfaces bonding " + lag + " slaves"] -                ) -                lines = output[0].splitlines()                  members = []                  member = {} -                if len(lines) > 1: -                    for line in lines[2:]: -                        splitted_line = line.split() - -                        if len(splitted_line) > 1: -                            member["member"] = splitted_line[0] -                            members.append(member) -                        else: -                            members = [] -                        member = {} + +                group_regex = r".*eth.* '%s'" % lag +                g_cfg = findall(group_regex, data, M) +                for item in g_cfg: +                    output = search("^set interfaces ethernet (\\S+)", item, M) +                    if output: +                        member["member"] = output.group(1).strip("'") +                        members.append(member)                  obj["name"] = lag.strip("'")                  if members:                      obj["members"] = members -                  if obj:                      objs.append(obj) -          facts = {}          if objs:              facts["lag_interfaces"] = [] diff --git a/plugins/modules/vyos_lag_interfaces.py b/plugins/modules/vyos_lag_interfaces.py index 84f3d018..1ba511c9 100644 --- a/plugins/modules/vyos_lag_interfaces.py +++ b/plugins/modules/vyos_lag_interfaces.py @@ -30,16 +30,13 @@ 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", "supported_by": "Ansible"}  DOCUMENTATION = """module: vyos_lag_interfaces -short_description: Manages attributes of link aggregation groups on VyOS network devices. +short_description: LAG interfaces resource module  description: This module manages attributes of link aggregation groups on VyOS network    devices. +version_added: "1.0.0"  notes:  - Tested against VyOS 1.1.8 (helium).  - This module works with connection C(network_cli). See L(the VyOS OS Platform Options,../network/user_guide/platform_vyos.html). @@ -100,6 +97,15 @@ options:              description:              - IP address to use for ARP monitoring.              type: list +  running_config: +    description: +      - This option is used only with state I(parsed). +      - The value of this option should be the output received from the VyOS device by executing +        the command B(show configuration commands | grep bond). +      - The state I(parsed) reads the configuration from C(running_config) option and transforms +        it into Ansible structured data as per the resource module's argspec and the value is then +        returned in the I(parsed) key within the result. +    type: str    state:      description:      - The state of the configuration after module completion. @@ -109,6 +115,9 @@ options:      - replaced      - overridden      - deleted +    - parsed +    - gathered +    - rendered      default: merged  """  EXAMPLES = """ @@ -122,7 +131,7 @@ EXAMPLES = """  # set interfaces bonding bond3  #  - name: Merge provided configuration with device configuration -  vyos_lag_interfaces: +  vyos.vyos.vyos_lag_interfaces:      config:        - name: bond2          mode: active-backup @@ -226,7 +235,7 @@ EXAMPLES = """  # set interfaces ethernet eth3 bond-group 'bond3'  #  - name: Replace device configurations of listed LAGs with provided configurations -  vyos_lag_interfaces: +  vyos.vyos.vyos_lag_interfaces:      config:        - name: bond3          mode: '802.3ad' @@ -331,7 +340,7 @@ EXAMPLES = """  # set interfaces ethernet eth3 bond-group 'bond3'  #  - name: Overrides all device configuration with provided configuration -  vyos_lag_interfaces: +  vyos.vyos.vyos_lag_interfaces:      config:        - name: bond3          mode: active-backup @@ -441,7 +450,7 @@ EXAMPLES = """  # set interfaces ethernet eth3 bond-group 'bond3'  #  - name: Delete LAG attributes of given interfaces (Note This won't delete the interface itself) -  vyos_lag_interfaces: +  vyos.vyos.vyos_lag_interfaces:      config:        - name: bond2        - name: bond3 @@ -507,6 +516,202 @@ EXAMPLES = """  # set interfaces bonding bond3 +# Using gathered +# +# Before state: +# ------------- +# +# vyos@192# run show configuration commands | grep bond +# set interfaces bonding bond0 hash-policy 'layer2' +# set interfaces bonding bond0 mode 'active-backup' +# set interfaces bonding bond0 primary 'eth1' +# set interfaces bonding bond1 hash-policy 'layer2+3' +# set interfaces bonding bond1 mode 'active-backup' +# set interfaces bonding bond1 primary 'eth2' +# set interfaces ethernet eth1 bond-group 'bond0' +# set interfaces ethernet eth2 bond-group 'bond1' +# +- name: Gather listed  lag interfaces with provided configurations +  vyos.vyos.vyos_lag_interfaces: +    config: +    state: gathered +# +# +# ------------------------- +# Module Execution Result +# ------------------------- +# +#    "gathered": [ +#        { +#            "afi": "ipv6", +#            "rule_sets": [ +#                { +#                    "default_action": "accept", +#                    "description": "This is ipv6 specific rule-set", +#                    "name": "UPLINK", +#                    "rules": [ +#                        { +#                            "action": "accept", +#                            "description": "Fwipv6-Rule 1 is configured by Ansible", +#                            "ipsec": "match-ipsec", +#                            "number": 1 +#                        }, +#                        { +#                            "action": "accept", +#                            "description": "Fwipv6-Rule 2 is configured by Ansible", +#                            "ipsec": "match-ipsec", +#                            "number": 2 +#                        } +#                    ] +#                } +#            ] +#        }, +#        { +#            "afi": "ipv4", +#            "rule_sets": [ +#                { +#                    "default_action": "accept", +#                    "description": "IPv4 INBOUND rule set", +#                    "name": "INBOUND", +#                    "rules": [ +#                        { +#                            "action": "accept", +#                            "description": "Rule 101 is configured by Ansible", +#                            "ipsec": "match-ipsec", +#                            "number": 101 +#                        }, +#                        { +#                            "action": "reject", +#                            "description": "Rule 102 is configured by Ansible", +#                            "ipsec": "match-ipsec", +#                            "number": 102 +#                        }, +#                        { +#                            "action": "accept", +#                            "description": "Rule 103 is configured by Ansible", +#                            "destination": { +#                                "group": { +#                                    "address_group": "inbound" +#                                } +#                            }, +#                            "number": 103, +#                            "source": { +#                                "address": "192.0.2.0" +#                            }, +#                            "state": { +#                                "established": true, +#                                "invalid": false, +#                                "new": false, +#                                "related": true +#                            } +#                        } +#                    ] +#                } +#            ] +#        } +#    ] +# +# +# After state: +# ------------- +# +# vyos@192# run show configuration commands | grep bond +# set interfaces bonding bond0 hash-policy 'layer2' +# set interfaces bonding bond0 mode 'active-backup' +# set interfaces bonding bond0 primary 'eth1' +# set interfaces bonding bond1 hash-policy 'layer2+3' +# set interfaces bonding bond1 mode 'active-backup' +# set interfaces bonding bond1 primary 'eth2' +# set interfaces ethernet eth1 bond-group 'bond0' +# set interfaces ethernet eth2 bond-group 'bond1' + + +# Using rendered +# +# +- name: Render the commands for provided  configuration +  vyos.vyos.vyos_lag_interfaces: +    config: +       - name: bond0 +         hash_policy: layer2 +         members: +           - member: eth1 +         mode: active-backup +         primary: eth1 +       - name: bond1 +         hash_policy: layer2+3 +         members: +           - member: eth2 +         mode: active-backup +         primary: eth2 +    state: rendered +# +# +# ------------------------- +# Module Execution Result +# ------------------------- +# +# +# "rendered": [ +#        "set interfaces bonding bond0 hash-policy 'layer2'", +#        "set interfaces ethernet eth1 bond-group 'bond0'", +#        "set interfaces bonding bond0 mode 'active-backup'", +#        "set interfaces bonding bond0 primary 'eth1'", +#        "set interfaces bonding bond1 hash-policy 'layer2+3'", +#        "set interfaces ethernet eth2 bond-group 'bond1'", +#        "set interfaces bonding bond1 mode 'active-backup'", +#        "set interfaces bonding bond1 primary 'eth2'" +#    ] + + +# Using parsed +# +# +- name: Parsed the commands for provided  configuration +  vyos.vyos.vyos_l3_interfaces: +    running_config: +      "set interfaces bonding bond0 hash-policy 'layer2' + set interfaces bonding bond0 mode 'active-backup' + set interfaces bonding bond0 primary 'eth1' + set interfaces bonding bond1 hash-policy 'layer2+3' + set interfaces bonding bond1 mode 'active-backup' + set interfaces bonding bond1 primary 'eth2' + set interfaces ethernet eth1 bond-group 'bond0' + set interfaces ethernet eth2 bond-group 'bond1'" +    state: parsed +# +# +# ------------------------- +# Module Execution Result +# ------------------------- +# +# +# "parsed": [ +#         { +#             "hash_policy": "layer2", +#             "members": [ +#                 { +#                     "member": "eth1" +#                 } +#             ], +#             "mode": "active-backup", +#             "name": "bond0", +#             "primary": "eth1" +#         }, +#         { +#             "hash_policy": "layer2+3", +#             "members": [ +#                 { +#                     "member": "eth2" +#                 } +#             ], +#             "mode": "active-backup", +#             "name": "bond1", +#             "primary": "eth2" +#         } +#     ] + +  """  RETURN = """  before: @@ -551,12 +756,17 @@ def main():      required_if = [          ("state", "merged", ("config",)),          ("state", "replaced", ("config",)), +        ("state", "rendered", ("config",)),          ("state", "overridden", ("config",)), +        ("state", "parsed", ("running_config",)),      ] +    mutually_exclusive = [("config", "running_config")] +      module = AnsibleModule(          argument_spec=Lag_interfacesArgs.argument_spec,          required_if=required_if,          supports_check_mode=True, +        mutually_exclusive=mutually_exclusive,      )      result = Lag_interfaces(module).execute_module() diff --git a/plugins/modules/vyos_static_routes.py b/plugins/modules/vyos_static_routes.py index 6e502037..e71114a1 100644 --- a/plugins/modules/vyos_static_routes.py +++ b/plugins/modules/vyos_static_routes.py @@ -30,16 +30,13 @@ 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", "supported_by": "Ansible"}  DOCUMENTATION = """module: vyos_static_routes -short_description: Manages attributes of static routes on VyOS network devices. +short_description: Static routes resource module  description: This module manages attributes of static routes on VyOS network devices.  notes: +version_added: "1.0.0"  - Tested against VyOS 1.1.8 (helium).  - This module works with connection C(network_cli). See L(the VyOS OS Platform Options,../network/user_guide/platform_vyos.html).  author: @@ -114,13 +111,12 @@ options:                      type: str    running_config:      description: -    - The module, by default, will connect to the remote device and retrieve the current -      running-config to use as a base for comparing against the contents of source. -      There are times when it is not desirable to have the task get the current running-config -      for every task in a playbook.  The I(running_config) argument allows the implementer -      to pass in the configuration to use as the base config for comparison. This -      value of this option should be the output received from device by executing -      command C(show configuration commands | grep 'static route') +      - This option is used only with state I(parsed). +      - The value of this option should be the output received from the VyOS device by executing +        the command B(show configuration commands | grep static route). +      - The state I(parsed) reads the configuration from C(running_config) option and transforms +        it into Ansible structured data as per the resource module's argspec and the value is then +        returned in the I(parsed) key within the result.      type: str    state:      description: @@ -145,7 +141,7 @@ EXAMPLES = """  # vyos@vyos:~$ show configuration  commands | grep static  #  - name: Merge the provided configuration with the exisiting running configuration -  vyos_static_routes: +  vyos.vyos.vyos_static_routes:      config:       - address_families:         - afi: 'ipv4' @@ -259,7 +255,7 @@ EXAMPLES = """  # set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::2'  #  - name: Replace device configurations of listed static routes with provided configurations -  vyos_static_routes: +  vyos.vyos.vyos_static_routes:      config:       - address_families:         - afi: 'ipv4' @@ -435,7 +431,7 @@ EXAMPLES = """  # set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::2'  #  - name: Overrides all device configuration with provided configuration -  vyos_static_routes: +  vyos.vyos.vyos_static_routes:      config:       - address_families:         - afi: 'ipv4' @@ -531,92 +527,6 @@ EXAMPLES = """  # set protocols static route 198.0.2.48/28 next-hop '192.0.2.18' -# Using deleted to delete static route based on destination -# -# Before state -# ------------- -# -# vyos@vyos:~$ show configuration commands| grep static -# set protocols static route 192.0.2.32/28 'blackhole' -# set protocols static route 192.0.2.32/28 next-hop '192.0.2.6' -# set protocols static route 192.0.2.32/28 next-hop '192.0.2.7' -# set protocols static route6 2001:db8:1000::/36 blackhole distance '2' -# set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::1' -# set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::2' -# -- name: Delete static route per destination. -  vyos_static_routes: -    config: -     - address_families: -       - afi: 'ipv4' -         routes: -           - dest: '192.0.2.32/28' -       - afi: 'ipv6' -         routes: -           - dest: '2001:db8:1000::/36' -    state: deleted -# -# -# ------------------------ -# Module Execution Results -# ------------------------ -# -#    "before": [ -#        { -#            "address_families": [ -#                { -#                    "afi": "ipv4", -#                    "routes": [ -#                        { -#                            "blackhole_config": { -#                                "type": "blackhole" -#                            }, -#                            "dest": "192.0.2.32/28", -#                            "next_hops": [ -#                                { -#                                    "forward_router_address": "192.0.2.6" -#                                }, -#                                { -#                                    "forward_router_address": "192.0.2.7" -#                                } -#                            ] -#                        } -#                    ] -#                }, -#                { -#                    "afi": "ipv6", -#                    "routes": [ -#                        { -#                            "blackhole_config": { -#                                "distance": 2 -#                            }, -#                            "dest": "2001:db8:1000::/36", -#                            "next_hops": [ -#                                { -#                                    "forward_router_address": "2001:db8:2000:2::1" -#                                }, -#                                { -#                                    "forward_router_address": "2001:db8:2000:2::2" -#                                } -#                            ] -#                        } -#                    ] -#                } -#            ] -#        } -#    ] -#    "commands": [ -#       "delete protocols static route 192.0.2.32/28", -#       "delete protocols static route6 2001:db8:1000::/36" -#    ] -# -# "after": [] -# After state -# ------------ -# vyos@vyos# run show configuration commands | grep static -# set protocols 'static' - -  # Using deleted to delete static route based on afi  #  # Before state @@ -631,7 +541,7 @@ EXAMPLES = """  # set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::2'  #  - name: Delete static route based on afi. -  vyos_static_routes: +  vyos.vyos.vyos_static_routes:      config:       - address_families:         - afi: 'ipv4' @@ -713,7 +623,7 @@ EXAMPLES = """  # set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::2'  #  - name: Delete all the static routes. -  vyos_static_routes: +  vyos.vyos.vyos_static_routes:      config:      state: deleted  # @@ -778,141 +688,11 @@ EXAMPLES = """  # set protocols 'static' -# Using deleted to delete static route based on next-hop -# -# Before state -# ------------- -# -# vyos@vyos:~$ show configuration commands| grep static -# set protocols static route 192.0.2.32/28 'blackhole' -# set protocols static route 192.0.2.32/28 next-hop '192.0.2.6' -# set protocols static route 192.0.2.32/28 next-hop '192.0.2.7' -# set protocols static route6 2001:db8:1000::/36 blackhole distance '2' -# set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::1' -# set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::2' -# -- name: Delete static routes per next-hops -  vyos_static_routes: -    config: -     - address_families: -       - afi: 'ipv4' -         routes: -           - dest: '192.0.2.32/28' -             next-hops: -               - forward_router_address: '192.0.2.6' -       - afi: 'ipv6' -         routes: -           - dest: '2001:db8:1000::/36' -             next-hops: -               - forward_router_address: '2001:db8:2000:2::1' -    state: deleted -# -# -# ------------------------ -# Module Execution Results -# ------------------------ -# -#    "before": [ -#        { -#            "address_families": [ -#                { -#                    "afi": "ipv4", -#                    "routes": [ -#                        { -#                            "blackhole_config": { -#                                "type": "blackhole" -#                            }, -#                            "dest": "192.0.2.32/28", -#                            "next_hops": [ -#                                { -#                                    "forward_router_address": "192.0.2.6" -#                                }, -#                                { -#                                    "forward_router_address": "192.0.2.7" -#                                } -#                            ] -#                        } -#                    ] -#                }, -#                { -#                    "afi": "ipv6", -#                    "routes": [ -#                        { -#                            "blackhole_config": { -#                                "distance": 2 -#                            }, -#                            "dest": "2001:db8:1000::/36", -#                            "next_hops": [ -#                                { -#                                    "forward_router_address": "2001:db8:2000:2::1" -#                                }, -#                                { -#                                    "forward_router_address": "2001:db8:2000:2::2" -#                                } -#                            ] -#                        } -#                    ] -#                } -#            ] -#        } -#    ] -#    "commands": [ -#       "delete protocols static route 192.0.2.32/28 next-hop '192.0.2.6'", -#       "delete protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::1'" -#    ] -# -#    "after": [ -#        { -#            "address_families": [ -#                { -#                    "afi": "ipv4", -#                    "routes": [ -#                        { -#                            "blackhole_config": { -#                                "type": "blackhole" -#                            }, -#                            "dest": "192.0.2.32/28", -#                            "next_hops": [ -#                                { -#                                    "forward_router_address": "192.0.2.7" -#                                } -#                            ] -#                        } -#                    ] -#                }, -#                { -#                    "afi": "ipv6", -#                    "routes": [ -#                        { -#                            "blackhole_config": { -#                                "distance": 2 -#                            }, -#                            "dest": "2001:db8:1000::/36", -#                            "next_hops": [ -#                                { -#                                    "forward_router_address": "2001:db8:2000:2::2" -#                                } -#                            ] -#                        } -#                    ] -#                } -#            ] -#        } -#    ] -# After state -# ------------ -# vyos@vyos:~$ show configuration commands| grep static -# set protocols static route 192.0.2.32/28 'blackhole' -# set protocols static route 192.0.2.32/28 next-hop '192.0.2.7' -# set protocols static route6 2001:db8:1000::/36 blackhole distance '2' -# set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::2' - -  # Using rendered  #  #  - name: Render the commands for provided  configuration -  vyos_static_routes: +  vyos.vyos.vyos_static_routes:      config:        - address_families:            - afi: 'ipv4' @@ -955,8 +735,8 @@ EXAMPLES = """  # Using parsed  #  # -- name: Render the commands for provided  configuration -  vyos_static_routes: +- name: Parse the provided running configuration +  vyos.vyos.vyos_static_routes:      running_config:        "set protocols static route 192.0.2.32/28 'blackhole'   set protocols static route 192.0.2.32/28 next-hop '192.0.2.6' @@ -1026,7 +806,7 @@ EXAMPLES = """  # set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::2'  #  - name: Gather listed static routes with provided configurations -  vyos_static_routes: +  vyos.vyos.vyos_static_routes:      config:      state: gathered  # @@ -1137,6 +917,7 @@ def main():      required_if = [          ("state", "merged", ("config",)),          ("state", "replaced", ("config",)), +        ("state", "rendered", ("config",)),          ("state", "overridden", ("config",)),          ("state", "parsed", ("running_config",)),      ] diff --git a/test-requirements.txt b/test-requirements.txt index 50373960..3f3af99d 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -1,4 +1,6 @@  black ; python_version > '3.5'  flake8 +mock  pexpect +pytest-xdist  yamllint diff --git a/tests/integration/network-integration.cfg b/tests/integration/network-integration.cfg new file mode 100644 index 00000000..d12c1efe --- /dev/null +++ b/tests/integration/network-integration.cfg @@ -0,0 +1,4 @@ +[persistent_connection] +command_timeout = 100 +connect_timeout = 100 +connect_retry_timeout = 100 diff --git a/tests/integration/targets/vyos_lag_interfaces/tests/cli/_parsed_config.cfg b/tests/integration/targets/vyos_lag_interfaces/tests/cli/_parsed_config.cfg new file mode 100644 index 00000000..ea3bfce6 --- /dev/null +++ b/tests/integration/targets/vyos_lag_interfaces/tests/cli/_parsed_config.cfg @@ -0,0 +1,8 @@ +set interfaces bonding bond0 hash-policy 'layer2' +set interfaces bonding bond0 mode 'active-backup' +set interfaces bonding bond0 primary 'eth1' +set interfaces bonding bond1 hash-policy 'layer2+3' +set interfaces bonding bond1 mode 'active-backup' +set interfaces bonding bond1 primary 'eth2' +set interfaces ethernet eth1 bond-group 'bond0' +set interfaces ethernet eth2 bond-group 'bond1'
\ No newline at end of file diff --git a/tests/integration/targets/vyos_lag_interfaces/tests/cli/empty_config.yaml b/tests/integration/targets/vyos_lag_interfaces/tests/cli/empty_config.yaml index 3894fb59..6e89eaed 100644 --- a/tests/integration/targets/vyos_lag_interfaces/tests/cli/empty_config.yaml +++ b/tests/integration/targets/vyos_lag_interfaces/tests/cli/empty_config.yaml @@ -35,3 +35,26 @@  - assert:      that:        - result.msg == 'value of config parameter must not be empty for state overridden' + +- name: Parsed with empty running_config should give appropriate error message +  register: result +  ignore_errors: true +  vyos.vyos.vyos_lag_interfaces: +    running_config: +    state: parsed + +- assert: +    that: +      - result.msg == 'value of running_config parameter must not be empty for state +        parsed' + +- name: Rendered with empty config should give appropriate error message +  register: result +  ignore_errors: true +  vyos.vyos.vyos_lag_interfaces: +    config: +    state: rendered + +- assert: +    that: +      - result.msg == 'value of config parameter must not be empty for state rendered' diff --git a/tests/integration/targets/vyos_lag_interfaces/tests/cli/gathered.yaml b/tests/integration/targets/vyos_lag_interfaces/tests/cli/gathered.yaml new file mode 100644 index 00000000..aca168dd --- /dev/null +++ b/tests/integration/targets/vyos_lag_interfaces/tests/cli/gathered.yaml @@ -0,0 +1,26 @@ +--- +- debug: +    msg: START vyos_lag_interfaces gathered integration tests on connection={{ ansible_connection +      }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate.yaml + +- block: + +    - name: Gather the provided configuration with the exisiting running configuration +      register: result +      vyos.vyos.vyos_lag_interfaces: &id001 +        config: +        state: gathered + +    - name: Assert that gathered dicts was correctly generated +      assert: +        that: +          - "{{ populate | symmetric_difference(result['gathered']) |length == 0\ +            \ }}" + +  always: + +    - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_lag_interfaces/tests/cli/parsed.yaml b/tests/integration/targets/vyos_lag_interfaces/tests/cli/parsed.yaml new file mode 100644 index 00000000..ed7bc612 --- /dev/null +++ b/tests/integration/targets/vyos_lag_interfaces/tests/cli/parsed.yaml @@ -0,0 +1,33 @@ +--- +- debug: +    msg: START vyos_lag_interfaces parsed integration tests on connection={{ ansible_connection +      }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate.yaml + +- block: + +    - name: Gather lag_interfaces facts +      register: lag_interfaces_facts +      vyos.vyos.vyos_facts: +        gather_subset: +          - default +        gather_network_resources: +          - lag_interfaces + +    - name: Provide the running configuration for parsing (config to be parsed) +      register: result +      vyos.vyos.vyos_lag_interfaces: +        running_config: "{{ lookup('file', '_parsed_config.cfg') }}" +        state: parsed + +    - name: Assert that correct parsing done +      assert: +        that: "{{ ansible_facts['network_resources']['lag_interfaces'] | symmetric_difference(result['parsed'])\ +          \ |length == 0 }}" + +  always: + +    - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_lag_interfaces/tests/cli/rendered.yaml b/tests/integration/targets/vyos_lag_interfaces/tests/cli/rendered.yaml new file mode 100644 index 00000000..e6d7928c --- /dev/null +++ b/tests/integration/targets/vyos_lag_interfaces/tests/cli/rendered.yaml @@ -0,0 +1,38 @@ +--- +- debug: +    msg: START vyos_lag_interfaces rendered integration tests on connection={{ ansible_connection +      }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate.yaml + +- block: + +    - name: Structure provided configuration into device specific commands +      register: result +      vyos.vyos.vyos_lag_interfaces: +        config: +          - name: bond0 +            hash_policy: layer2 +            members: +              - member: eth1 +            mode: active-backup +            primary: eth1 +          - name: bond1 +            hash_policy: layer2+3 +            members: +              - member: eth2 +            mode: active-backup +            primary: eth2 +        state: rendered + +    - name: Assert that correct set of commands were generated +      assert: +        that: +          - "{{ rendered['commands'] | symmetric_difference(result['rendered'])\ +            \ |length == 0 }}" + +  always: + +    - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_lag_interfaces/vars/main.yaml b/tests/integration/targets/vyos_lag_interfaces/vars/main.yaml index 57836936..9784fb97 100644 --- a/tests/integration/targets/vyos_lag_interfaces/vars/main.yaml +++ b/tests/integration/targets/vyos_lag_interfaces/vars/main.yaml @@ -83,6 +83,16 @@ deleted:    after:      - name: bond0      - name: bond1 +rendered: +  commands: +    - set interfaces bonding bond0 hash-policy 'layer2' +    - set interfaces ethernet eth1 bond-group 'bond0' +    - set interfaces bonding bond0 mode 'active-backup' +    - set interfaces bonding bond0 primary 'eth1' +    - set interfaces bonding bond1 hash-policy 'layer2+3' +    - set interfaces ethernet eth2 bond-group 'bond1' +    - set interfaces bonding bond1 mode 'active-backup' +    - set interfaces bonding bond1 primary 'eth2'  round_trip:    after:      - name: bond0 diff --git a/tests/integration/targets/vyos_static_routes/tests/cli/deleted.yaml b/tests/integration/targets/vyos_static_routes/tests/cli/deleted.yaml deleted file mode 100644 index 7f098f52..00000000 --- a/tests/integration/targets/vyos_static_routes/tests/cli/deleted.yaml +++ /dev/null @@ -1,62 +0,0 @@ ---- -- debug: -    msg: Start vyos_static_routes deleted integration tests ansible_connection={{ -      ansible_connection }} - -- include_tasks: _populate.yaml - -- block: - -    - name: Delete static route based on destiation. -      register: result -      vyos.vyos.vyos_static_routes: &id001 -        config: - -          - address_families: - -              - afi: ipv4 -                routes: - -                  - dest: 192.0.2.32/28 - -              - afi: ipv6 -                routes: - -                  - dest: 2001:db8:1000::/36 -        state: deleted - -    - name: Assert that the before dicts were correctly generated -      assert: -        that: -          - "{{ populate | symmetric_difference(result['before']) |length == 0 }}" - -    - name: Assert that the correct set of commands were generated -      assert: -        that: -          - "{{ deleted_dest['commands'] | symmetric_difference(result['commands'])\ -            \ |length == 0 }}" - -    - name: Assert that the after dicts were correctly generated -      assert: -        that: -          - "{{ deleted_dest['after'] | symmetric_difference(result['after']) |length\ -            \ == 0 }}" - -    - name: Delete attributes of given interfaces (IDEMPOTENT) -      register: result -      vyos.vyos.vyos_static_routes: *id001 - -    - name: Assert that the previous task was idempotent -      assert: -        that: -          - result.changed == false -          - result.commands|length == 0 - -    - name: Assert that the before dicts were correctly generated -      assert: -        that: -          - "{{ deleted_dest['after'] | symmetric_difference(result['before']) |length\ -            \ == 0 }}" -  always: - -    - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_static_routes/tests/cli/deleted_nh.yaml b/tests/integration/targets/vyos_static_routes/tests/cli/deleted_nh.yaml deleted file mode 100644 index f6075d26..00000000 --- a/tests/integration/targets/vyos_static_routes/tests/cli/deleted_nh.yaml +++ /dev/null @@ -1,68 +0,0 @@ ---- -- debug: -    msg: Start vyos_static_routes deleted integration tests ansible_connection={{ -      ansible_connection }} - -- include_tasks: _populate.yaml - -- block: - -    - name: Delete static route based on next_hop. -      register: result -      vyos.vyos.vyos_static_routes: &id001 -        config: - -          - address_families: - -              - afi: ipv4 -                routes: - -                  - dest: 192.0.2.32/28 -                    next_hops: - -                      - forward_router_address: 192.0.2.9 - -              - afi: ipv6 -                routes: - -                  - dest: 2001:db8:1000::/36 -                    next_hops: - -                      - forward_router_address: 2001:db8:2000:2::1 -        state: deleted - -    - name: Assert that the before dicts were correctly generated -      assert: -        that: -          - "{{ populate | symmetric_difference(result['before']) |length == 0 }}" - -    - name: Assert that the correct set of commands were generated -      assert: -        that: -          - "{{ deleted_nh['commands'] | symmetric_difference(result['commands'])\ -            \ |length == 0 }}" - -    - name: Assert that the after dicts were correctly generated -      assert: -        that: -          - "{{ deleted_nh['after'] | symmetric_difference(result['after']) |length\ -            \ == 0 }}" - -    - name: Delete attributes of given interfaces (IDEMPOTENT) -      register: result -      vyos.vyos.vyos_static_routes: *id001 - -    - name: Assert that the previous task was idempotent -      assert: -        that: -          - result.changed == false -          - result.commands|length == 0 - -    - name: Assert that the before dicts were correctly generated -      assert: -        that: -          - "{{ deleted_nh['after'] | symmetric_difference(result['before']) |length\ -            \ == 0 }}" -  always: - -    - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_static_routes/vars/main.yaml b/tests/integration/targets/vyos_static_routes/vars/main.yaml index 93b875f2..6ce4cea6 100644 --- a/tests/integration/targets/vyos_static_routes/vars/main.yaml +++ b/tests/integration/targets/vyos_static_routes/vars/main.yaml @@ -94,31 +94,7 @@ rendered:      - set protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::2'      - set protocols static route6 2001:db8:1000::/36 blackhole distance '2'      - set protocols static route6 2001:db8:1000::/36 -deleted_dest: -  commands: -    - delete protocols static route 192.0.2.32/28 -    - delete protocols static route6 2001:db8:1000::/36 -  after: [] -deleted_nh: -  commands: -    - delete protocols static route 192.0.2.32/28 next-hop '192.0.2.9' -    - delete protocols static route6 2001:db8:1000::/36 next-hop '2001:db8:2000:2::1' -  after: -    - address_families: -        - afi: ipv4 -          routes: -            - dest: 192.0.2.32/28 -              blackhole_config: -                type: blackhole -              next_hops: -                - forward_router_address: 192.0.2.10 -        - afi: ipv6 -          routes: -            - dest: 2001:db8:1000::/36 -              blackhole_config: -                distance: 2 -              next_hops: -                - forward_router_address: 2001:db8:2000:2::2 +  deleted_afi_all:    commands:      - delete protocols static route diff --git a/tests/unit/modules/network/vyos/test_vyos_static_route.py b/tests/unit/modules/network/vyos/test_vyos_static_route.py index 762508ce..8eef5fbc 100644 --- a/tests/unit/modules/network/vyos/test_vyos_static_route.py +++ b/tests/unit/modules/network/vyos/test_vyos_static_route.py @@ -21,7 +21,7 @@ from __future__ import absolute_import, division, print_function  __metaclass__ = type  from ansible_collections.vyos.vyos.tests.unit.compat.mock import patch -from ansible.modules.network.vyos import _vyos_static_route +from ansible.modules.network.vyos import vyos_static_route  from ansible_collections.vyos.vyos.tests.unit.modules.utils import (      set_module_args,  ) @@ -30,18 +30,18 @@ from .vyos_module import TestVyosModule  class TestVyosStaticRouteModule(TestVyosModule): -    module = _vyos_static_route +    module = vyos_static_route      def setUp(self):          super(TestVyosStaticRouteModule, self).setUp()          self.mock_get_config = patch( -            "ansible.modules.network.vyos._vyos_static_route.get_config" +            "ansible.modules.network.vyos.vyos_static_route.get_config"          )          self.get_config = self.mock_get_config.start()          self.mock_load_config = patch( -            "ansible.modules.network.vyos._vyos_static_route.load_config" +            "ansible.modules.network.vyos.vyos_static_route.load_config"          )          self.load_config = self.mock_load_config.start() diff --git a/tests/unit/modules/network/vyos/test_vyos_static_routes.py b/tests/unit/modules/network/vyos/test_vyos_static_routes.py index 3646d610..85c08422 100644 --- a/tests/unit/modules/network/vyos/test_vyos_static_routes.py +++ b/tests/unit/modules/network/vyos/test_vyos_static_routes.py @@ -277,17 +277,9 @@ class TestVyosStaticRoutesModule(TestVyosModule):      def test_vyos_static_routes_deleted(self):          set_module_args(              dict( -                config=[ -                    dict( -                        address_families=[ -                            dict( -                                afi="ipv4", routes=[dict(dest="192.0.2.32/28")] -                            ) -                        ] -                    ) -                ], +                config=[dict(address_families=[dict(afi="ipv4")])],                  state="deleted",              )          ) -        commands = ["delete protocols static route 192.0.2.32/28"] +        commands = ["delete protocols static route"]          self.execute_module(changed=True, commands=commands)  | 
