diff --git a/README.md b/README.md index 93852a2b..c2582085 100644 --- a/README.md +++ b/README.md @@ -1,298 +1,299 @@ # VyOS Collection [![codecov](https://codecov.io/gh/vyos/vyos.vyos/graph/badge.svg?token=J217GFD69W)](https://codecov.io/gh/vyos/vyos.vyos) [![CI](https://github.com/vyos/vyos.vyos/actions/workflows/tests.yml/badge.svg?branch=main&event=schedule)](https://github.com/vyos/vyos.vyos/actions/workflows/tests.yml) The Ansible VyOS collection includes a variety of Ansible content to help automate the management of VyOS network appliances. This collection has been tested against VyOS 1.3.8, 1.4.1 and the current rolling release for 1.5. Where possible, compatibility with older versions of VyOS are maintained but not guaranteed. ## Communication * Join the VyOS forum: * [FAQ](https://forum.vyos.io/faq): find answers to frequently asked questions. * [Guides and How To](https://forum.vyos.io/c/howto-guies/27): find guides and how-to articles. * [News & Announcements](https://forum.vyos.io/c/announcements/6): track project-wide announcements . ## Ansible version compatibility This collection has been tested against the following Ansible versions: **>=2.16.0**. Plugins and modules within a collection may be tested with only specific Ansible versions. A collection may contain metadata that identifies these versions. PEP440 is the schema used to describe the versions of Ansible. ### Supported connections The VyOS collection supports ``network_cli`` connections. ## Requirements - ansible-core >=2.16.0, see [Ansible version compatibility](#ansible-version-compatibility). - Python >=3.10 on the Ansible controller, as required by ansible-core 2.16. - [ansible.netcommon](https://galaxy.ansible.com/ui/repo/published/ansible/netcommon/) >=2.5.1, installed automatically as a dependency of this collection. - Python packages `paramiko` and `scp`, listed in [requirements.txt](https://github.com/vyos/vyos.vyos/blob/main/requirements.txt). `ansible-builder` picks these up when you build an execution environment that includes this collection. - VyOS devices reachable over SSH with the `ansible.netcommon.network_cli` connection. ## Included content ### Cliconf plugins Name | Description --- | --- [vyos.vyos.vyos](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_cliconf.rst)|Use vyos cliconf to run command on VyOS platform ### Modules Name | Description --- | --- [vyos.vyos.vyos_banner](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_banner_module.rst)|Manage multiline banners on VyOS devices [vyos.vyos.vyos_bgp_address_family](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_bgp_address_family_module.rst)|BGP Address Family resource module [vyos.vyos.vyos_bgp_global](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_bgp_global_module.rst)|BGP global resource module [vyos.vyos.vyos_command](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_command_module.rst)|Run one or more commands on VyOS devices [vyos.vyos.vyos_config](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_config_module.rst)|Manage VyOS configuration on remote device [vyos.vyos.vyos_facts](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_facts_module.rst)|Get facts about vyos devices. [vyos.vyos.vyos_file](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_file_module.rst)|Manage files, directories, and their ownership on VyOS devices [vyos.vyos.vyos_firewall_global](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_firewall_global_module.rst)|Firewall global resource module [vyos.vyos.vyos_firewall_interfaces](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_firewall_interfaces_module.rst)|Firewall interfaces resource module [vyos.vyos.vyos_firewall_rules](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_firewall_rules_module.rst)|Firewall rules resource module [vyos.vyos.vyos_ha](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ha_module.rst)|Manage VRRP and load balancer configuration on VyOS [vyos.vyos.vyos_hostname](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_hostname_module.rst)|Manages hostname resource module [vyos.vyos.vyos_interfaces](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_interfaces_module.rst)|Manages interface attributes of VyOS network devices. [vyos.vyos.vyos_l3_interfaces](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_l3_interfaces_module.rst)|Layer 3 interfaces resource module. [vyos.vyos.vyos_lag_interfaces](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_lag_interfaces_module.rst)|LAG interfaces resource module [vyos.vyos.vyos_lldp_global](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_lldp_global_module.rst)|LLDP global resource module [vyos.vyos.vyos_lldp_interfaces](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_lldp_interfaces_module.rst)|LLDP interfaces resource module [vyos.vyos.vyos_logging_global](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_logging_global_module.rst)|Logging resource module [vyos.vyos.vyos_nat](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_nat_module.rst)|NAT resource module [vyos.vyos.vyos_ntp_global](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ntp_global_module.rst)|NTP global resource module [vyos.vyos.vyos_ospf_interfaces](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ospf_interfaces_module.rst)|OSPF Interfaces Resource Module. [vyos.vyos.vyos_ospfv2](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ospfv2_module.rst)|OSPFv2 resource module [vyos.vyos.vyos_ospfv3](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ospfv3_module.rst)|OSPFv3 resource module [vyos.vyos.vyos_ping](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ping_module.rst)|Tests reachability using ping from VyOS network devices [vyos.vyos.vyos_prefix_lists](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_prefix_lists_module.rst)|Prefix-Lists resource module for VyOS [vyos.vyos.vyos_route_maps](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_route_maps_module.rst)|Route Map resource module [vyos.vyos.vyos_snmp_server](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_snmp_server_module.rst)|Manages snmp_server resource module [vyos.vyos.vyos_static_routes](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_static_routes_module.rst)|Static routes resource module [vyos.vyos.vyos_system](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_system_module.rst)|Run `set system` commands on VyOS devices [vyos.vyos.vyos_user](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_user_module.rst)|Manage the collection of local users on VyOS device [vyos.vyos.vyos_vlan](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_vlan_module.rst)|Manage VLANs on VyOS network devices [vyos.vyos.vyos_vpn_ipsec](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_vpn_ipsec_module.rst)|Manages global IPsec (ike-group, esp-group, profile, authentication, options) attributes of VyOS network devices. [vyos.vyos.vyos_vpn_ipsec_s2s](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_vpn_ipsec_s2s_module.rst)|Manages IPsec site-to-site VPN peers on VyOS network devices. +[vyos.vyos.vyos_vpp_acl](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_vpp_acl_module.rst)|VPP dataplane ACL resource module [vyos.vyos.vyos_vrf](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_vrf_module.rst)|VRF resource module Click the ``Content`` button to see the list of content included in this collection. ## Installing this collection You can install the VyOS collection with the Ansible Galaxy CLI: ansible-galaxy collection install vyos.vyos Red Hat customers should install this collection from [Red Hat Ansible Automation Hub](https://console.redhat.com/ansible/automation-hub/repo/published/vyos/vyos/). Configure Automation Hub as a Galaxy server in `ansible.cfg` as described in [Configuring Ansible Automation Hub](https://docs.redhat.com/en/documentation/red_hat_ansible_automation_platform/), then use the same commands as below. Red Hat does not support installing collections from GitHub. To install a specific version, for example 6.2.0: ansible-galaxy collection install vyos.vyos:==6.2.0 To upgrade to the latest version: ansible-galaxy collection install vyos.vyos --upgrade You can also include it in a `requirements.yml` file and install it with `ansible-galaxy collection install -r requirements.yml`, using the format: ```yaml --- collections: - name: vyos.vyos ``` ## Using this collection This collection includes [network resource modules](https://docs.ansible.com/ansible/latest/network/user_guide/network_resource_modules.html). ### Using modules from the VyOS collection in your playbooks You can call modules by their Fully Qualified Collection Namespace (FQCN), such as `vyos.vyos.vyos_static_routes`. The following example task replaces configuration changes in the existing configuration on a VyOS network device, using the FQCN: ```yaml --- - name: Replace device configurations of listed static routes with provided configurations register: result vyos.vyos.vyos_static_routes: &id001 config: - address_families: - afi: ipv4 routes: - dest: 192.0.2.32/28 blackhole_config: distance: 2 next_hops: - forward_router_address: 192.0.2.7 - forward_router_address: 192.0.2.8 - forward_router_address: 192.0.2.9 state: replaced ``` **NOTE**: For Ansible 2.9, you may not see deprecation warnings when you run your playbooks with this collection. Use this documentation to track when a module is deprecated. ### See Also: * [VyOS Platform Options](https://docs.ansible.com/ansible/latest/network/user_guide/platform_vyos.html) * [Ansible Using collections](https://docs.ansible.com/ansible/latest/user_guide/collections_using.html) for more details. ## Contributing to this collection We welcome community contributions to this collection. If you find problems, please open an issue or create a PR against the [VyOS collection repository](https://github.com/vyos/vyos.vyos). See [Contributing to VyOS](https://vyos.net/contribute/) for complete details. You can also join us on: - Forum - https://forum.vyos.io See the [Contributing to VyOS](https://vyos.net/contribute/) for details on contributing to Ansible. ### Code of Conduct This collection follows the Ansible project's [Code of Conduct](https://docs.ansible.com/ansible/devel/community/code_of_conduct.html). Please read and familiarize yourself with this document. ### Updating from resource module models Some of our modules were templated using `resource_module_builder`, but some use the newer [`cli_rm_builder`](https://github.com/ansible-network/cli_rm_builder) which tempaltes baed on in-place device information, but also uses a new network parsing engine designed to simplify and standardize the parsing of network configuration. #### Using older *resource_module_builder* modules Last build was with a slightly-modified version of resource_module_builder. This changes the calling parameters for the resources. To update the collection from the resource module models, run the following command: ```bash ansible-playbook -e rm_dest=`pwd` \ -e structure=collection \ -e collection_org=vyos \ -e collection_name=vyos \ -e model=../../../resource_module_models/models/vyos/firewall_rules/vyos_firewall_rules.yaml \ ../../../resource_module_builder/site.yml ``` #### Using *cli_rm_builder* modules The newer `cli_rm_builder` works similarly to the older `resource_module_builder`, but pulls the information directly from the `DOCUMENTATION`, `EXAMPLES` and `RETURN` blocks in the module itself. To update the collection from the `cli_rm_builder` models, run the following command: ```bash ansible-playbook -e rm_dest=`pwd` \ -e collection_org=vyos \ -e collection_name=vyos \ -e resource=bgp_address_family \ ../../../cli_rm_builder/run.yml ``` Unlike the `resource_module_builder`, the `cli_rm_builder` does not require the `model` parameter. Instead, it uses the `resource` parameter to specify the resource to build. ### Testing playbooks You can use `ANSIBLE_COLLECTIONS_PATH` to test the collection locally. For example: ``` ANSIBLE_COLLECTIONS_PATHS=~/my_dev_path ansible-playbook -i inventory.network test.yml ``` ### Integration Tests Integration tests are run using `ansible-test` and require that there be an inventory defined (you can pass this in with `--inventory `) and that the system be configured for access (recommended to use SSH keys). Additionally: - eth0 should be configured for `address dhcp` and should have an assigned address on the local network - eth1 and eth2 should be defined and uncofirgured (they'll be overwritten by the tests) - eth3 and beyond should not be present or interface-related tests will fail - when using VMs for testing, ensure that the interfaces don't use `virtio`, as it will supress some interface configurations. `e1000e` is a good choice for testing. - eth0 is also expected to show `duplex auto` and `speed auto` in the output of `show interfaces`, however others are not due to the fact that they are repeatedly deleted and recreated which causes the default values to be hidden. ## Changelogs Change logs are available [here](https://github.com/vyos/vyos.vyos/blob/main/CHANGELOG.rst). ## Release notes Release notes are available [here](https://github.com/vyos/vyos.vyos/blob/main/CHANGELOG.rst). ## Roadmap Major Version | Ansible Support | VyOS Support | Details --- | --- | --- | --- 4.1.0 | 2.15 | 1.1.2 | Final release for the 4.x series 5.0.0 | 2.16 | 1.1.2 | First relase under VyOS control as a separate collection 6.0.0 | 2.18 | 1.3.8 | *Planned* release for supporting VyOS 1.3.8+ 7.0.0 | x.xx | 1.4.x | *Prospective* release deprecating incompatible 1.3.x modules Note: - Unreleased versions are not guaranteed to be released as described. - Some modules may support a wider variety of versions depending upon the compatibility with prior versions of VyOS. - The roadmap is subject to change based on community feedback and contributions. ## More information VyOS resources - [Contributing to VyOS](https://vyos.net/contribute) - [VyOS documentation](https://docs.vyos.io/en/latest/) - [VyOS forum](https://forum.vyos.io) Ansible Resources - [Ansible network resources](https://docs.ansible.com/ansible/latest/network/getting_started/network_resources.html) - [Ansible Collection overview](https://github.com/ansible-collections/overview) - [Ansible User guide](https://docs.ansible.com/ansible/latest/user_guide/index.html) - [Ansible Developer guide](https://docs.ansible.com/ansible/latest/dev_guide/index.html) - [Ansible Community code of conduct](https://docs.ansible.com/ansible/latest/community/code_of_conduct.html) ## Support As Red Hat Ansible Certified Content, this collection is entitled to support through Ansible Automation Platform (AAP) using the **Create issue** button in the top right corner of the collection page on Automation Hub. The collection is maintained by the VyOS team. If the collection was obtained from Galaxy or GitHub, community help is available through [GitHub issues](https://github.com/vyos/vyos.vyos/issues), the [VyOS forum](https://forum.vyos.io/) and the [Ansible Forum](https://forum.ansible.com/). ## Licensing GNU General Public License v3.0 or later. See [LICENSE](https://www.gnu.org/licenses/gpl-3.0.txt) to see the full text. diff --git a/changelogs/fragments/T9696_vpp_acl_module.yml b/changelogs/fragments/T9696_vpp_acl_module.yml new file mode 100644 index 00000000..8fe02e50 --- /dev/null +++ b/changelogs/fragments/T9696_vpp_acl_module.yml @@ -0,0 +1,5 @@ +--- +minor_changes: + - vyos_vpp_acl - new resource module for the access control lists of the VPP dataplane + (vpp acl) - IP ACLs with protocol, prefix, port and TCP flag matches, MAC ACLs, and their + bindings to dataplane interfaces (IP ACLs per direction, ordered by ACL tag). diff --git a/docs/vyos.vyos.vyos_vpp_acl_module.rst b/docs/vyos.vyos.vyos_vpp_acl_module.rst new file mode 100644 index 00000000..250958c4 --- /dev/null +++ b/docs/vyos.vyos.vyos_vpp_acl_module.rst @@ -0,0 +1,1227 @@ +.. _vyos.vyos.vyos_vpp_acl_module: + + +********************** +vyos.vyos.vyos_vpp_acl +********************** + +**VPP dataplane ACL resource module** + + +Version added: 7.0.0 + +.. contents:: + :local: + :depth: 1 + + +Synopsis +-------- +- This module manages the access control lists of the accelerated dataplane (VPP) on devices running VyOS - IP and MAC ACLs, and their bindings to dataplane interfaces. + + + + +Parameters +---------- + +.. raw:: html + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ParameterChoices/DefaultsComments
+
+ config + +
+ dictionary +
+
+ +
The VPP dataplane ACL configuration.
+
+
+ ip + +
+ dictionary +
+
+ +
IP ACLs and their bindings.
+
+
+ acls + +
+ list + / elements=dictionary +
+
+ +
IP ACLs (vpp acl ip tag-name).
+
+
+ description + +
+ string +
+
+ +
The ACL description.
+
VyOS does not accept the single quote character in values.
+
+
+ name + +
+ string + / required +
+
+ +
The ACL name.
+
+
+ rules + +
+ list + / elements=dictionary +
+
+ +
The ACL rules.
+
+
+ action + +
+ string +
+
+
    Choices: +
  • permit
  • +
  • deny
  • +
  • permit-reflect
  • +
+
+
The action for matching packets.
+
+
+ description + +
+ string +
+
+ +
The rule description.
+
+
+ destination + +
+ dictionary +
+
+ +
The destination to match.
+
+
+ port + +
+ string +
+
+ +
The destination port or port range.
+
+
+ prefix + +
+ string +
+
+ +
The IPv4 or IPv6 destination prefix.
+
+
+ number + +
+ integer + / required +
+
+ +
The rule number.
+
+
+ protocol + +
+ string +
+
+ +
The protocol to match, as a name or number, or all.
+
+
+ source + +
+ dictionary +
+
+ +
The source to match.
+
+
+ port + +
+ string +
+
+ +
The source port or port range, for example 1024-65535.
+
+
+ prefix + +
+ string +
+
+ +
The IPv4 or IPv6 source prefix.
+
+
+ tcp_flags + +
+ dictionary +
+
+ +
The TCP flags to match.
+
+
+ is_not_set + +
+ list + / elements=string +
+
+
    Choices: +
  • ack
  • +
  • cwr
  • +
  • ecn
  • +
  • fin
  • +
  • psh
  • +
  • rst
  • +
  • syn
  • +
  • urg
  • +
+
+
The TCP flags that must not be set.
+
+
+ is_set + +
+ list + / elements=string +
+
+
    Choices: +
  • ack
  • +
  • cwr
  • +
  • ecn
  • +
  • fin
  • +
  • psh
  • +
  • rst
  • +
  • syn
  • +
  • urg
  • +
+
+
The TCP flags that must be set.
+
+
+ interfaces + +
+ list + / elements=dictionary +
+
+ +
The IP ACLs bound to dataplane interfaces.
+
+
+ input + +
+ list + / elements=dictionary +
+
+ +
The ACLs applied to incoming traffic, in the order of acl_tag.
+
+
+ acl_tag + +
+ integer + / required +
+
+ +
The position of the ACL in the list.
+
+
+ tag_name + +
+ string + / required +
+
+ +
The name of the IP ACL.
+
+
+ name + +
+ string + / required +
+
+ +
The interface name, for example eth1.
+
+
+ output + +
+ list + / elements=dictionary +
+
+ +
The ACLs applied to outgoing traffic, in the order of acl_tag.
+
+
+ acl_tag + +
+ integer + / required +
+
+ +
The position of the ACL in the list.
+
+
+ tag_name + +
+ string + / required +
+
+ +
The name of the IP ACL.
+
+
+ mac + +
+ dictionary +
+
+ +
MAC ACLs and their bindings.
+
+
+ acls + +
+ list + / elements=dictionary +
+
+ +
MAC ACLs (vpp acl mac tag-name).
+
+
+ description + +
+ string +
+
+ +
The ACL description.
+
+
+ name + +
+ string + / required +
+
+ +
The ACL name.
+
+
+ rules + +
+ list + / elements=dictionary +
+
+ +
The ACL rules.
+
+
+ action + +
+ string +
+
+
    Choices: +
  • permit
  • +
  • deny
  • +
+
+
The action for matching packets.
+
+
+ description + +
+ string +
+
+ +
The rule description.
+
+
+ mac_address + +
+ string +
+
+ +
The MAC address to match.
+
+
+ mac_mask + +
+ string +
+
+ +
The mask applied to mac_address.
+
+
+ number + +
+ integer + / required +
+
+ +
The rule number.
+
+
+ prefix + +
+ string +
+
+ +
The IPv4 or IPv6 prefix to match.
+
+
+ interfaces + +
+ list + / elements=dictionary +
+
+ +
The MAC ACLs bound to dataplane interfaces, one per interface.
+
+
+ name + +
+ string + / required +
+
+ +
The interface name, for example eth1.
+
+
+ tag_name + +
+ string +
+
+ +
The name of the MAC ACL.
+
+
+ running_config + +
+ string +
+
+ +
This option is used only with state parsed.
+
The value of this option should be the output received from the VyOS device by executing the command show configuration commands | grep 'vpp acl'.
+
The state parsed reads the configuration from 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 parsed key within the result.
+
+
+ state + +
+ string +
+
+
    Choices: +
  • merged ←
  • +
  • replaced
  • +
  • overridden
  • +
  • deleted
  • +
  • gathered
  • +
  • rendered
  • +
  • parsed
  • +
+
+
The state the configuration should be left in.
+
+
+ + +Notes +----- + +.. note:: + - Tested against VyOS 1.5.1. + - This module works with connection ``network_cli``. + - ACLs can only be bound to interfaces that are part of the dataplane (``vpp settings interface``). + - IP and MAC ACLs are handled independently. With *state=replaced*, the listed ACLs and interface bindings are replaced as given; with *state=overridden*, ACLs and bindings that are not listed are also deleted, bindings before ACLs. + - When not configured, VyOS uses the protocol ``all`` for IP rules and the mask ``ff:ff:ff:ff:ff:ff`` for MAC rules; the module reports only configured values. + - For more information on using Ansible to manage network devices see the :ref:`Ansible Network Guide ` + + + +Examples +-------- + +.. code-block:: yaml + + # Using merged + + # Before state: + # ------------- + # + # vyos@vyos:~$ show configuration commands | grep 'vpp acl' + # + + - name: Permit HTTPS from partners into eth1 + vyos.vyos.vyos_vpp_acl: + config: + ip: + acls: + - name: WEB-IN + description: Inbound web traffic + rules: + - number: 10 + action: permit + protocol: tcp + source: + prefix: 192.0.2.0/24 + destination: + prefix: 198.51.100.10/32 + port: "443" + - number: 20 + action: deny + interfaces: + - name: eth1 + input: + - acl_tag: 1 + tag_name: WEB-IN + state: merged + + # Task output + # ----------- + # + # commands: + # - set vpp acl ip tag-name WEB-IN description 'Inbound web traffic' + # - set vpp acl ip tag-name WEB-IN rule 10 action permit + # - set vpp acl ip tag-name WEB-IN rule 10 protocol tcp + # - set vpp acl ip tag-name WEB-IN rule 10 source prefix 192.0.2.0/24 + # - set vpp acl ip tag-name WEB-IN rule 10 destination prefix 198.51.100.10/32 + # - set vpp acl ip tag-name WEB-IN rule 10 destination port 443 + # - set vpp acl ip tag-name WEB-IN rule 20 action deny + # - set vpp acl ip interface eth1 input acl-tag 1 tag-name WEB-IN + + # Using merged, MAC ACL + + - name: Permit one vendor's MAC addresses on eth1 + vyos.vyos.vyos_vpp_acl: + config: + mac: + acls: + - name: MAC-FILTER + rules: + - number: 10 + action: permit + mac_address: 00:11:22:00:00:00 + mac_mask: ff:ff:ff:00:00:00 + interfaces: + - name: eth1 + tag_name: MAC-FILTER + state: merged + + # Using replaced + + # Before state: + # ------------- + # + # set vpp acl ip tag-name WEB-IN rule 10 action 'permit' + # set vpp acl ip tag-name WEB-IN rule 10 protocol 'tcp' + # set vpp acl ip tag-name WEB-IN rule 20 action 'deny' + + - name: Replace the rules of WEB-IN + vyos.vyos.vyos_vpp_acl: + config: + ip: + acls: + - name: WEB-IN + rules: + - number: 10 + action: permit + state: replaced + + # Task output + # ----------- + # + # commands: + # - delete vpp acl ip tag-name WEB-IN rule 10 protocol + # - delete vpp acl ip tag-name WEB-IN rule 20 + + # Using overridden + + - name: Keep only the WEB-IN ACL and its binding + vyos.vyos.vyos_vpp_acl: + config: + ip: + acls: + - name: WEB-IN + rules: + - number: 10 + action: permit + interfaces: + - name: eth1 + input: + - acl_tag: 1 + tag_name: WEB-IN + state: overridden + + # Using deleted + + - name: Remove the ACL bindings of eth1 and the WEB-IN ACL + vyos.vyos.vyos_vpp_acl: + config: + ip: + acls: + - name: WEB-IN + interfaces: + - name: eth1 + state: deleted + + - name: Delete all dataplane ACL configuration + vyos.vyos.vyos_vpp_acl: + state: deleted + + # Task output (all ACL configuration) + # ----------------------------------- + # + # commands: + # - delete vpp acl + + # Using gathered + + - name: Gather the dataplane ACL configuration + vyos.vyos.vyos_vpp_acl: + state: gathered + + # Using rendered + + - name: Render the commands for a MAC ACL + vyos.vyos.vyos_vpp_acl: + config: + mac: + acls: + - name: MAC-FILTER + rules: + - number: 10 + action: deny + mac_address: 00:11:22:33:44:55 + state: rendered + + # Task output + # ----------- + # + # rendered: + # - set vpp acl mac tag-name MAC-FILTER rule 10 action deny + # - set vpp acl mac tag-name MAC-FILTER rule 10 mac-address 00:11:22:33:44:55 + + # Using parsed + + - name: Parse dataplane ACL configuration from device output + vyos.vyos.vyos_vpp_acl: + running_config: | + set vpp acl ip tag-name WEB-IN rule 10 action 'permit' + set vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-set 'syn' + state: parsed + + # Task output + # ----------- + # + # parsed: + # ip: + # acls: + # - name: WEB-IN + # rules: + # - number: 10 + # action: permit + # tcp_flags: + # is_set: + # - syn + + + +Return Values +------------- +Common return values are documented `here `_, the following are the fields unique to this module: + +.. raw:: html + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
KeyReturnedDescription
+
+ after + +
+ dictionary +
+
when changed +
The resulting configuration after module execution.
+
+
Sample:
+
This output will always be in the same format as the module argspec.
+
+
+ before + +
+ dictionary +
+
when state is merged, replaced, overridden or deleted +
The configuration prior to the module execution.
+
+
Sample:
+
This output will always be in the same format as the module argspec.
+
+
+ commands + +
+ list +
+
when state is merged, replaced, overridden or deleted +
The set of commands pushed to the remote device.
+
+
Sample:
+
['set vpp acl ip tag-name WEB-IN rule 10 action permit', 'set vpp acl ip interface eth1 input acl-tag 1 tag-name WEB-IN']
+
+
+ gathered + +
+ dictionary +
+
when state is gathered +
Facts about the network resource gathered from the remote device as structured data.
+
+
Sample:
+
This output will always be in the same format as the module argspec.
+
+
+ parsed + +
+ dictionary +
+
when state is parsed +
The device native config provided in running_config option parsed into structured data as per module argspec.
+
+
Sample:
+
This output will always be in the same format as the module argspec.
+
+
+ rendered + +
+ list +
+
when state is rendered +
The provided configuration in the task rendered in device-native format (offline).
+
+
Sample:
+
['set vpp acl mac tag-name MAC-FILTER rule 10 action deny']
+
+

+ + +Status +------ + + +Authors +~~~~~~~ + +- VyOS community (@vyos) diff --git a/meta/runtime.yml b/meta/runtime.yml index 7517c7c1..cfe1a7f5 100644 --- a/meta/runtime.yml +++ b/meta/runtime.yml @@ -1,78 +1,80 @@ --- requires_ansible: ">=2.16.0" plugin_routing: modules: banner: redirect: vyos.vyos.vyos_banner bgp_global: redirect: vyos.vyos.vyos_bgp_global bgp_address_family: redirect: vyos.vyos.vyos_bgp_address_family command: redirect: vyos.vyos.vyos_command config: redirect: vyos.vyos.vyos_config facts: redirect: vyos.vyos.vyos_facts file: redirect: vyos.vyos.vyos_file firewall_global: redirect: vyos.vyos.vyos_firewall_global firewall_interfaces: redirect: vyos.vyos.vyos_firewall_interfaces firewall_rules: redirect: vyos.vyos.vyos_firewall_rules hostname: redirect: vyos.vyos.vyos_hostname interfaces: redirect: vyos.vyos.vyos_interfaces l3_interfaces: redirect: vyos.vyos.vyos_l3_interfaces lag_interfaces: redirect: vyos.vyos.vyos_lag_interfaces lldp_global: redirect: vyos.vyos.vyos_lldp_global lldp_interfaces: redirect: vyos.vyos.vyos_lldp_interfaces logging: tombstone: removal_version: 6.0.0 warning_text: use vyos_logging_global instead vyos_logging: tombstone: removal_version: 6.0.0 warning_text: use vyos_logging_global instead logging_global: redirect: vyos.vyos.vyos_logging_global ntp_global: redirect: vyos.vyos.vyos_ntp_global nat: redirect: vyos.vyos.vyos_nat ospfv2: redirect: vyos.vyos.vyos_ospfv2 ospfv3: redirect: vyos.vyos.vyos_ospfv3 ospf_interfaces: redirect: vyos.vyos.vyos_ospf_interfaces ping: redirect: vyos.vyos.vyos_ping prefix_lists: redirect: vyos.vyos.vyos_prefix_lists snmp_server: redirect: vyos.vyos.vyos_snmp_server static_routes: redirect: vyos.vyos.vyos_static_routes system: redirect: vyos.vyos.vyos_system user: redirect: vyos.vyos.vyos_user vlan: redirect: vyos.vyos.vyos_vlan vrf: redirect: vyos.vyos.vyos_vrf vpn_ipsec: redirect: vyos.vyos.vyos_vpn_ipsec vpn_ipsec_s2s: redirect: vyos.vyos.vyos_vpn_ipsec_s2s + vpp_acl: + redirect: vyos.vyos.vyos_vpp_acl ha: redirect: vyos.vyos.vyos_ha diff --git a/plugins/action/vpp_acl.py b/plugins/action/vpp_acl.py new file mode 100644 index 00000000..24195be9 --- /dev/null +++ b/plugins/action/vpp_acl.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- +# 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 + +from ansible_collections.vyos.vyos.plugins.action.vyos import ActionModule + + +__all__ = ["ActionModule"] diff --git a/plugins/module_utils/network/vyos/argspec/vpp_acl/__init__.py b/plugins/module_utils/network/vyos/argspec/vpp_acl/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/plugins/module_utils/network/vyos/argspec/vpp_acl/vpp_acl.py b/plugins/module_utils/network/vyos/argspec/vpp_acl/vpp_acl.py new file mode 100644 index 00000000..ce9e4435 --- /dev/null +++ b/plugins/module_utils/network/vyos/argspec/vpp_acl/vpp_acl.py @@ -0,0 +1,143 @@ +# -*- coding: utf-8 -*- +# Copyright 2026 Red Hat +# 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 + +""" +The arg spec for the vyos_vpp_acl module +""" + +TCP_FLAGS = ["ack", "cwr", "ecn", "fin", "psh", "rst", "syn", "urg"] + +IP_MATCH = { + "type": "dict", + "options": { + "prefix": {"type": "str"}, + "port": {"type": "str"}, + }, +} + +BINDINGS = { + "type": "list", + "elements": "dict", + "options": { + "acl_tag": {"type": "int", "required": True}, + "tag_name": {"type": "str", "required": True}, + }, +} + + +class Vpp_aclArgs(object): # pylint: disable=R0903 + """The arg spec for the vyos_vpp_acl module""" + + argument_spec = { + "config": { + "type": "dict", + "options": { + "ip": { + "type": "dict", + "options": { + "acls": { + "type": "list", + "elements": "dict", + "options": { + "name": {"type": "str", "required": True}, + "description": {"type": "str"}, + "rules": { + "type": "list", + "elements": "dict", + "options": { + "number": {"type": "int", "required": True}, + "action": { + "type": "str", + "choices": ["permit", "deny", "permit-reflect"], + }, + "description": {"type": "str"}, + "protocol": {"type": "str"}, + "source": IP_MATCH, + "destination": IP_MATCH, + "tcp_flags": { + "type": "dict", + "options": { + "is_set": { + "type": "list", + "elements": "str", + "choices": TCP_FLAGS, + }, + "is_not_set": { + "type": "list", + "elements": "str", + "choices": TCP_FLAGS, + }, + }, + }, + }, + }, + }, + }, + "interfaces": { + "type": "list", + "elements": "dict", + "options": { + "name": {"type": "str", "required": True}, + "input": BINDINGS, + "output": BINDINGS, + }, + }, + }, + }, + "mac": { + "type": "dict", + "options": { + "acls": { + "type": "list", + "elements": "dict", + "options": { + "name": {"type": "str", "required": True}, + "description": {"type": "str"}, + "rules": { + "type": "list", + "elements": "dict", + "options": { + "number": {"type": "int", "required": True}, + "action": {"type": "str", "choices": ["permit", "deny"]}, + "description": {"type": "str"}, + "prefix": {"type": "str"}, + "mac_address": {"type": "str"}, + "mac_mask": {"type": "str"}, + }, + }, + }, + }, + "interfaces": { + "type": "list", + "elements": "dict", + "options": { + "name": {"type": "str", "required": True}, + "tag_name": {"type": "str"}, + }, + }, + }, + }, + }, + }, + "running_config": {"type": "str"}, + "state": { + "choices": [ + "merged", + "replaced", + "overridden", + "deleted", + "gathered", + "rendered", + "parsed", + ], + "default": "merged", + "type": "str", + }, + } # pylint: disable=C0301 diff --git a/plugins/module_utils/network/vyos/config/vpp_acl/__init__.py b/plugins/module_utils/network/vyos/config/vpp_acl/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/plugins/module_utils/network/vyos/config/vpp_acl/vpp_acl.py b/plugins/module_utils/network/vyos/config/vpp_acl/vpp_acl.py new file mode 100644 index 00000000..c5fdf60f --- /dev/null +++ b/plugins/module_utils/network/vyos/config/vpp_acl/vpp_acl.py @@ -0,0 +1,217 @@ +# -*- coding: utf-8 -*- +# Copyright 2026 Red Hat +# 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 + +""" +The vyos_vpp_acl config file. +It is in this file where the current configuration (as dict) +is compared to the provided configuration (as dict) and the command set +necessary to bring the current configuration to its desired end-state is +created. +""" + +import re + +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module import ( + ResourceModule, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.utils import ( + dict_merge, + get_from_dict, +) + +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.facts import Facts +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.vpp_acl import ( + Vpp_aclTemplate, +) + + +FAMILIES = ("ip", "mac") + + +def _by(items, key): + return {item[key]: item for item in items or []} + + +class Vpp_acl(ResourceModule): + """ + The vyos_vpp_acl config class + """ + + def __init__(self, module): + super(Vpp_acl, self).__init__( + empty_fact_val={}, + facts_module=Facts(module), + module=module, + resource="vpp_acl", + tmplt=Vpp_aclTemplate(), + ) + # parser groups come from the template: .description for an ACL, + # .rule.* for a rule, .interface.* for an interface binding + names = [p["name"] for p in Vpp_aclTemplate.PARSERS] + self.acl_parsers = {f: [n for n in names if n == f + ".description"] for f in FAMILIES} + self.rule_parsers = {f: [n for n in names if n.startswith(f + ".rule.")] for f in FAMILIES} + self.interface_parsers = { + f: [n for n in names if n.startswith(f + ".interface.")] for f in FAMILIES + } + + def execute_module(self): + """Execute the module + + :rtype: A dictionary + :returns: The result from module execution + """ + if self.state not in ["parsed", "gathered"]: + self.generate_commands() + self.run_commands() + return self.result + + def generate_commands(self): + """Generate configuration commands to send based on + want, have and desired state. + """ + want = self.want or {} + have = self.have or {} + + if self.state == "deleted": + self._deleted(want, have) + return + + merge = self.state in ("merged", "rendered") + emptied = [] + for family in FAMILIES: + wsec, hsec = want.get(family) or {}, have.get(family) or {} + wacls, hacls = _by(wsec.get("acls"), "name"), _by(hsec.get("acls"), "name") + wifs, hifs = _by(wsec.get("interfaces"), "name"), _by(hsec.get("interfaces"), "name") + + if self.state == "overridden": + if not (wacls or wifs): + if hacls or hifs: + emptied.append(family) + continue + for name in sorted(set(hifs) - set(wifs)): + self.addcmd({"name": name}, family + ".interface", True) + for name in sorted(set(hacls) - set(wacls)): + self.addcmd({"name": name}, family + ".acl", True) + + for name, acl in sorted(wacls.items()): + self._compare_acl(family, acl, hacls.get(name), merge) + for name, iface in sorted(wifs.items()): + self._compare_entity( + family + ".interface", + self.interface_parsers[family], + iface, + hifs.get(name), + merge, + ) + self._delete_emptied(emptied, have) + + def _deleted(self, want, have): + """Delete the given ACLs and bindings, or all of vpp acl.""" + if not want: + if have: + self.commands.append("delete vpp acl") + return + emptied = [] + for family in FAMILIES: + wsec, hsec = want.get(family) or {}, have.get(family) or {} + hacls, hifs = _by(hsec.get("acls"), "name"), _by(hsec.get("interfaces"), "name") + dels_if = [i["name"] for i in wsec.get("interfaces") or [] if i["name"] in hifs] + dels_acl = [a["name"] for a in wsec.get("acls") or [] if a["name"] in hacls] + if (hacls or hifs) and set(dels_if) == set(hifs) and set(dels_acl) == set(hacls): + emptied.append(family) + continue + for name in dels_if: + self.addcmd({"name": name}, family + ".interface", True) + for name in dels_acl: + self.addcmd({"name": name}, family + ".acl", True) + self._delete_emptied(emptied, have) + + def _delete_emptied(self, families, have): + """Delete families that end up empty as a whole: VyOS rejects an empty family node.""" + if not families: + return + remaining = [f for f in FAMILIES if f not in families and have.get(f)] + if not remaining and self.state == "deleted": + self.commands.append("delete vpp acl") + return + for family in families: + self.commands.append("delete vpp acl " + family) + + def _compare_acl(self, family, want, have, merge): + """Compare one ACL, then its rules (each rule carries its ACL name as acl).""" + name = want["name"] + self._compare_entity(family + ".acl", self.acl_parsers[family], want, have, merge) + wrules = _by(want.get("rules"), "number") + hrules = _by((have or {}).get("rules"), "number") + for number, rule in sorted(wrules.items()): + hrule = hrules.get(number) + self._compare_entity( + family + ".rule", + self.rule_parsers[family], + dict(rule, acl=name), + dict(hrule, acl=name) if hrule else None, + merge, + ) + if not merge: + for number in sorted(set(hrules) - set(wrules)): + self.addcmd({"acl": name, "number": number}, family + ".rule", True) + + def _compare_entity(self, node, parsers, want, have, merge): + """Compare one ACL, rule or interface; create its bare node if it is new and empty.""" + content = [k for k in want if k not in ("name", "acl", "number")] + if have is None and not content: + self.addcmd(want, node, False) + return + have = have or {k: v for k, v in want.items() if k in ("name", "acl", "number")} + if merge: + want = dict_merge(have, want) + for parser in parsers: + path = self._tmplt.get_parser(parser).get("compval") or parser + if isinstance(get_from_dict(want, path), list) or isinstance( + get_from_dict(have, path), + list, + ): + self._compare_items(parser, path, want, have, merge) + else: + self.compare(parsers=[parser], want=want, have=have) + + def _compare_items(self, parser, path, want, have, merge): + """Compare a list-valued field one item at a time; an item's identity is + what the parser's remval deletes by (the whole value when there is none).""" + remval = self._tmplt.get_parser(parser).get("remval") or "" + keys = re.findall(r"{{\s*%s\.(\w+)\s*}}" % re.escape(path), remval) + + def ident(item): + return tuple(item[k] for k in keys) if keys else item + + witems = {ident(i): i for i in get_from_dict(want, path) or []} + hitems = {ident(i): i for i in get_from_dict(have, path) or []} + if merge: + witems = dict(list(hitems.items()) + list(witems.items())) + base = {k: v for k, v in want.items() if not isinstance(v, (list, dict))} + for key in sorted(set(witems) | set(hitems), key=str): + self.compare( + parsers=[parser], + want=self._with(base, path, witems.get(key)), + have=self._with(base, path, hitems.get(key)), + ) + + @staticmethod + def _with(base, path, value): + """base with value placed at the dotted path (nothing placed for None).""" + data = dict(base) + if value is None: + return data + node = data + parts = path.split(".") + for part in parts[:-1]: + node = node.setdefault(part, {}) + node[parts[-1]] = value + return data diff --git a/plugins/module_utils/network/vyos/facts/facts.py b/plugins/module_utils/network/vyos/facts/facts.py index 3ca031f7..00a70dec 100644 --- a/plugins/module_utils/network/vyos/facts/facts.py +++ b/plugins/module_utils/network/vyos/facts/facts.py @@ -1,150 +1,154 @@ # 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.ansible.netcommon.plugins.module_utils.network.common.facts.facts import ( FactsBase, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.bgp_address_family.bgp_address_family import ( Bgp_address_familyFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.bgp_global.bgp_global import ( Bgp_globalFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.firewall_global.firewall_global import ( Firewall_globalFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.firewall_interfaces.firewall_interfaces import ( Firewall_interfacesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.firewall_rules.firewall_rules import ( Firewall_rulesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.ha.ha import ( HaFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.hostname.hostname import ( HostnameFacts, ) 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.lag_interfaces.lag_interfaces import ( Lag_interfacesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.legacy.base import ( Config, Default, Neighbors, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.lldp_global.lldp_global import ( Lldp_globalFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.lldp_interfaces.lldp_interfaces import ( Lldp_interfacesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.logging_global.logging_global import ( Logging_globalFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.nat.nat import ( NatFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.ntp_global.ntp_global import ( Ntp_globalFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.ospf_interfaces.ospf_interfaces import ( Ospf_interfacesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.ospfv2.ospfv2 import ( Ospfv2Facts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.ospfv3.ospfv3 import ( Ospfv3Facts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.prefix_lists.prefix_lists import ( Prefix_listsFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.route_maps.route_maps import ( Route_mapsFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.snmp_server.snmp_server import ( Snmp_serverFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.static_routes.static_routes import ( Static_routesFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.vpn_ipsec.vpn_ipsec import ( Vpn_ipsecFacts, ) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.vpn_ipsec_s2s.vpn_ipsec_s2s import ( Vpn_ipsec_s2sFacts, ) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.vpp_acl.vpp_acl import ( + Vpp_aclFacts, +) from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.vrf.vrf import VrfFacts FACT_LEGACY_SUBSETS = dict(default=Default, neighbors=Neighbors, config=Config) FACT_RESOURCE_SUBSETS = dict( interfaces=InterfacesFacts, l3_interfaces=L3_interfacesFacts, lag_interfaces=Lag_interfacesFacts, lldp_global=Lldp_globalFacts, lldp_interfaces=Lldp_interfacesFacts, static_routes=Static_routesFacts, firewall_rules=Firewall_rulesFacts, firewall_global=Firewall_globalFacts, firewall_interfaces=Firewall_interfacesFacts, ha=HaFacts, ospfv3=Ospfv3Facts, ospfv2=Ospfv2Facts, ospf_interfaces=Ospf_interfacesFacts, bgp_global=Bgp_globalFacts, bgp_address_family=Bgp_address_familyFacts, route_maps=Route_mapsFacts, prefix_lists=Prefix_listsFacts, logging_global=Logging_globalFacts, ntp_global=Ntp_globalFacts, nat=NatFacts, snmp_server=Snmp_serverFacts, hostname=HostnameFacts, vrf=VrfFacts, vpn_ipsec=Vpn_ipsecFacts, vpn_ipsec_s2s=Vpn_ipsec_s2sFacts, + vpp_acl=Vpp_aclFacts, ) 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 """ if self.VALID_RESOURCE_SUBSETS: self.get_network_resources_facts(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/module_utils/network/vyos/facts/vpp_acl/__init__.py b/plugins/module_utils/network/vyos/facts/vpp_acl/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/plugins/module_utils/network/vyos/facts/vpp_acl/vpp_acl.py b/plugins/module_utils/network/vyos/facts/vpp_acl/vpp_acl.py new file mode 100644 index 00000000..780e6ae5 --- /dev/null +++ b/plugins/module_utils/network/vyos/facts/vpp_acl/vpp_acl.py @@ -0,0 +1,97 @@ +# -*- coding: utf-8 -*- +# Copyright 2026 Red Hat +# 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 + +""" +The vyos vpp_acl fact class +It is in this file the configuration is collected from the device +for a given resource, parsed, and the facts tree is populated +based on the configuration. +""" + +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import utils + +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.argspec.vpp_acl.vpp_acl import ( + Vpp_aclArgs, +) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.vpp_acl import ( + Vpp_aclTemplate, +) + + +def _number(item, key): + return int(item[key]) + + +def _name_key(item): + """Numeric names (parsed as int) first, in numeric order, then the others.""" + name = item["name"] + return (1, str(name)) if isinstance(name, str) else (0, name) + + +class Vpp_aclFacts(object): + """The vyos vpp_acl facts class""" + + def __init__(self, module, subspec="config", options="options"): + self._module = module + self.argument_spec = Vpp_aclArgs.argument_spec + + def get_device_data(self, connection): + return connection.get("show configuration commands | grep 'vpp acl'") + + def populate_facts(self, connection, ansible_facts, data=None): + """Populate the facts for Vpp_acl network resource + :param connection: the device connection + :param ansible_facts: Facts dictionary + :param data: previously collected conf + :rtype: dictionary + :returns: facts + """ + if not data: + data = self.get_device_data(connection) + + lines = [line.replace("'", "") for line in data.splitlines()] + objs = Vpp_aclTemplate(lines=lines, module=self._module).parse() + + for family in ("ip", "mac"): + section = objs.get(family) + if not section: + continue + acls = [] + for acl in (section.get("acls") or {}).values(): + rules = [] + for rule in (acl.get("rules") or {}).values(): + flags = rule.get("tcp_flags") or {} + for key in list(flags): + flags[key] = sorted(flags[key].values()) + rules.append(rule) + if rules: + acl["rules"] = sorted(rules, key=lambda r: _number(r, "number")) + acls.append(acl) + if acls: + section["acls"] = sorted(acls, key=_name_key) + interfaces = [] + for iface in (section.get("interfaces") or {}).values(): + for direction in ("input", "output"): + if direction in iface: + iface[direction] = sorted( + iface[direction].values(), + key=lambda b: _number(b, "acl_tag"), + ) + interfaces.append(iface) + if interfaces: + section["interfaces"] = sorted(interfaces, key=_name_key) + + facts = {"vpp_acl": {}} + if objs: + params = utils.validate_config(self.argument_spec, {"config": objs}) + facts["vpp_acl"] = utils.remove_empties(params["config"]) + + ansible_facts["ansible_network_resources"].update(facts) + return ansible_facts diff --git a/plugins/module_utils/network/vyos/rm_templates/vpp_acl.py b/plugins/module_utils/network/vyos/rm_templates/vpp_acl.py new file mode 100644 index 00000000..4f9591ec --- /dev/null +++ b/plugins/module_utils/network/vyos/rm_templates/vpp_acl.py @@ -0,0 +1,879 @@ +# -*- coding: utf-8 -*- +# Copyright 2026 Red Hat +# 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 + +""" +The Vpp_acl parser templates file. This contains +a list of parser definitions and associated functions that +facilitates both facts gathering and native command generation for +the given network resource. +""" + +import re + +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.network_template import ( + NetworkTemplate, +) + + +class Vpp_aclTemplate(NetworkTemplate): + def __init__(self, lines=None, module=None): + prefix = {"set": "set", "remove": "delete"} + super(Vpp_aclTemplate, self).__init__(lines=lines, tmplt=self, prefix=prefix, module=module) + + # Device lines reach the parser with their quotes removed (see the facts class). + # The first matching parser wins for each line, so the catch-alls of a level come + # after the parsers for its options. Each parser's compval is relative to the entity + # its setval renders: an ACL, a rule (with the ACL name as acl) or an interface. + # fmt: off + PARSERS = [ + # set vpp acl ip tag-name description + { + "name": "ip.description", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+ip + \s+tag-name + \s+(?P\S+) + \s+description + \s+(?P.+?) + \s*$""", + re.VERBOSE, + ), + "compval": "description", + "setval": "vpp acl ip tag-name {{ name }} description '{{ description }}'", + "remval": "vpp acl ip tag-name {{ name }} description", + "result": { + "ip": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "description": "{{ value }}", + }, + }, + }, + }, + }, + # set vpp acl ip tag-name rule action + { + "name": "ip.rule.action", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+ip + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + \s+action + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "compval": "action", + "setval": "vpp acl ip tag-name {{ acl }} rule {{ number }} action {{ action }}", + "remval": "vpp acl ip tag-name {{ acl }} rule {{ number }} action", + "result": { + "ip": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + "action": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + # set vpp acl ip tag-name rule description + { + "name": "ip.rule.description", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+ip + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + \s+description + \s+(?P.+?) + \s*$""", + re.VERBOSE, + ), + "compval": "description", + "setval": "vpp acl ip tag-name {{ acl }} rule {{ number }} description '{{ description }}'", + "remval": "vpp acl ip tag-name {{ acl }} rule {{ number }} description", + "result": { + "ip": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + "description": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + # set vpp acl ip tag-name rule protocol + { + "name": "ip.rule.protocol", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+ip + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + \s+protocol + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "compval": "protocol", + "setval": "vpp acl ip tag-name {{ acl }} rule {{ number }} protocol {{ protocol }}", + "remval": "vpp acl ip tag-name {{ acl }} rule {{ number }} protocol", + "result": { + "ip": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + "protocol": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + # set vpp acl ip tag-name rule source prefix + { + "name": "ip.rule.source.prefix", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+ip + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + \s+source + \s+prefix + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "compval": "source.prefix", + "setval": "vpp acl ip tag-name {{ acl }} rule {{ number }} source prefix {{ source.prefix }}", + "remval": "vpp acl ip tag-name {{ acl }} rule {{ number }} source prefix", + "result": { + "ip": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + "source": { + "prefix": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + }, + # set vpp acl ip tag-name rule source port + { + "name": "ip.rule.source.port", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+ip + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + \s+source + \s+port + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "compval": "source.port", + "setval": "vpp acl ip tag-name {{ acl }} rule {{ number }} source port {{ source.port }}", + "remval": "vpp acl ip tag-name {{ acl }} rule {{ number }} source port", + "result": { + "ip": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + "source": { + "port": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + }, + # set vpp acl ip tag-name rule destination prefix + { + "name": "ip.rule.destination.prefix", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+ip + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + \s+destination + \s+prefix + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "compval": "destination.prefix", + "setval": "vpp acl ip tag-name {{ acl }} rule {{ number }} destination prefix {{ destination.prefix }}", + "remval": "vpp acl ip tag-name {{ acl }} rule {{ number }} destination prefix", + "result": { + "ip": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + "destination": { + "prefix": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + }, + # set vpp acl ip tag-name rule destination port + { + "name": "ip.rule.destination.port", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+ip + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + \s+destination + \s+port + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "compval": "destination.port", + "setval": "vpp acl ip tag-name {{ acl }} rule {{ number }} destination port {{ destination.port }}", + "remval": "vpp acl ip tag-name {{ acl }} rule {{ number }} destination port", + "result": { + "ip": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + "destination": { + "port": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + }, + # set vpp acl ip tag-name rule tcp-flags is-set (one line per flag) + { + "name": "ip.rule.tcp_flags.is_set", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+ip + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + \s+tcp-flags + \s+is-set + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "compval": "tcp_flags.is_set", + "setval": "vpp acl ip tag-name {{ acl }} rule {{ number }} tcp-flags is-set {{ tcp_flags.is_set }}", + "result": { + "ip": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + "tcp_flags": { + "is_set": { + "{{ value }}": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + }, + }, + # set vpp acl ip tag-name rule tcp-flags is-not-set (one line per flag) + { + "name": "ip.rule.tcp_flags.is_not_set", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+ip + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + \s+tcp-flags + \s+is-not-set + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "compval": "tcp_flags.is_not_set", + "setval": "vpp acl ip tag-name {{ acl }} rule {{ number }} tcp-flags is-not-set {{ tcp_flags.is_not_set }}", + "result": { + "ip": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + "tcp_flags": { + "is_not_set": { + "{{ value }}": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + }, + }, + # set vpp acl ip tag-name rule (any other rule line) + { + "name": "ip.rule", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+ip + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + (?:\s.*)?$""", + re.VERBOSE, + ), + "setval": "vpp acl ip tag-name {{ acl }} rule {{ number }}", + "remval": "vpp acl ip tag-name {{ acl }} rule {{ number }}", + "result": { + "ip": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + }, + }, + }, + }, + }, + }, + }, + # set vpp acl ip interface input acl-tag tag-name (compared one acl-tag at a time) + { + "name": "ip.interface.input", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+ip + \s+interface + \s+(?P\S+) + \s+input + \s+acl-tag + \s+(?P\d+) + \s+tag-name + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "compval": "input", + "setval": "vpp acl ip interface {{ name }} input acl-tag {{ input.acl_tag }} tag-name {{ input.tag_name }}", + "remval": "vpp acl ip interface {{ name }} input acl-tag {{ input.acl_tag }}", + "result": { + "ip": { + "interfaces": { + "{{ name }}": { + "name": "{{ name }}", + "input": { + "{{ acl_tag }}": { + "acl_tag": "{{ acl_tag }}", + "tag_name": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + # set vpp acl ip interface output acl-tag tag-name (compared one acl-tag at a time) + { + "name": "ip.interface.output", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+ip + \s+interface + \s+(?P\S+) + \s+output + \s+acl-tag + \s+(?P\d+) + \s+tag-name + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "compval": "output", + "setval": "vpp acl ip interface {{ name }} output acl-tag {{ output.acl_tag }} tag-name {{ output.tag_name }}", + "remval": "vpp acl ip interface {{ name }} output acl-tag {{ output.acl_tag }}", + "result": { + "ip": { + "interfaces": { + "{{ name }}": { + "name": "{{ name }}", + "output": { + "{{ acl_tag }}": { + "acl_tag": "{{ acl_tag }}", + "tag_name": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + # set vpp acl ip interface (any other interface line) + { + "name": "ip.interface", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+ip + \s+interface + \s+(?P\S+) + (?:\s.*)?$""", + re.VERBOSE, + ), + "setval": "vpp acl ip interface {{ name }}", + "remval": "vpp acl ip interface {{ name }}", + "result": {"ip": {"interfaces": {"{{ name }}": {"name": "{{ name }}"}}}}, + }, + # set vpp acl ip tag-name (any other ACL line) + { + "name": "ip.acl", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+ip + \s+tag-name + \s+(?P\S+) + (?:\s.*)?$""", + re.VERBOSE, + ), + "setval": "vpp acl ip tag-name {{ name }}", + "remval": "vpp acl ip tag-name {{ name }}", + "result": {"ip": {"acls": {"{{ name }}": {"name": "{{ name }}"}}}}, + }, + # set vpp acl mac tag-name description + { + "name": "mac.description", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+mac + \s+tag-name + \s+(?P\S+) + \s+description + \s+(?P.+?) + \s*$""", + re.VERBOSE, + ), + "compval": "description", + "setval": "vpp acl mac tag-name {{ name }} description '{{ description }}'", + "remval": "vpp acl mac tag-name {{ name }} description", + "result": { + "mac": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "description": "{{ value }}", + }, + }, + }, + }, + }, + # set vpp acl mac tag-name rule action + { + "name": "mac.rule.action", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+mac + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + \s+action + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "compval": "action", + "setval": "vpp acl mac tag-name {{ acl }} rule {{ number }} action {{ action }}", + "remval": "vpp acl mac tag-name {{ acl }} rule {{ number }} action", + "result": { + "mac": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + "action": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + # set vpp acl mac tag-name rule description + { + "name": "mac.rule.description", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+mac + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + \s+description + \s+(?P.+?) + \s*$""", + re.VERBOSE, + ), + "compval": "description", + "setval": "vpp acl mac tag-name {{ acl }} rule {{ number }} description '{{ description }}'", + "remval": "vpp acl mac tag-name {{ acl }} rule {{ number }} description", + "result": { + "mac": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + "description": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + # set vpp acl mac tag-name rule prefix + { + "name": "mac.rule.prefix", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+mac + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + \s+prefix + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "compval": "prefix", + "setval": "vpp acl mac tag-name {{ acl }} rule {{ number }} prefix {{ prefix }}", + "remval": "vpp acl mac tag-name {{ acl }} rule {{ number }} prefix", + "result": { + "mac": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + "prefix": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + # set vpp acl mac tag-name rule mac-address + { + "name": "mac.rule.mac_address", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+mac + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + \s+mac-address + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "compval": "mac_address", + "setval": "vpp acl mac tag-name {{ acl }} rule {{ number }} mac-address {{ mac_address }}", + "remval": "vpp acl mac tag-name {{ acl }} rule {{ number }} mac-address", + "result": { + "mac": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + "mac_address": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + # set vpp acl mac tag-name rule mac-mask + { + "name": "mac.rule.mac_mask", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+mac + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + \s+mac-mask + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "compval": "mac_mask", + "setval": "vpp acl mac tag-name {{ acl }} rule {{ number }} mac-mask {{ mac_mask }}", + "remval": "vpp acl mac tag-name {{ acl }} rule {{ number }} mac-mask", + "result": { + "mac": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + "mac_mask": "{{ value }}", + }, + }, + }, + }, + }, + }, + }, + # set vpp acl mac tag-name rule (any other rule line) + { + "name": "mac.rule", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+mac + \s+tag-name + \s+(?P\S+) + \s+rule + \s+(?P\d+) + (?:\s.*)?$""", + re.VERBOSE, + ), + "setval": "vpp acl mac tag-name {{ acl }} rule {{ number }}", + "remval": "vpp acl mac tag-name {{ acl }} rule {{ number }}", + "result": { + "mac": { + "acls": { + "{{ name }}": { + "name": "{{ name }}", + "rules": { + "{{ number }}": { + "number": "{{ number }}", + }, + }, + }, + }, + }, + }, + }, + # set vpp acl mac interface tag-name + { + "name": "mac.interface.tag_name", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+mac + \s+interface + \s+(?P\S+) + \s+tag-name + \s+(?P\S+) + \s*$""", + re.VERBOSE, + ), + "compval": "tag_name", + "setval": "vpp acl mac interface {{ name }} tag-name {{ tag_name }}", + "remval": "vpp acl mac interface {{ name }} tag-name", + "result": { + "mac": { + "interfaces": { + "{{ name }}": { + "name": "{{ name }}", + "tag_name": "{{ value }}", + }, + }, + }, + }, + }, + # set vpp acl mac interface (any other interface line) + { + "name": "mac.interface", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+mac + \s+interface + \s+(?P\S+) + (?:\s.*)?$""", + re.VERBOSE, + ), + "setval": "vpp acl mac interface {{ name }}", + "remval": "vpp acl mac interface {{ name }}", + "result": {"mac": {"interfaces": {"{{ name }}": {"name": "{{ name }}"}}}}, + }, + # set vpp acl mac tag-name (any other ACL line) + { + "name": "mac.acl", + "getval": re.compile( + r""" + ^set + \s+vpp + \s+acl + \s+mac + \s+tag-name + \s+(?P\S+) + (?:\s.*)?$""", + re.VERBOSE, + ), + "setval": "vpp acl mac tag-name {{ name }}", + "remval": "vpp acl mac tag-name {{ name }}", + "result": {"mac": {"acls": {"{{ name }}": {"name": "{{ name }}"}}}}, + }, + ] + # fmt: on diff --git a/plugins/modules/vyos_vpp_acl.py b/plugins/modules/vyos_vpp_acl.py new file mode 100644 index 00000000..6ec025fb --- /dev/null +++ b/plugins/modules/vyos_vpp_acl.py @@ -0,0 +1,486 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright 2026 Red Hat +# GNU General Public License v3.0+ +# (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) + +""" +The module file for vyos_vpp_acl +""" + +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +DOCUMENTATION = """ +module: vyos_vpp_acl +version_added: 7.0.0 +short_description: VPP dataplane ACL resource module +description: + - This module manages the access control lists of the accelerated dataplane (VPP) on devices + running VyOS - IP and MAC ACLs, and their bindings to dataplane interfaces. +author: + - VyOS community (@vyos) +extends_documentation_fragment: + - vyos.vyos.vyos +notes: + - Tested against VyOS 1.5.1. + - This module works with connection C(network_cli). + - ACLs can only be bound to interfaces that are part of the dataplane (C(vpp settings interface)). + - IP and MAC ACLs are handled independently. With I(state=replaced), the listed ACLs and + interface bindings are replaced as given; with I(state=overridden), ACLs and bindings that + are not listed are also deleted, bindings before ACLs. + - When not configured, VyOS uses the protocol C(all) for IP rules and the mask + C(ff:ff:ff:ff:ff:ff) for MAC rules; the module reports only configured values. +options: + config: + description: The VPP dataplane ACL configuration. + type: dict + suboptions: + ip: + description: IP ACLs and their bindings. + type: dict + suboptions: + acls: + description: IP ACLs (C(vpp acl ip tag-name)). + type: list + elements: dict + suboptions: + name: + description: The ACL name. + type: str + required: true + description: + description: + - The ACL description. + - VyOS does not accept the single quote character in values. + type: str + rules: + description: The ACL rules. + type: list + elements: dict + suboptions: + number: + description: The rule number. + type: int + required: true + action: + description: The action for matching packets. + type: str + choices: ["permit", "deny", "permit-reflect"] + description: + description: The rule description. + type: str + protocol: + description: The protocol to match, as a name or number, or C(all). + type: str + source: + description: The source to match. + type: dict + suboptions: + prefix: + description: The IPv4 or IPv6 source prefix. + type: str + port: + description: The source port or port range, for example C(1024-65535). + type: str + destination: + description: The destination to match. + type: dict + suboptions: + prefix: + description: The IPv4 or IPv6 destination prefix. + type: str + port: + description: The destination port or port range. + type: str + tcp_flags: + description: The TCP flags to match. + type: dict + suboptions: + is_set: + description: The TCP flags that must be set. + type: list + elements: str + choices: ["ack", "cwr", "ecn", "fin", "psh", "rst", "syn", "urg"] + is_not_set: + description: The TCP flags that must not be set. + type: list + elements: str + choices: ["ack", "cwr", "ecn", "fin", "psh", "rst", "syn", "urg"] + interfaces: + description: The IP ACLs bound to dataplane interfaces. + type: list + elements: dict + suboptions: + name: + description: The interface name, for example C(eth1). + type: str + required: true + input: + description: The ACLs applied to incoming traffic, in the order of I(acl_tag). + type: list + elements: dict + suboptions: + acl_tag: + description: The position of the ACL in the list. + type: int + required: true + tag_name: + description: The name of the IP ACL. + type: str + required: true + output: + description: The ACLs applied to outgoing traffic, in the order of I(acl_tag). + type: list + elements: dict + suboptions: + acl_tag: + description: The position of the ACL in the list. + type: int + required: true + tag_name: + description: The name of the IP ACL. + type: str + required: true + mac: + description: MAC ACLs and their bindings. + type: dict + suboptions: + acls: + description: MAC ACLs (C(vpp acl mac tag-name)). + type: list + elements: dict + suboptions: + name: + description: The ACL name. + type: str + required: true + description: + description: The ACL description. + type: str + rules: + description: The ACL rules. + type: list + elements: dict + suboptions: + number: + description: The rule number. + type: int + required: true + action: + description: The action for matching packets. + type: str + choices: ["permit", "deny"] + description: + description: The rule description. + type: str + prefix: + description: The IPv4 or IPv6 prefix to match. + type: str + mac_address: + description: The MAC address to match. + type: str + mac_mask: + description: The mask applied to I(mac_address). + type: str + interfaces: + description: The MAC ACLs bound to dataplane interfaces, one per interface. + type: list + elements: dict + suboptions: + name: + description: The interface name, for example C(eth1). + type: str + required: true + tag_name: + description: The name of the MAC ACL. + type: str + 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 'vpp acl'). + - 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 the configuration should be left in. + type: str + choices: + - merged + - replaced + - overridden + - deleted + - gathered + - rendered + - parsed + default: merged +""" + +EXAMPLES = """ +# Using merged + +# Before state: +# ------------- +# +# vyos@vyos:~$ show configuration commands | grep 'vpp acl' +# + +- name: Permit HTTPS from partners into eth1 + vyos.vyos.vyos_vpp_acl: + config: + ip: + acls: + - name: WEB-IN + description: Inbound web traffic + rules: + - number: 10 + action: permit + protocol: tcp + source: + prefix: 192.0.2.0/24 + destination: + prefix: 198.51.100.10/32 + port: "443" + - number: 20 + action: deny + interfaces: + - name: eth1 + input: + - acl_tag: 1 + tag_name: WEB-IN + state: merged + +# Task output +# ----------- +# +# commands: +# - set vpp acl ip tag-name WEB-IN description 'Inbound web traffic' +# - set vpp acl ip tag-name WEB-IN rule 10 action permit +# - set vpp acl ip tag-name WEB-IN rule 10 protocol tcp +# - set vpp acl ip tag-name WEB-IN rule 10 source prefix 192.0.2.0/24 +# - set vpp acl ip tag-name WEB-IN rule 10 destination prefix 198.51.100.10/32 +# - set vpp acl ip tag-name WEB-IN rule 10 destination port 443 +# - set vpp acl ip tag-name WEB-IN rule 20 action deny +# - set vpp acl ip interface eth1 input acl-tag 1 tag-name WEB-IN + +# Using merged, MAC ACL + +- name: Permit one vendor's MAC addresses on eth1 + vyos.vyos.vyos_vpp_acl: + config: + mac: + acls: + - name: MAC-FILTER + rules: + - number: 10 + action: permit + mac_address: 00:11:22:00:00:00 + mac_mask: ff:ff:ff:00:00:00 + interfaces: + - name: eth1 + tag_name: MAC-FILTER + state: merged + +# Using replaced + +# Before state: +# ------------- +# +# set vpp acl ip tag-name WEB-IN rule 10 action 'permit' +# set vpp acl ip tag-name WEB-IN rule 10 protocol 'tcp' +# set vpp acl ip tag-name WEB-IN rule 20 action 'deny' + +- name: Replace the rules of WEB-IN + vyos.vyos.vyos_vpp_acl: + config: + ip: + acls: + - name: WEB-IN + rules: + - number: 10 + action: permit + state: replaced + +# Task output +# ----------- +# +# commands: +# - delete vpp acl ip tag-name WEB-IN rule 10 protocol +# - delete vpp acl ip tag-name WEB-IN rule 20 + +# Using overridden + +- name: Keep only the WEB-IN ACL and its binding + vyos.vyos.vyos_vpp_acl: + config: + ip: + acls: + - name: WEB-IN + rules: + - number: 10 + action: permit + interfaces: + - name: eth1 + input: + - acl_tag: 1 + tag_name: WEB-IN + state: overridden + +# Using deleted + +- name: Remove the ACL bindings of eth1 and the WEB-IN ACL + vyos.vyos.vyos_vpp_acl: + config: + ip: + acls: + - name: WEB-IN + interfaces: + - name: eth1 + state: deleted + +- name: Delete all dataplane ACL configuration + vyos.vyos.vyos_vpp_acl: + state: deleted + +# Task output (all ACL configuration) +# ----------------------------------- +# +# commands: +# - delete vpp acl + +# Using gathered + +- name: Gather the dataplane ACL configuration + vyos.vyos.vyos_vpp_acl: + state: gathered + +# Using rendered + +- name: Render the commands for a MAC ACL + vyos.vyos.vyos_vpp_acl: + config: + mac: + acls: + - name: MAC-FILTER + rules: + - number: 10 + action: deny + mac_address: 00:11:22:33:44:55 + state: rendered + +# Task output +# ----------- +# +# rendered: +# - set vpp acl mac tag-name MAC-FILTER rule 10 action deny +# - set vpp acl mac tag-name MAC-FILTER rule 10 mac-address 00:11:22:33:44:55 + +# Using parsed + +- name: Parse dataplane ACL configuration from device output + vyos.vyos.vyos_vpp_acl: + running_config: | + set vpp acl ip tag-name WEB-IN rule 10 action 'permit' + set vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-set 'syn' + state: parsed + +# Task output +# ----------- +# +# parsed: +# ip: +# acls: +# - name: WEB-IN +# rules: +# - number: 10 +# action: permit +# tcp_flags: +# is_set: +# - syn +""" + +RETURN = """ +before: + description: The configuration prior to the module execution. + returned: when I(state) is C(merged), C(replaced), C(overridden) or C(deleted) + type: dict + sample: > + This output will always be in the same format as the + module argspec. +after: + description: The resulting configuration after module execution. + returned: when changed + type: dict + sample: > + This output will always be in the same format as the + module argspec. +commands: + description: The set of commands pushed to the remote device. + returned: when I(state) is C(merged), C(replaced), C(overridden) or C(deleted) + type: list + sample: + - set vpp acl ip tag-name WEB-IN rule 10 action permit + - set vpp acl ip interface eth1 input acl-tag 1 tag-name WEB-IN +rendered: + description: The provided configuration in the task rendered in device-native format (offline). + returned: when I(state) is C(rendered) + type: list + sample: + - set vpp acl mac tag-name MAC-FILTER rule 10 action deny +gathered: + description: Facts about the network resource gathered from the remote device as structured data. + returned: when I(state) is C(gathered) + type: dict + sample: > + This output will always be in the same format as the + module argspec. +parsed: + description: The device native config provided in I(running_config) option parsed into structured data as per module argspec. + returned: when I(state) is C(parsed) + type: dict + sample: > + This output will always be in the same format as the + module argspec. +""" + + +from ansible.module_utils.basic import AnsibleModule + +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.argspec.vpp_acl.vpp_acl import ( + Vpp_aclArgs, +) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.vpp_acl.vpp_acl import ( + Vpp_acl, +) + + +def main(): + """ + Main entry point for module execution + + :returns: the result form module invocation + """ + module = AnsibleModule( + argument_spec=Vpp_aclArgs.argument_spec, + mutually_exclusive=[["config", "running_config"]], + required_if=[ + ["state", "merged", ["config"]], + ["state", "replaced", ["config"]], + ["state", "overridden", ["config"]], + ["state", "rendered", ["config"]], + ["state", "parsed", ["running_config"]], + ], + supports_check_mode=True, + ) + + result = Vpp_acl(module).execute_module() + module.exit_json(**result) + + +if __name__ == "__main__": + main() diff --git a/tests/integration/targets/vyos_vpp_acl/defaults/main.yaml b/tests/integration/targets/vyos_vpp_acl/defaults/main.yaml new file mode 100644 index 00000000..164afead --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/defaults/main.yaml @@ -0,0 +1,3 @@ +--- +testcase: "[^_].*" +test_items: [] diff --git a/tests/integration/targets/vyos_vpp_acl/meta/main.yaml b/tests/integration/targets/vyos_vpp_acl/meta/main.yaml new file mode 100644 index 00000000..7413320e --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/meta/main.yaml @@ -0,0 +1,3 @@ +--- +dependencies: + - prepare_vyos_tests diff --git a/tests/integration/targets/vyos_vpp_acl/tasks/cli.yaml b/tests/integration/targets/vyos_vpp_acl/tasks/cli.yaml new file mode 100644 index 00000000..ac905c46 --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tasks/cli.yaml @@ -0,0 +1,31 @@ +--- +- name: Collect all cli test cases + ansible.builtin.find: + paths: "{{ role_path }}/tests/cli" + patterns: "{{ testcase }}.yaml" + use_regex: true + register: test_cases + delegate_to: localhost + +- name: Set test_items + ansible.builtin.set_fact: + test_items: "{{ test_cases.files | map(attribute='path') | list }}" + +# ACLs bind to dataplane interfaces; VyOS restarts the dataplane whenever its settings +# change, so it is enabled once for all test cases and removed once at the end. +- name: Run test cases with the dataplane enabled + vars: + ansible_connection: ansible.netcommon.network_cli + block: + - name: Enable the dataplane on eth2 + ansible.builtin.include_tasks: "{{ role_path }}/tests/cli/_setup_dataplane.yaml" + + - name: Run test case (connection=ansible.netcommon.network_cli) + ansible.builtin.include_tasks: "{{ test_case_to_run }}" + with_items: "{{ test_items }}" + loop_control: + loop_var: test_case_to_run + + always: + - name: Remove the dataplane + ansible.builtin.include_tasks: "{{ role_path }}/tests/cli/_remove_dataplane.yaml" diff --git a/tests/integration/targets/vyos_vpp_acl/tasks/main.yaml b/tests/integration/targets/vyos_vpp_acl/tasks/main.yaml new file mode 100644 index 00000000..e6378581 --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tasks/main.yaml @@ -0,0 +1,5 @@ +--- +- name: Run CLI tests + ansible.builtin.include_tasks: cli.yaml + tags: + - network_cli diff --git a/tests/integration/targets/vyos_vpp_acl/tests/cli/_parsed.cfg b/tests/integration/targets/vyos_vpp_acl/tests/cli/_parsed.cfg new file mode 100644 index 00000000..825ed12b --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tests/cli/_parsed.cfg @@ -0,0 +1,12 @@ +set vpp acl ip interface eth2 input acl-tag 1 tag-name 'WEB-IN' +set vpp acl ip tag-name WEB-IN description 'Inbound web traffic' +set vpp acl ip tag-name WEB-IN rule 10 action 'permit' +set vpp acl ip tag-name WEB-IN rule 10 destination port '443' +set vpp acl ip tag-name WEB-IN rule 10 protocol 'tcp' +set vpp acl ip tag-name WEB-IN rule 10 source prefix '192.0.2.0/24' +set vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-set 'syn' +set vpp acl ip tag-name WEB-IN rule 20 action 'deny' +set vpp acl mac interface eth2 tag-name 'MAC-FILTER' +set vpp acl mac tag-name MAC-FILTER rule 10 action 'permit' +set vpp acl mac tag-name MAC-FILTER rule 10 mac-address '00:11:22:00:00:00' +set vpp acl mac tag-name MAC-FILTER rule 10 mac-mask 'ff:ff:ff:00:00:00' diff --git a/tests/integration/targets/vyos_vpp_acl/tests/cli/_populate_config.yaml b/tests/integration/targets/vyos_vpp_acl/tests/cli/_populate_config.yaml new file mode 100644 index 00000000..d83ac7f9 --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tests/cli/_populate_config.yaml @@ -0,0 +1,18 @@ +--- +- name: Populate the dataplane ACL configuration + vyos.vyos.vyos_config: + lines: + - set vpp acl ip interface eth2 input acl-tag 1 tag-name 'WEB-IN' + - set vpp acl ip tag-name WEB-IN description 'Inbound web traffic' + - set vpp acl ip tag-name WEB-IN rule 10 action 'permit' + - set vpp acl ip tag-name WEB-IN rule 10 destination port '443' + - set vpp acl ip tag-name WEB-IN rule 10 protocol 'tcp' + - set vpp acl ip tag-name WEB-IN rule 10 source prefix '192.0.2.0/24' + - set vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-set 'syn' + - set vpp acl ip tag-name WEB-IN rule 20 action 'deny' + - set vpp acl mac interface eth2 tag-name 'MAC-FILTER' + - set vpp acl mac tag-name MAC-FILTER rule 10 action 'permit' + - set vpp acl mac tag-name MAC-FILTER rule 10 mac-address '00:11:22:00:00:00' + - set vpp acl mac tag-name MAC-FILTER rule 10 mac-mask 'ff:ff:ff:00:00:00' + vars: + ansible_connection: ansible.netcommon.network_cli diff --git a/tests/integration/targets/vyos_vpp_acl/tests/cli/_remove_config.yaml b/tests/integration/targets/vyos_vpp_acl/tests/cli/_remove_config.yaml new file mode 100644 index 00000000..452c1d19 --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tests/cli/_remove_config.yaml @@ -0,0 +1,8 @@ +--- +- name: Remove the dataplane ACL configuration + vyos.vyos.vyos_config: + lines: + - delete vpp acl + ignore_errors: true + vars: + ansible_connection: ansible.netcommon.network_cli diff --git a/tests/integration/targets/vyos_vpp_acl/tests/cli/_remove_dataplane.yaml b/tests/integration/targets/vyos_vpp_acl/tests/cli/_remove_dataplane.yaml new file mode 100644 index 00000000..1510f309 --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tests/cli/_remove_dataplane.yaml @@ -0,0 +1,6 @@ +--- +# removes all of vpp, so eth2 returns to the kernel +- name: Remove the dataplane + vyos.vyos.vyos_config: + lines: + - delete vpp diff --git a/tests/integration/targets/vyos_vpp_acl/tests/cli/_setup_dataplane.yaml b/tests/integration/targets/vyos_vpp_acl/tests/cli/_setup_dataplane.yaml new file mode 100644 index 00000000..20aa7860 --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tests/cli/_setup_dataplane.yaml @@ -0,0 +1,7 @@ +--- +- name: Enable the dataplane on eth2 + vyos.vyos.vyos_config: + lines: + - set vpp settings allow-unsupported-nics + - set vpp settings interface eth2 + - set vpp settings resource-allocation memory main-heap-size '1G' diff --git a/tests/integration/targets/vyos_vpp_acl/tests/cli/deleted.yaml b/tests/integration/targets/vyos_vpp_acl/tests/cli/deleted.yaml new file mode 100644 index 00000000..84523127 --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tests/cli/deleted.yaml @@ -0,0 +1,31 @@ +--- +- debug: + msg: START vyos_vpp_acl deleted integration tests on connection={{ ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + - name: Delete all dataplane ACL configuration + register: result + vyos.vyos.vyos_vpp_acl: &id001 + state: deleted + + - assert: + that: + - result.changed == true + - result.commands == deleted.commands + - result.after == {} + + - name: Delete all dataplane ACL configuration (IDEMPOTENT) + register: result + vyos.vyos.vyos_vpp_acl: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_vpp_acl/tests/cli/empty_config.yaml b/tests/integration/targets/vyos_vpp_acl/tests/cli/empty_config.yaml new file mode 100644 index 00000000..1a919049 --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tests/cli/empty_config.yaml @@ -0,0 +1,58 @@ +--- +- debug: + msg: START vyos_vpp_acl empty_config integration tests on connection={{ ansible_connection }} + +- name: Merged with empty configuration should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_vpp_acl: + config: + state: merged + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state merged' + +- name: Replaced with empty configuration should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_vpp_acl: + config: + state: replaced + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state replaced' + +- name: Overridden with empty configuration should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_vpp_acl: + config: + state: overridden + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state overridden' + +- name: Rendered with empty configuration should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_vpp_acl: + config: + state: rendered + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state rendered' + +- name: Parsed with empty running_config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_vpp_acl: + running_config: + state: parsed + +- assert: + that: + - result.msg == 'value of running_config parameter must not be empty for state parsed' diff --git a/tests/integration/targets/vyos_vpp_acl/tests/cli/gathered.yaml b/tests/integration/targets/vyos_vpp_acl/tests/cli/gathered.yaml new file mode 100644 index 00000000..fcdab2e0 --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tests/cli/gathered.yaml @@ -0,0 +1,21 @@ +--- +- debug: + msg: START vyos_vpp_acl gathered integration tests on connection={{ ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + - name: Gather the dataplane ACL configuration + register: result + vyos.vyos.vyos_vpp_acl: + state: gathered + + - assert: + that: + - result.changed == false + - result.gathered == base + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_vpp_acl/tests/cli/merged.yaml b/tests/integration/targets/vyos_vpp_acl/tests/cli/merged.yaml new file mode 100644 index 00000000..b596f3e6 --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tests/cli/merged.yaml @@ -0,0 +1,31 @@ +--- +- debug: + msg: START vyos_vpp_acl merged integration tests on connection={{ ansible_connection }} + +- include_tasks: _remove_config.yaml + +- block: + - name: Merge the provided configuration with the existing running configuration + register: result + vyos.vyos.vyos_vpp_acl: &id001 + config: "{{ base }}" + state: merged + + - assert: + that: + - result.changed == true + - result.before == {} + - result.commands|symmetric_difference(merged.commands) == [] + - result.after == base + + - name: Merge the provided configuration with the existing running configuration (IDEMPOTENT) + register: result + vyos.vyos.vyos_vpp_acl: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_vpp_acl/tests/cli/overridden.yaml b/tests/integration/targets/vyos_vpp_acl/tests/cli/overridden.yaml new file mode 100644 index 00000000..e6481738 --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tests/cli/overridden.yaml @@ -0,0 +1,32 @@ +--- +- debug: + msg: START vyos_vpp_acl overridden integration tests on connection={{ ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + - name: Apply the configuration (overridden) + register: result + vyos.vyos.vyos_vpp_acl: &id001 + config: "{{ overridden.config }}" + state: overridden + + - assert: + that: + - result.changed == true + - result.commands|symmetric_difference(overridden.commands) == [] + - result.after == overridden.after + + - name: Apply the configuration (overridden, IDEMPOTENT) + register: result + vyos.vyos.vyos_vpp_acl: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_vpp_acl/tests/cli/parsed.yaml b/tests/integration/targets/vyos_vpp_acl/tests/cli/parsed.yaml new file mode 100644 index 00000000..d2693b76 --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tests/cli/parsed.yaml @@ -0,0 +1,14 @@ +--- +- debug: + msg: START vyos_vpp_acl parsed integration tests on connection={{ ansible_connection }} + +- name: Parse the provided running configuration + register: result + vyos.vyos.vyos_vpp_acl: + running_config: "{{ lookup('file', '_parsed.cfg') }}" + state: parsed + +- assert: + that: + - result.changed == false + - result.parsed == base diff --git a/tests/integration/targets/vyos_vpp_acl/tests/cli/rendered.yaml b/tests/integration/targets/vyos_vpp_acl/tests/cli/rendered.yaml new file mode 100644 index 00000000..bf35ddc2 --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tests/cli/rendered.yaml @@ -0,0 +1,14 @@ +--- +- debug: + msg: START vyos_vpp_acl rendered integration tests on connection={{ ansible_connection }} + +- name: Render the configuration as device commands + register: result + vyos.vyos.vyos_vpp_acl: + config: "{{ base }}" + state: rendered + +- assert: + that: + - result.changed == false + - result.rendered|symmetric_difference(merged.commands) == [] diff --git a/tests/integration/targets/vyos_vpp_acl/tests/cli/replaced.yaml b/tests/integration/targets/vyos_vpp_acl/tests/cli/replaced.yaml new file mode 100644 index 00000000..868174e9 --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tests/cli/replaced.yaml @@ -0,0 +1,32 @@ +--- +- debug: + msg: START vyos_vpp_acl replaced integration tests on connection={{ ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + - name: Apply the configuration (replaced) + register: result + vyos.vyos.vyos_vpp_acl: &id001 + config: "{{ replaced.config }}" + state: replaced + + - assert: + that: + - result.changed == true + - result.commands|symmetric_difference(replaced.commands) == [] + - result.after == replaced.after + + - name: Apply the configuration (replaced, IDEMPOTENT) + register: result + vyos.vyos.vyos_vpp_acl: *id001 + + - name: Assert that the previous task was idempotent + assert: + that: + - result['changed'] == false + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_vpp_acl/tests/cli/rtt.yaml b/tests/integration/targets/vyos_vpp_acl/tests/cli/rtt.yaml new file mode 100644 index 00000000..980c9b5c --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/tests/cli/rtt.yaml @@ -0,0 +1,53 @@ +--- +- debug: + msg: START vyos_vpp_acl round trip integration tests on connection={{ ansible_connection }} + +- include_tasks: _remove_config.yaml + +- block: + - name: Apply the provided configuration (base config) + register: base_config + vyos.vyos.vyos_vpp_acl: + config: "{{ base }}" + state: merged + + - name: Gather dataplane ACL facts + vyos.vyos.vyos_facts: + gather_subset: + - default + gather_network_resources: + - vpp_acl + + - name: Apply the provided configuration (config to be reverted) + register: result + vyos.vyos.vyos_vpp_acl: + config: + ip: + acls: + - name: WEB-IN + rules: + - number: 10 + tcp_flags: + is_set: + - fin + - number: 30 + action: deny + protocol: udp + state: merged + + - name: Assert that changes were applied + assert: + that: result['after'] == round_trip['after'] + + - name: Revert back to base config using facts round trip + register: revert + vyos.vyos.vyos_vpp_acl: + config: "{{ ansible_facts['network_resources']['vpp_acl'] }}" + state: overridden + + - name: Assert that config was reverted + assert: + that: revert['after'] == base_config['after'] + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_vpp_acl/vars/main.yaml b/tests/integration/targets/vyos_vpp_acl/vars/main.yaml new file mode 100644 index 00000000..6c842723 --- /dev/null +++ b/tests/integration/targets/vyos_vpp_acl/vars/main.yaml @@ -0,0 +1,164 @@ +--- +base: &base + ip: + acls: + - name: WEB-IN + description: Inbound web traffic + rules: + - number: 10 + action: permit + protocol: tcp + source: + prefix: 192.0.2.0/24 + destination: + port: "443" + tcp_flags: + is_set: + - syn + - number: 20 + action: deny + interfaces: + - name: eth2 + input: + - acl_tag: 1 + tag_name: WEB-IN + mac: + acls: + - name: MAC-FILTER + rules: + - number: 10 + action: permit + mac_address: 00:11:22:00:00:00 + mac_mask: ff:ff:ff:00:00:00 + interfaces: + - name: eth2 + tag_name: MAC-FILTER + +merged: + commands: + - set vpp acl ip tag-name WEB-IN description 'Inbound web traffic' + - set vpp acl ip tag-name WEB-IN rule 10 action permit + - set vpp acl ip tag-name WEB-IN rule 10 protocol tcp + - set vpp acl ip tag-name WEB-IN rule 10 source prefix 192.0.2.0/24 + - set vpp acl ip tag-name WEB-IN rule 10 destination port 443 + - set vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-set syn + - set vpp acl ip tag-name WEB-IN rule 20 action deny + - set vpp acl ip interface eth2 input acl-tag 1 tag-name WEB-IN + - set vpp acl mac tag-name MAC-FILTER rule 10 action permit + - set vpp acl mac tag-name MAC-FILTER rule 10 mac-address 00:11:22:00:00:00 + - set vpp acl mac tag-name MAC-FILTER rule 10 mac-mask ff:ff:ff:00:00:00 + - set vpp acl mac interface eth2 tag-name MAC-FILTER + +replaced: + config: + ip: + acls: + - name: WEB-IN + rules: + - number: 10 + action: permit + protocol: tcp + commands: + - delete vpp acl ip tag-name WEB-IN description + - delete vpp acl ip tag-name WEB-IN rule 10 source prefix + - delete vpp acl ip tag-name WEB-IN rule 10 destination port + - delete vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-set syn + - delete vpp acl ip tag-name WEB-IN rule 20 + after: + ip: + acls: + - name: WEB-IN + rules: + - number: 10 + action: permit + protocol: tcp + interfaces: + - name: eth2 + input: + - acl_tag: 1 + tag_name: WEB-IN + mac: + acls: + - name: MAC-FILTER + rules: + - number: 10 + action: permit + mac_address: 00:11:22:00:00:00 + mac_mask: ff:ff:ff:00:00:00 + interfaces: + - name: eth2 + tag_name: MAC-FILTER + +overridden: + config: + ip: + acls: + - name: WEB-IN + rules: + - number: 20 + action: deny + interfaces: + - name: eth2 + input: + - acl_tag: 1 + tag_name: WEB-IN + commands: + - delete vpp acl ip tag-name WEB-IN description + - delete vpp acl ip tag-name WEB-IN rule 10 + - delete vpp acl mac + after: + ip: + acls: + - name: WEB-IN + rules: + - number: 20 + action: deny + interfaces: + - name: eth2 + input: + - acl_tag: 1 + tag_name: WEB-IN + +deleted: + commands: + - delete vpp acl + +round_trip: + after: + ip: + acls: + - name: WEB-IN + description: Inbound web traffic + rules: + - number: 10 + action: permit + protocol: tcp + source: + prefix: 192.0.2.0/24 + destination: + port: "443" + tcp_flags: + is_set: + - fin + - syn + - number: 20 + action: deny + - number: 30 + action: deny + protocol: udp + interfaces: + - name: eth2 + input: + - acl_tag: 1 + tag_name: WEB-IN + mac: + acls: + - name: MAC-FILTER + rules: + - number: 10 + action: permit + mac_address: 00:11:22:00:00:00 + mac_mask: ff:ff:ff:00:00:00 + interfaces: + - name: eth2 + tag_name: MAC-FILTER diff --git a/tests/sanity/ignore-2.16.txt b/tests/sanity/ignore-2.16.txt index ee3d2aac..173f772a 100644 --- a/tests/sanity/ignore-2.16.txt +++ b/tests/sanity/ignore-2.16.txt @@ -1,39 +1,40 @@ plugins/action/vyos.py action-plugin-docs # base class for deprecated network platform modules using `connection: local` plugins/action/banner.py action-plugin-docs plugins/action/bgp_address_family.py action-plugin-docs plugins/action/bgp_global.py action-plugin-docs plugins/action/command.py action-plugin-docs plugins/action/config.py action-plugin-docs plugins/action/facts.py action-plugin-docs plugins/action/firewall_global.py action-plugin-docs plugins/action/firewall_interfaces.py action-plugin-docs plugins/action/firewall_rules.py action-plugin-docs plugins/action/ha.py action-plugin-docs plugins/action/hostname.py action-plugin-docs plugins/action/interface.py action-plugin-docs plugins/action/interfaces.py action-plugin-docs plugins/action/l3_interface.py action-plugin-docs plugins/action/l3_interfaces.py action-plugin-docs plugins/action/lag_interfaces.py action-plugin-docs plugins/action/linkagg.py action-plugin-docs plugins/action/lldp.py action-plugin-docs plugins/action/lldp_global.py action-plugin-docs plugins/action/lldp_interface.py action-plugin-docs plugins/action/lldp_interfaces.py action-plugin-docs plugins/action/logging_global.py action-plugin-docs plugins/action/nat.py action-plugin-docs plugins/action/ntp_global.py action-plugin-docs plugins/action/ospf_interfaces.py action-plugin-docs plugins/action/ospfv2.py action-plugin-docs plugins/action/ospfv3.py action-plugin-docs plugins/action/ping.py action-plugin-docs plugins/action/prefix_lists.py action-plugin-docs plugins/action/route_maps.py action-plugin-docs plugins/action/snmp_server.py action-plugin-docs plugins/action/static_route.py action-plugin-docs plugins/action/static_routes.py action-plugin-docs plugins/action/system.py action-plugin-docs plugins/action/user.py action-plugin-docs plugins/action/vlan.py action-plugin-docs +plugins/action/vpp_acl.py action-plugin-docs plugins/action/vrf.py action-plugin-docs plugins/action/file.py action-plugin-docs diff --git a/tests/sanity/ignore-2.17.txt b/tests/sanity/ignore-2.17.txt index ee3d2aac..173f772a 100644 --- a/tests/sanity/ignore-2.17.txt +++ b/tests/sanity/ignore-2.17.txt @@ -1,39 +1,40 @@ plugins/action/vyos.py action-plugin-docs # base class for deprecated network platform modules using `connection: local` plugins/action/banner.py action-plugin-docs plugins/action/bgp_address_family.py action-plugin-docs plugins/action/bgp_global.py action-plugin-docs plugins/action/command.py action-plugin-docs plugins/action/config.py action-plugin-docs plugins/action/facts.py action-plugin-docs plugins/action/firewall_global.py action-plugin-docs plugins/action/firewall_interfaces.py action-plugin-docs plugins/action/firewall_rules.py action-plugin-docs plugins/action/ha.py action-plugin-docs plugins/action/hostname.py action-plugin-docs plugins/action/interface.py action-plugin-docs plugins/action/interfaces.py action-plugin-docs plugins/action/l3_interface.py action-plugin-docs plugins/action/l3_interfaces.py action-plugin-docs plugins/action/lag_interfaces.py action-plugin-docs plugins/action/linkagg.py action-plugin-docs plugins/action/lldp.py action-plugin-docs plugins/action/lldp_global.py action-plugin-docs plugins/action/lldp_interface.py action-plugin-docs plugins/action/lldp_interfaces.py action-plugin-docs plugins/action/logging_global.py action-plugin-docs plugins/action/nat.py action-plugin-docs plugins/action/ntp_global.py action-plugin-docs plugins/action/ospf_interfaces.py action-plugin-docs plugins/action/ospfv2.py action-plugin-docs plugins/action/ospfv3.py action-plugin-docs plugins/action/ping.py action-plugin-docs plugins/action/prefix_lists.py action-plugin-docs plugins/action/route_maps.py action-plugin-docs plugins/action/snmp_server.py action-plugin-docs plugins/action/static_route.py action-plugin-docs plugins/action/static_routes.py action-plugin-docs plugins/action/system.py action-plugin-docs plugins/action/user.py action-plugin-docs plugins/action/vlan.py action-plugin-docs +plugins/action/vpp_acl.py action-plugin-docs plugins/action/vrf.py action-plugin-docs plugins/action/file.py action-plugin-docs diff --git a/tests/sanity/ignore-2.18.txt b/tests/sanity/ignore-2.18.txt index ee3d2aac..173f772a 100644 --- a/tests/sanity/ignore-2.18.txt +++ b/tests/sanity/ignore-2.18.txt @@ -1,39 +1,40 @@ plugins/action/vyos.py action-plugin-docs # base class for deprecated network platform modules using `connection: local` plugins/action/banner.py action-plugin-docs plugins/action/bgp_address_family.py action-plugin-docs plugins/action/bgp_global.py action-plugin-docs plugins/action/command.py action-plugin-docs plugins/action/config.py action-plugin-docs plugins/action/facts.py action-plugin-docs plugins/action/firewall_global.py action-plugin-docs plugins/action/firewall_interfaces.py action-plugin-docs plugins/action/firewall_rules.py action-plugin-docs plugins/action/ha.py action-plugin-docs plugins/action/hostname.py action-plugin-docs plugins/action/interface.py action-plugin-docs plugins/action/interfaces.py action-plugin-docs plugins/action/l3_interface.py action-plugin-docs plugins/action/l3_interfaces.py action-plugin-docs plugins/action/lag_interfaces.py action-plugin-docs plugins/action/linkagg.py action-plugin-docs plugins/action/lldp.py action-plugin-docs plugins/action/lldp_global.py action-plugin-docs plugins/action/lldp_interface.py action-plugin-docs plugins/action/lldp_interfaces.py action-plugin-docs plugins/action/logging_global.py action-plugin-docs plugins/action/nat.py action-plugin-docs plugins/action/ntp_global.py action-plugin-docs plugins/action/ospf_interfaces.py action-plugin-docs plugins/action/ospfv2.py action-plugin-docs plugins/action/ospfv3.py action-plugin-docs plugins/action/ping.py action-plugin-docs plugins/action/prefix_lists.py action-plugin-docs plugins/action/route_maps.py action-plugin-docs plugins/action/snmp_server.py action-plugin-docs plugins/action/static_route.py action-plugin-docs plugins/action/static_routes.py action-plugin-docs plugins/action/system.py action-plugin-docs plugins/action/user.py action-plugin-docs plugins/action/vlan.py action-plugin-docs +plugins/action/vpp_acl.py action-plugin-docs plugins/action/vrf.py action-plugin-docs plugins/action/file.py action-plugin-docs diff --git a/tests/sanity/ignore-2.19.txt b/tests/sanity/ignore-2.19.txt index ee3d2aac..173f772a 100644 --- a/tests/sanity/ignore-2.19.txt +++ b/tests/sanity/ignore-2.19.txt @@ -1,39 +1,40 @@ plugins/action/vyos.py action-plugin-docs # base class for deprecated network platform modules using `connection: local` plugins/action/banner.py action-plugin-docs plugins/action/bgp_address_family.py action-plugin-docs plugins/action/bgp_global.py action-plugin-docs plugins/action/command.py action-plugin-docs plugins/action/config.py action-plugin-docs plugins/action/facts.py action-plugin-docs plugins/action/firewall_global.py action-plugin-docs plugins/action/firewall_interfaces.py action-plugin-docs plugins/action/firewall_rules.py action-plugin-docs plugins/action/ha.py action-plugin-docs plugins/action/hostname.py action-plugin-docs plugins/action/interface.py action-plugin-docs plugins/action/interfaces.py action-plugin-docs plugins/action/l3_interface.py action-plugin-docs plugins/action/l3_interfaces.py action-plugin-docs plugins/action/lag_interfaces.py action-plugin-docs plugins/action/linkagg.py action-plugin-docs plugins/action/lldp.py action-plugin-docs plugins/action/lldp_global.py action-plugin-docs plugins/action/lldp_interface.py action-plugin-docs plugins/action/lldp_interfaces.py action-plugin-docs plugins/action/logging_global.py action-plugin-docs plugins/action/nat.py action-plugin-docs plugins/action/ntp_global.py action-plugin-docs plugins/action/ospf_interfaces.py action-plugin-docs plugins/action/ospfv2.py action-plugin-docs plugins/action/ospfv3.py action-plugin-docs plugins/action/ping.py action-plugin-docs plugins/action/prefix_lists.py action-plugin-docs plugins/action/route_maps.py action-plugin-docs plugins/action/snmp_server.py action-plugin-docs plugins/action/static_route.py action-plugin-docs plugins/action/static_routes.py action-plugin-docs plugins/action/system.py action-plugin-docs plugins/action/user.py action-plugin-docs plugins/action/vlan.py action-plugin-docs +plugins/action/vpp_acl.py action-plugin-docs plugins/action/vrf.py action-plugin-docs plugins/action/file.py action-plugin-docs diff --git a/tests/sanity/ignore-2.20.txt b/tests/sanity/ignore-2.20.txt index a96f7e91..fd4ca783 100644 --- a/tests/sanity/ignore-2.20.txt +++ b/tests/sanity/ignore-2.20.txt @@ -1,39 +1,40 @@ plugins/action/vyos.py action-plugin-docs # base class for deprecated network platform modules using `connection: local` plugins/action/banner.py action-plugin-docs plugins/action/bgp_address_family.py action-plugin-docs plugins/action/bgp_global.py action-plugin-docs plugins/action/command.py action-plugin-docs plugins/action/config.py action-plugin-docs plugins/action/facts.py action-plugin-docs plugins/action/firewall_global.py action-plugin-docs plugins/action/firewall_interfaces.py action-plugin-docs plugins/action/firewall_rules.py action-plugin-docs plugins/action/ha.py action-plugin-docs plugins/action/hostname.py action-plugin-docs plugins/action/interface.py action-plugin-docs plugins/action/interfaces.py action-plugin-docs plugins/action/l3_interface.py action-plugin-docs plugins/action/l3_interfaces.py action-plugin-docs plugins/action/lag_interfaces.py action-plugin-docs plugins/action/linkagg.py action-plugin-docs plugins/action/lldp.py action-plugin-docs plugins/action/lldp_global.py action-plugin-docs plugins/action/lldp_interface.py action-plugin-docs plugins/action/lldp_interfaces.py action-plugin-docs plugins/action/nat.py action-plugin-docs plugins/action/logging_global.py action-plugin-docs plugins/action/ntp_global.py action-plugin-docs plugins/action/ospf_interfaces.py action-plugin-docs plugins/action/ospfv2.py action-plugin-docs plugins/action/ospfv3.py action-plugin-docs plugins/action/ping.py action-plugin-docs plugins/action/prefix_lists.py action-plugin-docs plugins/action/route_maps.py action-plugin-docs plugins/action/snmp_server.py action-plugin-docs plugins/action/static_route.py action-plugin-docs plugins/action/static_routes.py action-plugin-docs plugins/action/system.py action-plugin-docs plugins/action/user.py action-plugin-docs plugins/action/vlan.py action-plugin-docs +plugins/action/vpp_acl.py action-plugin-docs plugins/action/vrf.py action-plugin-docs plugins/action/file.py action-plugin-docs diff --git a/tests/sanity/ignore-2.21.txt b/tests/sanity/ignore-2.21.txt index ee3d2aac..173f772a 100644 --- a/tests/sanity/ignore-2.21.txt +++ b/tests/sanity/ignore-2.21.txt @@ -1,39 +1,40 @@ plugins/action/vyos.py action-plugin-docs # base class for deprecated network platform modules using `connection: local` plugins/action/banner.py action-plugin-docs plugins/action/bgp_address_family.py action-plugin-docs plugins/action/bgp_global.py action-plugin-docs plugins/action/command.py action-plugin-docs plugins/action/config.py action-plugin-docs plugins/action/facts.py action-plugin-docs plugins/action/firewall_global.py action-plugin-docs plugins/action/firewall_interfaces.py action-plugin-docs plugins/action/firewall_rules.py action-plugin-docs plugins/action/ha.py action-plugin-docs plugins/action/hostname.py action-plugin-docs plugins/action/interface.py action-plugin-docs plugins/action/interfaces.py action-plugin-docs plugins/action/l3_interface.py action-plugin-docs plugins/action/l3_interfaces.py action-plugin-docs plugins/action/lag_interfaces.py action-plugin-docs plugins/action/linkagg.py action-plugin-docs plugins/action/lldp.py action-plugin-docs plugins/action/lldp_global.py action-plugin-docs plugins/action/lldp_interface.py action-plugin-docs plugins/action/lldp_interfaces.py action-plugin-docs plugins/action/logging_global.py action-plugin-docs plugins/action/nat.py action-plugin-docs plugins/action/ntp_global.py action-plugin-docs plugins/action/ospf_interfaces.py action-plugin-docs plugins/action/ospfv2.py action-plugin-docs plugins/action/ospfv3.py action-plugin-docs plugins/action/ping.py action-plugin-docs plugins/action/prefix_lists.py action-plugin-docs plugins/action/route_maps.py action-plugin-docs plugins/action/snmp_server.py action-plugin-docs plugins/action/static_route.py action-plugin-docs plugins/action/static_routes.py action-plugin-docs plugins/action/system.py action-plugin-docs plugins/action/user.py action-plugin-docs plugins/action/vlan.py action-plugin-docs +plugins/action/vpp_acl.py action-plugin-docs plugins/action/vrf.py action-plugin-docs plugins/action/file.py action-plugin-docs diff --git a/tests/sanity/ignore-2.22.txt b/tests/sanity/ignore-2.22.txt index ee3d2aac..173f772a 100644 --- a/tests/sanity/ignore-2.22.txt +++ b/tests/sanity/ignore-2.22.txt @@ -1,39 +1,40 @@ plugins/action/vyos.py action-plugin-docs # base class for deprecated network platform modules using `connection: local` plugins/action/banner.py action-plugin-docs plugins/action/bgp_address_family.py action-plugin-docs plugins/action/bgp_global.py action-plugin-docs plugins/action/command.py action-plugin-docs plugins/action/config.py action-plugin-docs plugins/action/facts.py action-plugin-docs plugins/action/firewall_global.py action-plugin-docs plugins/action/firewall_interfaces.py action-plugin-docs plugins/action/firewall_rules.py action-plugin-docs plugins/action/ha.py action-plugin-docs plugins/action/hostname.py action-plugin-docs plugins/action/interface.py action-plugin-docs plugins/action/interfaces.py action-plugin-docs plugins/action/l3_interface.py action-plugin-docs plugins/action/l3_interfaces.py action-plugin-docs plugins/action/lag_interfaces.py action-plugin-docs plugins/action/linkagg.py action-plugin-docs plugins/action/lldp.py action-plugin-docs plugins/action/lldp_global.py action-plugin-docs plugins/action/lldp_interface.py action-plugin-docs plugins/action/lldp_interfaces.py action-plugin-docs plugins/action/logging_global.py action-plugin-docs plugins/action/nat.py action-plugin-docs plugins/action/ntp_global.py action-plugin-docs plugins/action/ospf_interfaces.py action-plugin-docs plugins/action/ospfv2.py action-plugin-docs plugins/action/ospfv3.py action-plugin-docs plugins/action/ping.py action-plugin-docs plugins/action/prefix_lists.py action-plugin-docs plugins/action/route_maps.py action-plugin-docs plugins/action/snmp_server.py action-plugin-docs plugins/action/static_route.py action-plugin-docs plugins/action/static_routes.py action-plugin-docs plugins/action/system.py action-plugin-docs plugins/action/user.py action-plugin-docs plugins/action/vlan.py action-plugin-docs +plugins/action/vpp_acl.py action-plugin-docs plugins/action/vrf.py action-plugin-docs plugins/action/file.py action-plugin-docs diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_vpp_acl_config.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_vpp_acl_config.cfg new file mode 100644 index 00000000..0c018eaf --- /dev/null +++ b/tests/unit/modules/network/vyos/fixtures/vyos_vpp_acl_config.cfg @@ -0,0 +1,21 @@ +set vpp acl ip interface eth1 input acl-tag 1 tag-name 'WEB-IN' +set vpp acl ip interface eth1 input acl-tag 2 tag-name 'MGMT' +set vpp acl ip interface eth1 output acl-tag 1 tag-name 'WEB-OUT' +set vpp acl ip tag-name MGMT rule 10 action 'permit' +set vpp acl ip tag-name WEB-IN description 'Inbound web traffic' +set vpp acl ip tag-name WEB-IN rule 10 action 'permit' +set vpp acl ip tag-name WEB-IN rule 10 description 'Allow HTTPS from partners' +set vpp acl ip tag-name WEB-IN rule 10 destination port '443' +set vpp acl ip tag-name WEB-IN rule 10 destination prefix '198.51.100.10/32' +set vpp acl ip tag-name WEB-IN rule 10 protocol 'tcp' +set vpp acl ip tag-name WEB-IN rule 10 source port '1024-65535' +set vpp acl ip tag-name WEB-IN rule 10 source prefix '192.0.2.0/24' +set vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-not-set 'ack' +set vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-set 'syn' +set vpp acl ip tag-name WEB-IN rule 20 action 'deny' +set vpp acl ip tag-name WEB-OUT rule 10 action 'permit-reflect' +set vpp acl mac interface eth1 tag-name 'MAC-FILTER' +set vpp acl mac tag-name MAC-FILTER rule 10 action 'permit' +set vpp acl mac tag-name MAC-FILTER rule 10 mac-address '00:11:22:33:44:55' +set vpp acl mac tag-name MAC-FILTER rule 10 mac-mask 'ff:ff:ff:00:00:00' +set vpp acl mac tag-name MAC-FILTER rule 10 prefix '192.0.2.0/24' diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_vpp_acl_config_names.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_vpp_acl_config_names.cfg new file mode 100644 index 00000000..b6eb7686 --- /dev/null +++ b/tests/unit/modules/network/vyos/fixtures/vyos_vpp_acl_config_names.cfg @@ -0,0 +1,4 @@ +set vpp acl ip tag-name 100 rule 10 action 'permit' +set vpp acl ip tag-name 20 rule 10 action 'deny' +set vpp acl ip tag-name WEB description 'Partner web ACL' +set vpp acl ip tag-name WEB rule 10 action 'permit' diff --git a/tests/unit/modules/network/vyos/test_vyos_vpp_acl.py b/tests/unit/modules/network/vyos/test_vyos_vpp_acl.py new file mode 100644 index 00000000..4e20ff6b --- /dev/null +++ b/tests/unit/modules/network/vyos/test_vyos_vpp_acl.py @@ -0,0 +1,358 @@ +# -*- coding: utf-8 -*- +# Copyright 2026 Red Hat +# 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 + +from unittest.mock import patch + +from ansible_collections.vyos.vyos.plugins.modules import vyos_vpp_acl +from ansible_collections.vyos.vyos.tests.unit.modules.utils import set_module_args + +from .vyos_module import TestVyosModule, load_fixture + + +WEB_IN = dict( + name="WEB-IN", + description="Inbound web traffic", + rules=[ + dict( + number=10, + action="permit", + description="Allow HTTPS from partners", + protocol="tcp", + source=dict(prefix="192.0.2.0/24", port="1024-65535"), + destination=dict(prefix="198.51.100.10/32", port="443"), + tcp_flags=dict(is_set=["syn"], is_not_set=["ack"]), + ), + dict(number=20, action="deny"), + ], +) +ACLS = dict( + ip=dict( + acls=[ + dict(name="MGMT", rules=[dict(number=10, action="permit")]), + WEB_IN, + dict(name="WEB-OUT", rules=[dict(number=10, action="permit-reflect")]), + ], + interfaces=[ + dict( + name="eth1", + input=[dict(acl_tag=1, tag_name="WEB-IN"), dict(acl_tag=2, tag_name="MGMT")], + output=[dict(acl_tag=1, tag_name="WEB-OUT")], + ), + ], + ), + mac=dict( + acls=[ + dict( + name="MAC-FILTER", + rules=[ + dict( + number=10, + action="permit", + prefix="192.0.2.0/24", + mac_address="00:11:22:33:44:55", + mac_mask="ff:ff:ff:00:00:00", + ), + ], + ), + ], + interfaces=[dict(name="eth1", tag_name="MAC-FILTER")], + ), +) + + +class TestVyosVppAclModule(TestVyosModule): + module = vyos_vpp_acl + + def setUp(self): + super(TestVyosVppAclModule, self).setUp() + + self.mock_get_resource_connection_config = patch( + "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module_base.get_resource_connection", + ) + self.get_resource_connection_config = self.mock_get_resource_connection_config.start() + + self.mock_get_resource_connection_facts = patch( + "ansible_collections.ansible.netcommon.plugins.module_utils.network.common.facts.facts.get_resource_connection", + ) + self.get_resource_connection_facts = self.mock_get_resource_connection_facts.start() + + self.mock_get_device_data = patch( + "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.vpp_acl.vpp_acl.Vpp_aclFacts.get_device_data", + ) + self.get_device_data = self.mock_get_device_data.start() + + def tearDown(self): + super(TestVyosVppAclModule, self).tearDown() + self.mock_get_resource_connection_config.stop() + self.mock_get_resource_connection_facts.stop() + self.mock_get_device_data.stop() + + def load_fixtures(self, commands=None, filename=None): + if filename is None: + filename = "vyos_vpp_acl_config.cfg" + + def load_from_file(*args, **kwargs): + return load_fixture(filename) + + self.get_device_data.side_effect = load_from_file + + def test_vyos_vpp_acl_merged_idempotent(self): + set_module_args(dict(config=ACLS, state="merged")) + self.execute_module(changed=False, commands=[]) + + def test_vyos_vpp_acl_merged_tcp_flag_order_does_not_matter(self): + rule = dict(number=10, tcp_flags=dict(is_set=["syn"], is_not_set=["ack"])) + set_module_args( + dict(config=dict(ip=dict(acls=[dict(name="WEB-IN", rules=[rule])])), state="merged"), + ) + self.execute_module(changed=False, commands=[]) + + def test_vyos_vpp_acl_merged(self): + set_module_args( + dict( + config=dict( + ip=dict( + acls=[ + dict( + name="WEB-IN", + rules=[ + dict(number=10, tcp_flags=dict(is_set=["syn", "fin"])), + dict(number=30, action="deny", protocol="udp"), + ], + ), + dict(name="NEW", description="New ACL with spaces"), + ], + interfaces=[ + dict(name="eth1", input=[dict(acl_tag=3, tag_name="NEW")]), + dict(name="eth2", output=[dict(acl_tag=1, tag_name="NEW")]), + ], + ), + ), + state="merged", + ), + ) + commands = [ + "set vpp acl ip tag-name NEW description 'New ACL with spaces'", + "set vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-set fin", + "set vpp acl ip tag-name WEB-IN rule 30 action deny", + "set vpp acl ip tag-name WEB-IN rule 30 protocol udp", + "set vpp acl ip interface eth1 input acl-tag 3 tag-name NEW", + "set vpp acl ip interface eth2 output acl-tag 1 tag-name NEW", + ] + self.execute_module(changed=True, commands=commands) + + def test_vyos_vpp_acl_replaced_acl(self): + set_module_args( + dict( + config=dict( + ip=dict( + acls=[ + dict( + name="WEB-IN", + rules=[ + dict( + number=10, + action="permit", + protocol="tcp", + tcp_flags=dict(is_set=["syn"]), + ), + ], + ), + ], + ), + ), + state="replaced", + ), + ) + commands = [ + "delete vpp acl ip tag-name WEB-IN description", + "delete vpp acl ip tag-name WEB-IN rule 10 description", + "delete vpp acl ip tag-name WEB-IN rule 10 source prefix", + "delete vpp acl ip tag-name WEB-IN rule 10 source port", + "delete vpp acl ip tag-name WEB-IN rule 10 destination prefix", + "delete vpp acl ip tag-name WEB-IN rule 10 destination port", + "delete vpp acl ip tag-name WEB-IN rule 10 tcp-flags is-not-set ack", + "delete vpp acl ip tag-name WEB-IN rule 20", + ] + self.execute_module(changed=True, commands=commands) + + def test_vyos_vpp_acl_replaced_bindings(self): + set_module_args( + dict( + config=dict( + ip=dict( + interfaces=[dict(name="eth1", input=[dict(acl_tag=1, tag_name="MGMT")])], + ), + ), + state="replaced", + ), + ) + commands = [ + "set vpp acl ip interface eth1 input acl-tag 1 tag-name MGMT", + "delete vpp acl ip interface eth1 input acl-tag 2", + "delete vpp acl ip interface eth1 output acl-tag 1", + ] + self.execute_module(changed=True, commands=commands) + + def test_vyos_vpp_acl_replaced_mac(self): + set_module_args( + dict( + config=dict( + mac=dict( + acls=[ + dict( + name="MAC-FILTER", + rules=[ + dict(number=10, action="deny", mac_address="00:11:22:33:44:55"), + ], + ), + ], + interfaces=[dict(name="eth1", tag_name="MAC-FILTER")], + ), + ), + state="replaced", + ), + ) + commands = [ + "set vpp acl mac tag-name MAC-FILTER rule 10 action deny", + "delete vpp acl mac tag-name MAC-FILTER rule 10 prefix", + "delete vpp acl mac tag-name MAC-FILTER rule 10 mac-mask", + ] + self.execute_module(changed=True, commands=commands) + + def test_vyos_vpp_acl_replaced_idempotent(self): + set_module_args(dict(config=ACLS, state="replaced")) + self.execute_module(changed=False, commands=[]) + + def test_vyos_vpp_acl_overridden(self): + set_module_args( + dict( + config=dict( + ip=dict( + acls=[dict(name="MGMT", rules=[dict(number=10, action="permit")])], + interfaces=[dict(name="eth1", input=[dict(acl_tag=1, tag_name="MGMT")])], + ), + ), + state="overridden", + ), + ) + commands = [ + "delete vpp acl ip tag-name WEB-IN", + "delete vpp acl ip tag-name WEB-OUT", + "set vpp acl ip interface eth1 input acl-tag 1 tag-name MGMT", + "delete vpp acl ip interface eth1 input acl-tag 2", + "delete vpp acl ip interface eth1 output acl-tag 1", + "delete vpp acl mac", + ] + self.execute_module(changed=True, commands=commands) + + def test_vyos_vpp_acl_overridden_idempotent(self): + set_module_args(dict(config=ACLS, state="overridden")) + self.execute_module(changed=False, commands=[]) + + def test_vyos_vpp_acl_deleted_all(self): + set_module_args(dict(state="deleted")) + self.execute_module(changed=True, commands=["delete vpp acl"]) + + def test_vyos_vpp_acl_deleted_parts(self): + set_module_args( + dict( + config=dict( + ip=dict(acls=[dict(name="WEB-OUT")], interfaces=[dict(name="eth1")]), + mac=dict(interfaces=[dict(name="eth1")]), + ), + state="deleted", + ), + ) + commands = [ + "delete vpp acl ip interface eth1", + "delete vpp acl ip tag-name WEB-OUT", + "delete vpp acl mac interface eth1", + ] + self.execute_module(changed=True, commands=commands) + + def test_vyos_vpp_acl_rendered(self): + set_module_args( + dict( + config=dict( + mac=dict( + acls=[ + dict( + name="MAC-FILTER", + description="Vendor filter", + rules=[ + dict(number=10, action="deny", mac_address="00:11:22:33:44:55"), + ], + ), + ], + interfaces=[dict(name="eth2", tag_name="MAC-FILTER")], + ), + ), + state="rendered", + ), + ) + commands = [ + "set vpp acl mac tag-name MAC-FILTER description 'Vendor filter'", + "set vpp acl mac tag-name MAC-FILTER rule 10 action deny", + "set vpp acl mac tag-name MAC-FILTER rule 10 mac-address 00:11:22:33:44:55", + "set vpp acl mac interface eth2 tag-name MAC-FILTER", + ] + result = self.execute_module(changed=False) + self.assertEqual(sorted(result["rendered"]), sorted(commands)) + + def test_vyos_vpp_acl_gathered(self): + set_module_args(dict(state="gathered")) + result = self.execute_module(changed=False) + self.assertEqual(result["gathered"], ACLS) + + def test_vyos_vpp_acl_parsed(self): + set_module_args( + dict(running_config=load_fixture("vyos_vpp_acl_config.cfg"), state="parsed"), + ) + result = self.execute_module(changed=False) + self.assertEqual(result["parsed"], ACLS) + + def test_vyos_vpp_acl_deleted_whole_family(self): + # all MAC ACLs and bindings listed: the family node goes as a whole + set_module_args( + dict( + config=dict( + mac=dict(acls=[dict(name="MAC-FILTER")], interfaces=[dict(name="eth1")]), + ), + state="deleted", + ), + ) + self.execute_module(changed=True, commands=["delete vpp acl mac"]) + + def test_vyos_vpp_acl_deleted_both_families(self): + set_module_args( + dict( + config=dict( + ip=dict( + acls=[dict(name="MGMT"), dict(name="WEB-IN"), dict(name="WEB-OUT")], + interfaces=[dict(name="eth1")], + ), + mac=dict(acls=[dict(name="MAC-FILTER")], interfaces=[dict(name="eth1")]), + ), + state="deleted", + ), + ) + self.execute_module(changed=True, commands=["delete vpp acl"]) + + def test_vyos_vpp_acl_parsed_numeric_and_text_names(self): + # numeric and text ACL names together, numeric ones first in numeric order + set_module_args( + dict( + running_config=load_fixture("vyos_vpp_acl_config_names.cfg"), + state="parsed", + ), + ) + acls = self.execute_module(changed=False)["parsed"]["ip"]["acls"] + self.assertEqual([acl["name"] for acl in acls], ["20", "100", "WEB"])