diff --git a/README.md b/README.md index 5d8e1e8b..c323bba0 100644 --- a/README.md +++ b/README.md @@ -1,261 +1,262 @@ # VyOS Collection [](https://codecov.io/gh/vyos/vyos.vyos) [](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 . <!--start requires_ansible--> ## Ansible version compatibility This collection has been tested against following Ansible versions: **>=2.15.0**. For collections that support Ansible 2.9, please ensure you update your `network_os` to use the fully qualified collection name (for example, `cisco.ios.ios`). 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. <!--end requires_ansible--> ### Supported connections The VyOS collection supports ``network_cli`` connections. ## Included content <!--start collection 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_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_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_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_vrf](https://github.com/vyos/vyos.vyos/blob/main/docs/vyos.vyos.vyos_vrf_module.rst)|VRF resource module <!--end collection content--> 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 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 <filename>`) 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 - 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 <!--Add a link to a changelog.md file or an external docsite to cover this information. --> ## Release notes Release notes are available [here](https://github.com/vyos/vyos.vyos/blob/main/CHANGELOG.rst). ## Roadmap <!-- Optional. Include the roadmap for this collection, and the proposed release/versioning strategy so users can anticipate the upgrade/update cycle. --> 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) ## 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/docs/vyos.vyos.vyos_vrf_module.rst b/docs/vyos.vyos.vyos_vrf_module.rst new file mode 100644 index 00000000..e20661fa --- /dev/null +++ b/docs/vyos.vyos.vyos_vrf_module.rst @@ -0,0 +1,999 @@ +.. _vyos.vyos.vyos_vrf_module: + + +****************** +vyos.vyos.vyos_vrf +****************** + +**VRF resource module** + + +Version added: 1.0.0 + +.. contents:: + :local: + :depth: 1 + + +Synopsis +-------- +- This module manages vrf configuration on devices running Vyos + + + + +Parameters +---------- + +.. raw:: html + + <table border=0 cellpadding=0 class="documentation-table"> + <tr> + <th colspan="3">Parameter</th> + <th>Choices/<font color="blue">Defaults</font></th> + <th width="100%">Comments</th> + </tr> + <tr> + <td colspan="3"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>config</b> + <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> + <div style="font-size: small"> + <span style="color: purple">dictionary</span> + </div> + </td> + <td> + </td> + <td> + <div>List of vrf configuration.</div> + </td> + </tr> + <tr> + <td class="elbow-placeholder"></td> + <td colspan="2"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>bind-to-all</b> + <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> + <div style="font-size: small"> + <span style="color: purple">boolean</span> + </div> + </td> + <td> + <ul style="margin: 0; padding: 0"><b>Choices:</b> + <li><div style="color: blue"><b>no</b> ←</div></li> + <li>yes</li> + </ul> + </td> + <td> + <div>Enable binding services to all VRFs</div> + </td> + </tr> + <tr> + <td class="elbow-placeholder"></td> + <td colspan="2"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>instances</b> + <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> + <div style="font-size: small"> + <span style="color: purple">list</span> + / <span style="color: purple">elements=dictionary</span> + </div> + </td> + <td> + </td> + <td> + <div>Virtual Routing and Forwarding instance</div> + </td> + </tr> + <tr> + <td class="elbow-placeholder"></td> + <td class="elbow-placeholder"></td> + <td colspan="1"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>description</b> + <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> + <div style="font-size: small"> + <span style="color: purple">string</span> + </div> + </td> + <td> + </td> + <td> + <div>Description</div> + </td> + </tr> + <tr> + <td class="elbow-placeholder"></td> + <td class="elbow-placeholder"></td> + <td colspan="1"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>disabled</b> + <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> + <div style="font-size: small"> + <span style="color: purple">boolean</span> + </div> + </td> + <td> + <ul style="margin: 0; padding: 0"><b>Choices:</b> + <li><div style="color: blue"><b>no</b> ←</div></li> + <li>yes</li> + </ul> + </td> + <td> + <div>Administratively disable interface</div> + <div style="font-size: small; color: darkgreen"><br/>aliases: disable</div> + </td> + </tr> + <tr> + <td class="elbow-placeholder"></td> + <td class="elbow-placeholder"></td> + <td colspan="1"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>name</b> + <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> + <div style="font-size: small"> + <span style="color: purple">string</span> + </div> + </td> + <td> + </td> + <td> + <div>VRF instance name</div> + </td> + </tr> + <tr> + <td class="elbow-placeholder"></td> + <td class="elbow-placeholder"></td> + <td colspan="1"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>table_id</b> + <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> + <div style="font-size: small"> + <span style="color: purple">integer</span> + </div> + </td> + <td> + </td> + <td> + <div>Routing table associated with this instance</div> + </td> + </tr> + <tr> + <td class="elbow-placeholder"></td> + <td class="elbow-placeholder"></td> + <td colspan="1"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>vni</b> + <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> + <div style="font-size: small"> + <span style="color: purple">integer</span> + </div> + </td> + <td> + </td> + <td> + <div>Virtual Network Identifier</div> + </td> + </tr> + + + <tr> + <td colspan="3"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>running_config</b> + <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> + <div style="font-size: small"> + <span style="color: purple">string</span> + </div> + </td> + <td> + </td> + <td> + <div>This option is used only with state <em>parsed</em>.</div> + <div>The value of this option should be the output received from the VYOS device by executing the command <b>show configuration commands | grep ntp</b>.</div> + <div>The states <em>replaced</em> and <em>overridden</em> have identical behaviour for this module.</div> + <div>The state <em>parsed</em> reads the configuration from <code>show configuration commands | grep ntp</code> option and transforms it into Ansible structured data as per the resource module's argspec and the value is then returned in the <em>parsed</em> key within the result.</div> + </td> + </tr> + <tr> + <td colspan="3"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>state</b> + <a class="ansibleOptionLink" href="#parameter-" title="Permalink to this option"></a> + <div style="font-size: small"> + <span style="color: purple">string</span> + </div> + </td> + <td> + <ul style="margin: 0; padding: 0"><b>Choices:</b> + <li>deleted</li> + <li><div style="color: blue"><b>merged</b> ←</div></li> + <li>overridden</li> + <li>replaced</li> + <li>gathered</li> + <li>rendered</li> + <li>parsed</li> + </ul> + </td> + <td> + <div>The state the configuration should be left in.</div> + </td> + </tr> + </table> + <br/> + + +Notes +----- + +.. note:: + - Tested against vyos 1.4+ + - This module works with connection ``network_cli``. + + + +Examples +-------- + +.. code-block:: yaml + + # # ------------------- + # # 1. Using merged + # # ------------------- + + # # Before state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set service ntp server time1.vyos.net + # set service ntp server time2.vyos.net + # set service ntp server time3.vyos.net + # vyos@vyos:~$ + + # # Task + # # ------------- + - name: Replace the existing ntp config with the new config + vyos.vyos.vyos_ntp_global: + config: + allow_clients: + - 10.6.6.0/24 + listen_addresses: + - 10.1.3.1 + servers: + - server: 203.0.113.0 + options: + - prefer + + + # Task output: + # ------------- + # "after": { + # "allow_clients": [ + # "10.6.6.0/24" + # ], + # "listen_addresses": [ + # "10.1.3.1" + # ], + # "servers": [ + # { + # "server": "ser", + # "options": [ + # "prefer" + # ] + # }, + # { + # "server": "time1.vyos.net" + # }, + # { + # "server": "time2.vyos.net" + # }, + # { + # "server": "time3.vyos.net" + # } + # ] + # }, + # "before": { + # }, + # "changed": true, + # "commands": [ + # "set service ntp allow-clients address 10.6.6.0/24", + # "set service ntp listen-address 10.1.3.1", + # "set service ntp server 203.0.113.0 prefer" + # ] + + # After state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set service ntp allow-clients address '10.6.6.0/24' + # set service ntp listen-address '10.1.3.1' + # set service ntp server 203.0.113.0 prefer, + # set service ntp server time1.vyos.net + # set service ntp server time2.vyos.net + # set service ntp server time3.vyos.net + # vyos@vyos:~$ + + + # # ------------------- + # # 2. Using replaced + # # ------------------- + + # # Before state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set service ntp allow-clients address '10.4.9.0/24' + # set service ntp allow-clients address '10.4.7.0/24' + # set service ntp allow-clients address '10.1.2.0/24' + # set service ntp allow-clients address '10.2.3.0/24' + # set service ntp listen-address '10.1.9.16' + # set service ntp listen-address '10.5.3.2' + # set service ntp listen-address '10.7.9.21' + # set service ntp listen-address '10.8.9.4' + # set service ntp listen-address '10.4.5.1' + # set service ntp server 10.3.6.5 noselect + # set service ntp server 10.3.6.5 dynamic + # set service ntp server 10.3.6.5 preempt + # set service ntp server 10.3.6.5 prefer + # set service ntp server server4 noselect + # set service ntp server server4 dynamic + # set service ntp server server5 + # set service ntp server time1.vyos.net + # set service ntp server time2.vyos.net + # set service ntp server time3.vyos.net + # vyos@vyos:~$ + + # # Task + # # ------------- + - name: Replace the existing ntp config with the new config + vyos.vyos.vyos_ntp_global: + config: + allow_clients: + - 10.6.6.0/24 + listen_addresses: + - 10.1.3.1 + servers: + - server: 203.0.113.0 + options: + - prefer + state: replaced + + + # # Task output: + # # ------------- + # "after": { + # "allow_clients": [ + # "10.6.6.0/24" + # ], + # "listen_addresses": [ + # "10.1.3.1" + # ], + # "servers": [ + # { + # "server": "ser", + # "options": [ + # "prefer" + # ] + # }, + # { + # "server": "time1.vyos.net" + # }, + # { + # "server": "time2.vyos.net" + # }, + # { + # "server": "time3.vyos.net" + # } + # ] + # }, + # "before": { + # "allow_clients": [ + # "10.4.7.0/24", + # "10.2.3.0/24", + # "10.1.2.0/24", + # "10.4.9.0/24" + # ], + # "listen_addresses": [ + # "10.7.9.21", + # "10.4.5.1", + # "10.5.3.2", + # "10.8.9.4", + # "10.1.9.16" + # ], + # "servers": [ + # { + # "server": "10.3.6.5", + # "options": [ + # "noselect", + # "dynamic", + # "preempt", + # "prefer" + # ] + # }, + # { + # "server": "server4", + # "options": [ + # "noselect", + # "dynamic" + # ] + # }, + # { + # "server": "server5" + # }, + # { + # "server": "time1.vyos.net" + # }, + # { + # "server": "time2.vyos.net" + # }, + # { + # "server": "time3.vyos.net" + # } + # ] + # }, + # "changed": true, + # "commands": [ + # "delete service ntp allow-clients address 10.4.7.0/24", + # "delete service ntp allow-clients address 10.2.3.0/24", + # "delete service ntp allow-clients address 10.1.2.0/24", + # "delete service ntp allow-clients address 10.4.9.0/24", + # "delete service ntp listen-address 10.7.9.21", + # "delete service ntp listen-address 10.4.5.1", + # "delete service ntp listen-address 10.5.3.2", + # "delete service ntp listen-address 10.8.9.4", + # "delete service ntp listen-address 10.1.9.16", + # "delete service ntp server 10.3.6.5", + # "delete service ntp server server4", + # "delete service ntp server server5", + # "set service ntp allow-clients address 10.6.6.0/24", + # "set service ntp listen-address 10.1.3.1", + # "set service ntp server 203.0.113.0 prefer" + # ] + + # After state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set service ntp allow-clients address '10.6.6.0/24' + # set service ntp listen-address '10.1.3.1' + # set service ntp server 203.0.113.0 prefer, + # set service ntp server time1.vyos.net + # set service ntp server time2.vyos.net + # set service ntp server time3.vyos.net + # vyos@vyos:~$ + + # # ------------------- + # # 3. Using overridden + # # ------------------- + + # # Before state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set service ntp allow-clients address '10.6.6.0/24' + # set service ntp listen-address '10.1.3.1' + # set service ntp server 203.0.113.0 prefer, + # set service ntp server time1.vyos.net + # set service ntp server time2.vyos.net + # set service ntp server time3.vyos.net + # vyos@vyos:~$ + + # Task + # ------------- + - name: Override ntp config + vyos.vyos.vyos_ntp_global: + config: + allow_clients: + - 10.3.3.0/24 + listen_addresses: + - 10.7.8.1 + servers: + - server: server1 + options: + - dynamic + - prefer + + - server: server2 + options: + - noselect + - preempt + + - server: serv + state: overridden + + # # Task output: + # # ------------- + # "after": { + # "allow_clients": [ + # "10.3.3.0/24" + # ], + # "listen_addresses": [ + # "10.7.8.1" + # ], + # "servers": [ + # { + # "server": "serv" + # }, + # { + # "server": "server1", + # "options": [ + # "dynamic", + # "prefer" + # ] + # }, + # { + # "server": "server2", + # "options": [ + # "noselect", + # "preempt" + # ] + # }, + # { + # "server": "time1.vyos.net" + # }, + # { + # "server": "time2.vyos.net" + # }, + # { + # "server": "time3.vyos.net" + # } + # ] + # }, + # "before": { + # "allow_clients": [ + # "10.6.6.0/24" + # ], + # "listen_addresses": [ + # "10.1.3.1" + # ], + # "servers": [ + # { + # "server": "ser", + # "options": [ + # "prefer" + # ] + # }, + # { + # "server": "time1.vyos.net" + # }, + # { + # "server": "time2.vyos.net" + # }, + # { + # "server": "time3.vyos.net" + # } + # ] + # }, + # "changed": true, + # "commands": [ + # "delete service ntp allow-clients address 10.6.6.0/24", + # "delete service ntp listen-address 10.1.3.1", + # "delete service ntp server ser", + # "set service ntp allow-clients address 10.3.3.0/24", + # "set service ntp listen-address 10.7.8.1", + # "set service ntp server server1 dynamic", + # "set service ntp server server1 prefer", + # "set service ntp server server2 noselect", + # "set service ntp server server2 preempt", + # "set service ntp server serv" + # ] + + # After state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set service ntp allow-clients address '10.3.3.0/24' + # set service ntp listen-address '10.7.8.1' + # set service ntp server serv + # set service ntp server server1 dynamic + # set service ntp server server1 prefer + # set service ntp server server2 noselect + # set service ntp server server2 preempt + # set service ntp server time1.vyos.net + # set service ntp server time2.vyos.net + # set service ntp server time3.vyos.net + # vyos@vyos:~$ + + # 4. Using gathered + # ------------------- + + # # Before state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set service ntp allow-clients address '10.3.3.0/24' + # set service ntp listen-address '10.7.8.1' + # set service ntp server serv + # set service ntp server server1 dynamic + # set service ntp server server1 prefer + # set service ntp server server2 noselect + # set service ntp server server2 preempt + # set service ntp server time1.vyos.net + # set service ntp server time2.vyos.net + # set service ntp server time3.vyos.net + # vyos@vyos:~$ + + # Task + # ------------- + - name: Gather ntp config + vyos.vyos.vyos_ntp_global: + state: gathered + + # # Task output: + # # ------------- + # "gathered": { + # "allow_clients": [ + # "10.3.3.0/24" + # ], + # "listen_addresses": [ + # "10.7.8.1" + # ], + # "servers": [ + # { + # "server": "serv" + # }, + # { + # "server": "server1", + # "options": [ + # "dynamic", + # "prefer" + # ] + # }, + # { + # "server": "server2", + # "options": [ + # "noselect", + # "preempt" + # ] + # }, + # { + # "server": "time1.vyos.net" + # }, + # { + # "server": "time2.vyos.net" + # }, + # { + # "server": "time3.vyos.net" + # } + # ] + # } + + # After state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set service ntp allow-clients address '10.3.3.0/24' + # set service ntp listen-address '10.7.8.1' + # set service ntp server serv + # set service ntp server server1 dynamic + # set service ntp server server1 prefer + # set service ntp server server2 noselect + # set service ntp server server2 preempt + # set service ntp server time1.vyos.net + # set service ntp server time2.vyos.net + # set service ntp server time3.vyos.net + # vyos@vyos:~$ + + + # # ------------------- + # # 5. Using deleted + # # ------------------- + + # # Before state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set service ntp allow-clients address '10.3.3.0/24' + # set service ntp listen-address '10.7.8.1' + # set service ntp server serv + # set service ntp server server1 dynamic + # set service ntp server server1 prefer + # set service ntp server server2 noselect + # set service ntp server server2 preempt + # set service ntp server time1.vyos.net + # set service ntp server time2.vyos.net + # set service ntp server time3.vyos.net + # vyos@vyos:~$ + + # # Task + # # ------------- + - name: Delete ntp config + vyos.vyos.vyos_ntp_global: + state: deleted + + + # # Task output: + # # ------------- + # "after": { + # "servers": [ + # { + # "server": "time1.vyos.net" + # }, + # { + # "server": "time2.vyos.net" + # }, + # { + # "server": "time3.vyos.net" + # } + # ] + # }, + # "before": { + # "allow_clients": [ + # "10.3.3.0/24" + # ], + # "listen_addresses": [ + # "10.7.8.1" + # ], + # "servers": [ + # { + # "server": "serv" + # }, + # { + # "server": "server1", + # "options": [ + # "dynamic", + # "prefer" + # ] + # }, + # { + # "server": "server2", + # "options": [ + # "noselect", + # "preempt" + # ] + # }, + # { + # "server": "time1.vyos.net" + # }, + # { + # "server": "time2.vyos.net" + # }, + # { + # "server": "time3.vyos.net" + # } + # ] + # }, + # "changed": true, + # "commands": [ + # "delete service ntp allow-clients", + # "delete service ntp listen-address", + # "delete service ntp server serv", + # "delete service ntp server server1", + # "delete service ntp server server2" + # + # ] + + # After state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set service ntp server time1.vyos.net + # set service ntp server time2.vyos.net + # set service ntp server time3.vyos.net + # vyos@vyos:~$ + + + # # ------------------- + # # 6. Using rendered + # # ------------------- + + # # Before state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set service ntp server time1.vyos.net + # set service ntp server time2.vyos.net + # set service ntp server time3.vyos.net + # vyos@vyos:~$ + + # Task + # ------------- + - name: Render ntp config + vyos.vyos.vyos_ntp_global: + config: + allow_clients: + - 10.7.7.0/24 + - 10.8.8.0/24 + listen_addresses: + - 10.7.9.1 + servers: + - server: server7 + - server: server45 + options: + - noselect + - prefer + - pool + - server: time1.vyos.net + - server: time2.vyos.net + - server: time3.vyos.net + state: rendered + + # # Task output: + # # ------------- + # "rendered": [ + # "set service ntp allow-clients address 10.7.7.0/24", + # "set service ntp allow-clients address 10.8.8.0/24", + # "set service ntp listen-address 10.7.9.1", + # "set service ntp server server7", + # "set service ntp server server45 noselect", + # "set service ntp server server45 prefer", + # "set service ntp server server45 pool", + # "set service ntp server time1.vyos.net", + # "set service ntp server time2.vyos.net", + # "set service ntp server time3.vyos.net" + # ] + + + # # ------------------- + # # 7. Using parsed + # # ------------------- + + # # sample_config.cfg: + # # ------------- + # "set service ntp allow-clients address 10.7.7.0/24", + # "set service ntp listen-address 10.7.9.1", + # "set service ntp server server45 noselect", + # "set service ntp allow-clients addres 10.8.6.0/24", + # "set service ntp listen-address 10.5.4.1", + # "set service ntp server server45 dynamic", + # "set service ntp server time1.vyos.net", + # "set service ntp server time2.vyos.net", + # "set service ntp server time3.vyos.net" + + # Task: + # ------------- + - name: Parse externally provided ntp configuration + vyos.vyos.vyos_ntp_global: + running_config: "{{ lookup('file', './sample_config.cfg') }}" + state: parsed + + # # Task output: + # # ------------- + # parsed = { + # "allow_clients": [ + # "10.7.7.0/24", + # "10.8.6.0/24 + # ], + # "listen_addresses": [ + # "10.5.4.1", + # "10.7.9.1" + # ], + # "servers": [ + # { + # "server": "server45", + # "options": [ + # "noselect", + # "dynamic" + # + # ] + # }, + # { + # "server": "time1.vyos.net" + # }, + # { + # "server": "time2.vyos.net" + # }, + # { + # "server": "time3.vyos.net" + # } + # + # ] + # } + + + +Return Values +------------- +Common return values are documented `here <https://docs.ansible.com/ansible/latest/reference_appendices/common_return_values.html#common-return-values>`_, the following are the fields unique to this module: + +.. raw:: html + + <table border=0 cellpadding=0 class="documentation-table"> + <tr> + <th colspan="1">Key</th> + <th>Returned</th> + <th width="100%">Description</th> + </tr> + <tr> + <td colspan="1"> + <div class="ansibleOptionAnchor" id="return-"></div> + <b>after</b> + <a class="ansibleOptionLink" href="#return-" title="Permalink to this return value"></a> + <div style="font-size: small"> + <span style="color: purple">dictionary</span> + </div> + </td> + <td>when changed</td> + <td> + <div>The resulting configuration after module execution.</div> + <br/> + <div style="font-size: smaller"><b>Sample:</b></div> + <div style="font-size: smaller; color: blue; word-wrap: break-word; word-break: break-all;">This output will always be in the same format as the module argspec.</div> + </td> + </tr> + <tr> + <td colspan="1"> + <div class="ansibleOptionAnchor" id="return-"></div> + <b>before</b> + <a class="ansibleOptionLink" href="#return-" title="Permalink to this return value"></a> + <div style="font-size: small"> + <span style="color: purple">dictionary</span> + </div> + </td> + <td>when <em>state</em> is <code>merged</code>, <code>replaced</code>, <code>overridden</code>, <code>deleted</code> or <code>purged</code></td> + <td> + <div>The configuration prior to the module execution.</div> + <br/> + <div style="font-size: smaller"><b>Sample:</b></div> + <div style="font-size: smaller; color: blue; word-wrap: break-word; word-break: break-all;">This output will always be in the same format as the module argspec.</div> + </td> + </tr> + <tr> + <td colspan="1"> + <div class="ansibleOptionAnchor" id="return-"></div> + <b>commands</b> + <a class="ansibleOptionLink" href="#return-" title="Permalink to this return value"></a> + <div style="font-size: small"> + <span style="color: purple">list</span> + </div> + </td> + <td>when <em>state</em> is <code>merged</code>, <code>replaced</code>, <code>overridden</code>, <code>deleted</code> or <code>purged</code></td> + <td> + <div>The set of commands pushed to the remote device.</div> + <br/> + <div style="font-size: smaller"><b>Sample:</b></div> + <div style="font-size: smaller; color: blue; word-wrap: break-word; word-break: break-all;">['set system ntp server server1 dynamic', 'set system ntp server server1 prefer', 'set system ntp server server2 noselect', 'set system ntp server server2 preempt', 'set system ntp server server_add preempt']</div> + </td> + </tr> + <tr> + <td colspan="1"> + <div class="ansibleOptionAnchor" id="return-"></div> + <b>gathered</b> + <a class="ansibleOptionLink" href="#return-" title="Permalink to this return value"></a> + <div style="font-size: small"> + <span style="color: purple">list</span> + </div> + </td> + <td>when <em>state</em> is <code>gathered</code></td> + <td> + <div>Facts about the network resource gathered from the remote device as structured data.</div> + <br/> + <div style="font-size: smaller"><b>Sample:</b></div> + <div style="font-size: smaller; color: blue; word-wrap: break-word; word-break: break-all;">This output will always be in the same format as the module argspec.</div> + </td> + </tr> + <tr> + <td colspan="1"> + <div class="ansibleOptionAnchor" id="return-"></div> + <b>parsed</b> + <a class="ansibleOptionLink" href="#return-" title="Permalink to this return value"></a> + <div style="font-size: small"> + <span style="color: purple">list</span> + </div> + </td> + <td>when <em>state</em> is <code>parsed</code></td> + <td> + <div>The device native config provided in <em>running_config</em> option parsed into structured data as per module argspec.</div> + <br/> + <div style="font-size: smaller"><b>Sample:</b></div> + <div style="font-size: smaller; color: blue; word-wrap: break-word; word-break: break-all;">This output will always be in the same format as the module argspec.</div> + </td> + </tr> + <tr> + <td colspan="1"> + <div class="ansibleOptionAnchor" id="return-"></div> + <b>rendered</b> + <a class="ansibleOptionLink" href="#return-" title="Permalink to this return value"></a> + <div style="font-size: small"> + <span style="color: purple">list</span> + </div> + </td> + <td>when <em>state</em> is <code>rendered</code></td> + <td> + <div>The provided configuration in the task rendered in device-native format (offline).</div> + <br/> + <div style="font-size: smaller"><b>Sample:</b></div> + <div style="font-size: smaller; color: blue; word-wrap: break-word; word-break: break-all;">['set system ntp server server1 dynamic', 'set system ntp server server1 prefer', 'set system ntp server server2 noselect', 'set system ntp server server2 preempt', 'set system ntp server server_add preempt']</div> + </td> + </tr> + </table> + <br/><br/> + + +Status +------ + + +Authors +~~~~~~~ + +- Evgeny Molotkov (@omnom62) diff --git a/meta/runtime.yml b/meta/runtime.yml index 58899674..36579e05 100644 --- a/meta/runtime.yml +++ b/meta/runtime.yml @@ -1,66 +1,68 @@ --- requires_ansible: ">=2.15.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 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 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_servers 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 diff --git a/plugins/action/vrf.py b/plugins/action/vrf.py new file mode 100644 index 00000000..148d7c64 --- /dev/null +++ b/plugins/action/vrf.py @@ -0,0 +1,55 @@ +# +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. +# +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +from ansible.utils.display import Display +from ansible_collections.ansible.netcommon.plugins.action.network import ( + ActionModule as ActionNetworkModule, +) + + +display = Display() + + +class ActionModule(ActionNetworkModule): + def run(self, tmp=None, task_vars=None): + del tmp # tmp no longer has any effect + + module_name = self._task.action.split(".")[-1] + self._config_module = True if module_name in ["vyos_config", "config"] else False + persistent_connection = self._play_context.connection.split(".")[-1] + warnings = [] + + if persistent_connection != "network_cli": + return { + "failed": True, + "msg": "Connection type %s is not valid for this module" + % self._play_context.connection, + } + + result = super(ActionModule, self).run(task_vars=task_vars) + if warnings: + if "warnings" in result: + result["warnings"].extend(warnings) + else: + result["warnings"] = warnings + return result diff --git a/plugins/module_utils/network/vyos/argspec/vrf/__init__.py b/plugins/module_utils/network/vyos/argspec/vrf/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/plugins/module_utils/network/vyos/argspec/vrf/vrf.py b/plugins/module_utils/network/vyos/argspec/vrf/vrf.py new file mode 100644 index 00000000..f0b15371 --- /dev/null +++ b/plugins/module_utils/network/vyos/argspec/vrf/vrf.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +# Copyright 2024 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 + +############################################# +# WARNING # +############################################# +# +# This file is auto generated by the +# cli_rm_builder. +# +# Manually editing this file is not advised. +# +# To update the argspec make the desired changes +# in the module docstring and re-run +# cli_rm_builder. +# +############################################# + +""" +The arg spec for the vyos_vrf module +""" + + +class VrfArgs(object): # pylint: disable=R0903 + """The arg spec for the vyos_vrf module""" + + argument_spec = { + "config": { + "type": "dict", + "options": { + "bind-to-all": {"type": "bool"}, + "instances": { + "type": "list", + "elements": "dict", + "options": { + "name": {"type": "str"}, + "description": {"type": "str"}, + "disabled": {"type": "bool"}, + "table_id": {"type": "int"}, + "vni": {"type": "int"}, + }, + }, + }, + }, + "state": { + "type": "str", + "choices": [ + "deleted", + "merged", + "purged", + "replaced", + "gathered", + "rendered", + "parsed", + ], + "default": "merged", + }, + "running_config": {"type": "str"}, + } # pylint: disable=C0301 diff --git a/plugins/module_utils/network/vyos/config/vrf/__init__.py b/plugins/module_utils/network/vyos/config/vrf/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/plugins/module_utils/network/vyos/config/vrf/vrf.py b/plugins/module_utils/network/vyos/config/vrf/vrf.py new file mode 100644 index 00000000..061a2ed9 --- /dev/null +++ b/plugins/module_utils/network/vyos/config/vrf/vrf.py @@ -0,0 +1,205 @@ +# +# -*- coding: utf-8 -*- +# Copyright 2021 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_ntp 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. +""" + +from ansible.module_utils.six import iteritems +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, +) + +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.vrf import ( + VrfTemplate, +) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.utils.version import ( + LooseVersion, +) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.vyos import get_os_version + + +class Vrf(ResourceModule): + """ + The vyos_vrf config class + """ + + def __init__(self, module): + super(Vrf, self).__init__( + empty_fact_val={}, + facts_module=Facts(module), + module=module, + resource="vrf", + tmplt=VrfTemplate(), + ) + self.parsers = [ + "allow_clients", + "listen_addresses", + "server", + "options", + "allow_clients_delete", + "listen_addresses_delete", + ] + + 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. + """ + + if LooseVersion(get_os_version(self._module)) >= LooseVersion("1.4"): + path = "service" + ac = "allow-client" + else: + path = "system" + ac = "allow-clients" + + self._tmplt.set_ntp_path(path) + self._tmplt.set_ntp_ac(ac) + + wantd = self._ntp_list_to_dict(self.want) + haved = self._ntp_list_to_dict(self.have) + + # if state is merged, merge want onto have and then compare + if self.state == "merged": + wantd = dict_merge(haved, wantd) + + # if state is deleted, empty out wantd and set haved to wantd + if self.state == "deleted": + haved = {k: v for k, v in iteritems(haved) if k in wantd or not wantd} + wantd = {} + + commandlist = self._commandlist(haved) + servernames = self._servernames(haved) + # removing the servername and commandlist from the list after deleting it from haved + # iterate through the top-level items to delete + for k, have in iteritems(haved): + if k not in wantd: + for hk, hval in iteritems(have): + if hk == "allow_clients" and hk in commandlist: + self.commands.append( + self._tmplt.render({"": hk}, "allow_clients_delete", True), + ) + commandlist.remove(hk) + elif hk == "listen_addresses" and hk in commandlist: + self.commands.append( + self._tmplt.render({"": hk}, "listen_addresses_delete", True), + ) + commandlist.remove(hk) + elif hk == "server" and have["server"] in servernames: + self._compareoverride(want={}, have=have) + servernames.remove(have["server"]) + # if everything is deleted add the delete command for {path} ntp + # this should be equiv: servernames == [] and commandlist == ["server"]: + if wantd == {} and haved != {}: + self.commands.append( + self._tmplt.render({}, "service_delete", True), + ) + + # remove existing config for overridden and replaced + # Getting the list of the server names from haved + # to avoid the duplication of overridding/replacing the servers + if self.state in ["overridden", "replaced"]: + commandlist = self._commandlist(haved) + servernames = self._servernames(haved) + + for k, have in iteritems(haved): + if k not in wantd: + if "server" not in have: + self._compareoverride(want={}, have=have) + # removing the servername from the list after deleting it from haved + elif have["server"] in servernames: + self._compareoverride(want={}, have=have) + servernames.remove(have["server"]) + + for k, want in iteritems(wantd): + self._compare(want=want, have=haved.pop(k, {})) + + def _compare(self, want, have): + """Leverages the base class `compare()` method and + populates the list of commands to be run by comparing + the `want` and `have` data with the `parsers` defined + for the Ntp network resource. + """ + if "options" in want: + self.compare(parsers="options", want=want, have=have) + else: + self.compare(parsers=self.parsers, want=want, have=have) + + def _compareoverride(self, want, have): + # do not delete configuration with options level + for i, val in iteritems(have): + if i == "options": + pass + else: + self.compare(parsers=i, want={}, have=have) + + def _ntp_list_to_dict(self, entry): + servers_dict = {} + for k, data in iteritems(entry): + if k == "servers": + for value in data: + if "options" in value: + result = self._serveroptions_list_to_dict(value) + for res, resvalue in iteritems(result): + servers_dict.update({res: resvalue}) + else: + servers_dict.update({value["server"]: value}) + else: + for value in data: + servers_dict.update({"ip_" + value: {k: value}}) + return servers_dict + + def _serveroptions_list_to_dict(self, entry): + serveroptions_dict = {} + for Opk, Op in iteritems(entry): + if Opk == "options": + for val in Op: + dict = {} + dict.update({"server": entry["server"]}) + dict.update({Opk: val}) + serveroptions_dict.update({entry["server"] + "_" + val: dict}) + return serveroptions_dict + + def _commandlist(self, haved): + commandlist = [] + for k, have in iteritems(haved): + for ck, cval in iteritems(have): + if ck != "options" and ck not in commandlist: + commandlist.append(ck) + return commandlist + + def _servernames(self, haved): + servernames = [] + for k, have in iteritems(haved): + for sk, sval in iteritems(have): + if sk != "options" and sval not in servernames: + servernames.append(sval) + return servernames diff --git a/plugins/module_utils/network/vyos/facts/facts.py b/plugins/module_utils/network/vyos/facts/facts.py index 74bbda74..24c444dd 100644 --- a/plugins/module_utils/network/vyos/facts/facts.py +++ b/plugins/module_utils/network/vyos/facts/facts.py @@ -1,131 +1,133 @@ # 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.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.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.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, 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, snmp_server=Snmp_serverFacts, hostname=HostnameFacts, + vrf=VrfFacts, ) 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/vrf/__init__.py b/plugins/module_utils/network/vyos/facts/vrf/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/plugins/module_utils/network/vyos/facts/vrf/vrf.py b/plugins/module_utils/network/vyos/facts/vrf/vrf.py new file mode 100644 index 00000000..99503536 --- /dev/null +++ b/plugins/module_utils/network/vyos/facts/vrf/vrf.py @@ -0,0 +1,91 @@ +# -*- coding: utf-8 -*- +# Copyright 2021 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 vrf 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. +""" + +import re + +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common import utils + +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.argspec.vrf.vrf import VrfArgs +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.rm_templates.vrf import ( + VrfTemplate, +) + + +class VrfFacts(object): + """The vyos vrf facts class""" + + def __init__(self, module, subspec="config", options="options"): + self._module = module + self.argument_spec = VrfArgs.argument_spec + + def get_config(self, connection): + return connection.get("show configuration commands | match 'set vrf'") + + def populate_facts(self, connection, ansible_facts, data=None): + """Populate the facts for Vrf network resource + + :param connection: the device connection + :param ansible_facts: Facts dictionary + :param data: previously collected conf + + :rtype: dictionary + :returns: facts + """ + facts = {} + objs = [] + config_lines = [] + + if not data: + data = self.get_config(connection) + + for resource in data.splitlines(): + config_lines.append(re.sub("'", "", resource)) + self._module.fail_json(msg=data) + # parse native config using the Vrf template + vrf_parser = VrfTemplate(lines=config_lines, module=self._module) + + objs = vrf_parser.parse() + + if objs: + if "allow_clients" in objs: + objs["allow_clients"] = sorted(list(objs["allow_clients"])) + + if "listen_addresses" in objs: + objs["listen_addresses"] = sorted(list(objs["listen_addresses"])) + + """ if "options" in objs["servers"].values(): + val = objs["servers"].values() + val["options"] = sorted(val["options"]) """ + + if "servers" in objs: + objs["servers"] = list(objs["servers"].values()) + objs["servers"] = sorted(objs["servers"], key=lambda k: k["server"]) + for i in objs["servers"]: + if "options" in i: + i["options"] = sorted(list(i["options"])) + + ansible_facts["ansible_network_resources"].pop("ntp_global", None) + + params = utils.remove_empties( + vrf_parser.validate_config(self.argument_spec, {"config": objs}, redact=True), + ) + + if params.get("config"): + facts["ntp_global"] = params["config"] + ansible_facts["ansible_network_resources"].update(facts) + + return ansible_facts diff --git a/plugins/module_utils/network/vyos/rm_templates/vrf.py b/plugins/module_utils/network/vyos/rm_templates/vrf.py new file mode 100644 index 00000000..b69fd7a6 --- /dev/null +++ b/plugins/module_utils/network/vyos/rm_templates/vrf.py @@ -0,0 +1,169 @@ +# -*- coding: utf-8 -*- +# Copyright 2021 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 Ntp 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 VrfTemplate(NetworkTemplate): + def __init__(self, lines=None, module=None): + prefix = {"set": "set", "remove": "delete"} + self._overrides = { # 1.4+ by default + "_path": "service", # 1.4 or greater, "system" for 1.3 or less + "_ac": "allow-client", # 1.4 or greater, "allow-clients" for 1.3 or less + } + super(VrfTemplate, self).__init__(lines=lines, tmplt=self, prefix=prefix, module=module) + + def set_ntp_path(self, path: str): + """set_ntp_path""" + self._overrides["_path"] = path + + def set_ntp_ac(self, ac: str): + """set_ntp_ac""" + self._overrides["_ac"] = ac + + def render(self, data, parser_name, negate=False): + """render""" + # add path to the data before rendering + data = data.copy() + data.update(self._overrides) + # call the original method + return super(VrfTemplate, self).render(data, parser_name, negate) + + # fmt: off + PARSERS = [ + + # set system ntp allow_clients address <address> + { + "name": "allow_clients", + "getval": re.compile( + r""" + ^set\s(?P<path>system|service)\sntp\s(?P<ac>allow-clients|allow-client)\saddress (\s(?P<ipaddress>\S+))? + $""", + re.VERBOSE, + ), + "setval": "{{_path}} ntp {{_ac}} address {{allow_clients}}", + "result": { + "allow_clients": ["{{ipaddress}}"], + }, + }, + + # set system ntp allow_clients + { + "name": "allow_clients_delete", + "getval": re.compile( + r""" + ^set\s(?P<path>system|service)\sntp\s(?P<ac>allow-clients|allow-client) + $""", + re.VERBOSE, + ), + "setval": "{{_path}} ntp {{_ac}}", + "result": { + + }, + + }, + + # set system ntp listen_address <address> + { + "name": "listen_addresses", + "getval": re.compile( + r""" + ^set\s(?P<path>system|service)\sntp\slisten-address (\s(?P<ip_address>\S+))? + $""", + re.VERBOSE, + ), + "setval": "{{_path}} ntp listen-address {{listen_addresses}}", + "result": { + "listen_addresses": ["{{ip_address}}"], + }, + }, + + # set system ntp listen_address + { + "name": "listen_addresses_delete", + "getval": re.compile( + r""" + ^set\s(?P<path>system|service)\sntp\slisten-address + $""", + re.VERBOSE, + ), + "setval": "{{_path}} ntp listen-address", + "result": { + }, + }, + + # set {{path}} ntp - for deleting the ntp configuration + { + "name": "service_delete", + "getval": re.compile( + r""" + ^set\s(?P<path>system|service)\sntp$ + $""", + re.VERBOSE, + ), + "setval": "{{_path}} ntp", + "result": { + }, + }, + + # set system ntp server <name> + { + "name": "server", + "getval": re.compile( + r""" + ^set\s(?P<path>system|service)\sntp\sserver (\s(?P<name>\S+)) + $""", + re.VERBOSE, + ), + "setval": "{{_path}} ntp server {{server}}", + "result": { + "servers": { + "{{name}}": { + "server": "{{name}}", + }, + }, + + }, + }, + + # set system ntp server <name> <options> + { + "name": "options", + "getval": re.compile( + r""" + ^set\s(?P<path>system|service)\sntp\sserver + \s(?P<name>\S+) + \s(?P<options>dynamic|preempt|pool|noselect|prefer|nts|interleave|ptp) + $""", + re.VERBOSE, + ), + "setval": "{{_path}} ntp server {{server}} {{options}}", + "result": { + "servers": { + "{{name}}": { + "server": "{{name}}", + "options": ["{{options}}"], + }, + }, + }, + }, + ] + # fmt: on diff --git a/plugins/modules/vyos_vrf.py b/plugins/modules/vyos_vrf.py new file mode 100644 index 00000000..423bc546 --- /dev/null +++ b/plugins/modules/vyos_vrf.py @@ -0,0 +1,799 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# Copyright 2024 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_vrf +""" + +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +DOCUMENTATION = """ +module: vyos_vrf +version_added: 1.0.0 +short_description: VRF resource module +description: +- This module manages vrf configuration on devices running Vyos +author: +- Evgeny Molotkov (@omnom62) +notes: +- Tested against vyos 1.4+ +- This module works with connection C(network_cli). +options: + config: + description: List of vrf configuration. + type: dict + suboptions: + bind-to-all: + default: false + description: Enable binding services to all VRFs + type: bool + instances: + description: Virtual Routing and Forwarding instance + type: list + elements: dict + suboptions: + name: + description: VRF instance name + type: str + description: + description: Description + type: str + disabled: + default: false + description: Administratively disable interface + type: bool + aliases: ['disable'] + table_id: + description: Routing table associated with this instance + type: int + vni: + description: Virtual Network Identifier + type: int + 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 ntp). + - The states I(replaced) and I(overridden) have identical + behaviour for this module. + - The state I(parsed) reads the configuration from C(show configuration commands | grep ntp) 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: + - deleted + - merged + - overridden + - replaced + - gathered + - rendered + - parsed + default: merged +""" + +EXAMPLES = """ +# # ------------------- +# # 1. Using merged +# # ------------------- + +# # Before state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set service ntp server time1.vyos.net +# set service ntp server time2.vyos.net +# set service ntp server time3.vyos.net +# vyos@vyos:~$ + +# # Task +# # ------------- +- name: Replace the existing ntp config with the new config + vyos.vyos.vyos_ntp_global: + config: + allow_clients: + - 10.6.6.0/24 + listen_addresses: + - 10.1.3.1 + servers: + - server: 203.0.113.0 + options: + - prefer + + +# Task output: +# ------------- +# "after": { +# "allow_clients": [ +# "10.6.6.0/24" +# ], +# "listen_addresses": [ +# "10.1.3.1" +# ], +# "servers": [ +# { +# "server": "ser", +# "options": [ +# "prefer" +# ] +# }, +# { +# "server": "time1.vyos.net" +# }, +# { +# "server": "time2.vyos.net" +# }, +# { +# "server": "time3.vyos.net" +# } +# ] +# }, +# "before": { +# }, +# "changed": true, +# "commands": [ +# "set service ntp allow-clients address 10.6.6.0/24", +# "set service ntp listen-address 10.1.3.1", +# "set service ntp server 203.0.113.0 prefer" +# ] + +# After state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set service ntp allow-clients address '10.6.6.0/24' +# set service ntp listen-address '10.1.3.1' +# set service ntp server 203.0.113.0 prefer, +# set service ntp server time1.vyos.net +# set service ntp server time2.vyos.net +# set service ntp server time3.vyos.net +# vyos@vyos:~$ + + +# # ------------------- +# # 2. Using replaced +# # ------------------- + +# # Before state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set service ntp allow-clients address '10.4.9.0/24' +# set service ntp allow-clients address '10.4.7.0/24' +# set service ntp allow-clients address '10.1.2.0/24' +# set service ntp allow-clients address '10.2.3.0/24' +# set service ntp listen-address '10.1.9.16' +# set service ntp listen-address '10.5.3.2' +# set service ntp listen-address '10.7.9.21' +# set service ntp listen-address '10.8.9.4' +# set service ntp listen-address '10.4.5.1' +# set service ntp server 10.3.6.5 noselect +# set service ntp server 10.3.6.5 dynamic +# set service ntp server 10.3.6.5 preempt +# set service ntp server 10.3.6.5 prefer +# set service ntp server server4 noselect +# set service ntp server server4 dynamic +# set service ntp server server5 +# set service ntp server time1.vyos.net +# set service ntp server time2.vyos.net +# set service ntp server time3.vyos.net +# vyos@vyos:~$ + +# # Task +# # ------------- +- name: Replace the existing ntp config with the new config + vyos.vyos.vyos_ntp_global: + config: + allow_clients: + - 10.6.6.0/24 + listen_addresses: + - 10.1.3.1 + servers: + - server: 203.0.113.0 + options: + - prefer + state: replaced + + +# # Task output: +# # ------------- +# "after": { +# "allow_clients": [ +# "10.6.6.0/24" +# ], +# "listen_addresses": [ +# "10.1.3.1" +# ], +# "servers": [ +# { +# "server": "ser", +# "options": [ +# "prefer" +# ] +# }, +# { +# "server": "time1.vyos.net" +# }, +# { +# "server": "time2.vyos.net" +# }, +# { +# "server": "time3.vyos.net" +# } +# ] +# }, +# "before": { +# "allow_clients": [ +# "10.4.7.0/24", +# "10.2.3.0/24", +# "10.1.2.0/24", +# "10.4.9.0/24" +# ], +# "listen_addresses": [ +# "10.7.9.21", +# "10.4.5.1", +# "10.5.3.2", +# "10.8.9.4", +# "10.1.9.16" +# ], +# "servers": [ +# { +# "server": "10.3.6.5", +# "options": [ +# "noselect", +# "dynamic", +# "preempt", +# "prefer" +# ] +# }, +# { +# "server": "server4", +# "options": [ +# "noselect", +# "dynamic" +# ] +# }, +# { +# "server": "server5" +# }, +# { +# "server": "time1.vyos.net" +# }, +# { +# "server": "time2.vyos.net" +# }, +# { +# "server": "time3.vyos.net" +# } +# ] +# }, +# "changed": true, +# "commands": [ +# "delete service ntp allow-clients address 10.4.7.0/24", +# "delete service ntp allow-clients address 10.2.3.0/24", +# "delete service ntp allow-clients address 10.1.2.0/24", +# "delete service ntp allow-clients address 10.4.9.0/24", +# "delete service ntp listen-address 10.7.9.21", +# "delete service ntp listen-address 10.4.5.1", +# "delete service ntp listen-address 10.5.3.2", +# "delete service ntp listen-address 10.8.9.4", +# "delete service ntp listen-address 10.1.9.16", +# "delete service ntp server 10.3.6.5", +# "delete service ntp server server4", +# "delete service ntp server server5", +# "set service ntp allow-clients address 10.6.6.0/24", +# "set service ntp listen-address 10.1.3.1", +# "set service ntp server 203.0.113.0 prefer" +# ] + +# After state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set service ntp allow-clients address '10.6.6.0/24' +# set service ntp listen-address '10.1.3.1' +# set service ntp server 203.0.113.0 prefer, +# set service ntp server time1.vyos.net +# set service ntp server time2.vyos.net +# set service ntp server time3.vyos.net +# vyos@vyos:~$ + +# # ------------------- +# # 3. Using overridden +# # ------------------- + +# # Before state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set service ntp allow-clients address '10.6.6.0/24' +# set service ntp listen-address '10.1.3.1' +# set service ntp server 203.0.113.0 prefer, +# set service ntp server time1.vyos.net +# set service ntp server time2.vyos.net +# set service ntp server time3.vyos.net +# vyos@vyos:~$ + +# Task +# ------------- +- name: Override ntp config + vyos.vyos.vyos_ntp_global: + config: + allow_clients: + - 10.3.3.0/24 + listen_addresses: + - 10.7.8.1 + servers: + - server: server1 + options: + - dynamic + - prefer + + - server: server2 + options: + - noselect + - preempt + + - server: serv + state: overridden + +# # Task output: +# # ------------- +# "after": { +# "allow_clients": [ +# "10.3.3.0/24" +# ], +# "listen_addresses": [ +# "10.7.8.1" +# ], +# "servers": [ +# { +# "server": "serv" +# }, +# { +# "server": "server1", +# "options": [ +# "dynamic", +# "prefer" +# ] +# }, +# { +# "server": "server2", +# "options": [ +# "noselect", +# "preempt" +# ] +# }, +# { +# "server": "time1.vyos.net" +# }, +# { +# "server": "time2.vyos.net" +# }, +# { +# "server": "time3.vyos.net" +# } +# ] +# }, +# "before": { +# "allow_clients": [ +# "10.6.6.0/24" +# ], +# "listen_addresses": [ +# "10.1.3.1" +# ], +# "servers": [ +# { +# "server": "ser", +# "options": [ +# "prefer" +# ] +# }, +# { +# "server": "time1.vyos.net" +# }, +# { +# "server": "time2.vyos.net" +# }, +# { +# "server": "time3.vyos.net" +# } +# ] +# }, +# "changed": true, +# "commands": [ +# "delete service ntp allow-clients address 10.6.6.0/24", +# "delete service ntp listen-address 10.1.3.1", +# "delete service ntp server ser", +# "set service ntp allow-clients address 10.3.3.0/24", +# "set service ntp listen-address 10.7.8.1", +# "set service ntp server server1 dynamic", +# "set service ntp server server1 prefer", +# "set service ntp server server2 noselect", +# "set service ntp server server2 preempt", +# "set service ntp server serv" +# ] + +# After state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set service ntp allow-clients address '10.3.3.0/24' +# set service ntp listen-address '10.7.8.1' +# set service ntp server serv +# set service ntp server server1 dynamic +# set service ntp server server1 prefer +# set service ntp server server2 noselect +# set service ntp server server2 preempt +# set service ntp server time1.vyos.net +# set service ntp server time2.vyos.net +# set service ntp server time3.vyos.net +# vyos@vyos:~$ + +# 4. Using gathered +# ------------------- + +# # Before state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set service ntp allow-clients address '10.3.3.0/24' +# set service ntp listen-address '10.7.8.1' +# set service ntp server serv +# set service ntp server server1 dynamic +# set service ntp server server1 prefer +# set service ntp server server2 noselect +# set service ntp server server2 preempt +# set service ntp server time1.vyos.net +# set service ntp server time2.vyos.net +# set service ntp server time3.vyos.net +# vyos@vyos:~$ + +# Task +# ------------- +- name: Gather ntp config + vyos.vyos.vyos_ntp_global: + state: gathered + +# # Task output: +# # ------------- +# "gathered": { +# "allow_clients": [ +# "10.3.3.0/24" +# ], +# "listen_addresses": [ +# "10.7.8.1" +# ], +# "servers": [ +# { +# "server": "serv" +# }, +# { +# "server": "server1", +# "options": [ +# "dynamic", +# "prefer" +# ] +# }, +# { +# "server": "server2", +# "options": [ +# "noselect", +# "preempt" +# ] +# }, +# { +# "server": "time1.vyos.net" +# }, +# { +# "server": "time2.vyos.net" +# }, +# { +# "server": "time3.vyos.net" +# } +# ] +# } + +# After state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set service ntp allow-clients address '10.3.3.0/24' +# set service ntp listen-address '10.7.8.1' +# set service ntp server serv +# set service ntp server server1 dynamic +# set service ntp server server1 prefer +# set service ntp server server2 noselect +# set service ntp server server2 preempt +# set service ntp server time1.vyos.net +# set service ntp server time2.vyos.net +# set service ntp server time3.vyos.net +# vyos@vyos:~$ + + +# # ------------------- +# # 5. Using deleted +# # ------------------- + +# # Before state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set service ntp allow-clients address '10.3.3.0/24' +# set service ntp listen-address '10.7.8.1' +# set service ntp server serv +# set service ntp server server1 dynamic +# set service ntp server server1 prefer +# set service ntp server server2 noselect +# set service ntp server server2 preempt +# set service ntp server time1.vyos.net +# set service ntp server time2.vyos.net +# set service ntp server time3.vyos.net +# vyos@vyos:~$ + +# # Task +# # ------------- +- name: Delete ntp config + vyos.vyos.vyos_ntp_global: + state: deleted + + +# # Task output: +# # ------------- +# "after": { +# "servers": [ +# { +# "server": "time1.vyos.net" +# }, +# { +# "server": "time2.vyos.net" +# }, +# { +# "server": "time3.vyos.net" +# } +# ] +# }, +# "before": { +# "allow_clients": [ +# "10.3.3.0/24" +# ], +# "listen_addresses": [ +# "10.7.8.1" +# ], +# "servers": [ +# { +# "server": "serv" +# }, +# { +# "server": "server1", +# "options": [ +# "dynamic", +# "prefer" +# ] +# }, +# { +# "server": "server2", +# "options": [ +# "noselect", +# "preempt" +# ] +# }, +# { +# "server": "time1.vyos.net" +# }, +# { +# "server": "time2.vyos.net" +# }, +# { +# "server": "time3.vyos.net" +# } +# ] +# }, +# "changed": true, +# "commands": [ +# "delete service ntp allow-clients", +# "delete service ntp listen-address", +# "delete service ntp server serv", +# "delete service ntp server server1", +# "delete service ntp server server2" +# +# ] + +# After state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set service ntp server time1.vyos.net +# set service ntp server time2.vyos.net +# set service ntp server time3.vyos.net +# vyos@vyos:~$ + + +# # ------------------- +# # 6. Using rendered +# # ------------------- + +# # Before state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set service ntp server time1.vyos.net +# set service ntp server time2.vyos.net +# set service ntp server time3.vyos.net +# vyos@vyos:~$ + +# Task +# ------------- +- name: Render ntp config + vyos.vyos.vyos_ntp_global: + config: + allow_clients: + - 10.7.7.0/24 + - 10.8.8.0/24 + listen_addresses: + - 10.7.9.1 + servers: + - server: server7 + - server: server45 + options: + - noselect + - prefer + - pool + - server: time1.vyos.net + - server: time2.vyos.net + - server: time3.vyos.net + state: rendered + +# # Task output: +# # ------------- +# "rendered": [ +# "set service ntp allow-clients address 10.7.7.0/24", +# "set service ntp allow-clients address 10.8.8.0/24", +# "set service ntp listen-address 10.7.9.1", +# "set service ntp server server7", +# "set service ntp server server45 noselect", +# "set service ntp server server45 prefer", +# "set service ntp server server45 pool", +# "set service ntp server time1.vyos.net", +# "set service ntp server time2.vyos.net", +# "set service ntp server time3.vyos.net" +# ] + + +# # ------------------- +# # 7. Using parsed +# # ------------------- + +# # sample_config.cfg: +# # ------------- +# "set service ntp allow-clients address 10.7.7.0/24", +# "set service ntp listen-address 10.7.9.1", +# "set service ntp server server45 noselect", +# "set service ntp allow-clients addres 10.8.6.0/24", +# "set service ntp listen-address 10.5.4.1", +# "set service ntp server server45 dynamic", +# "set service ntp server time1.vyos.net", +# "set service ntp server time2.vyos.net", +# "set service ntp server time3.vyos.net" + +# Task: +# ------------- +- name: Parse externally provided ntp configuration + vyos.vyos.vyos_ntp_global: + running_config: "{{ lookup('file', './sample_config.cfg') }}" + state: parsed + +# # Task output: +# # ------------- +# parsed = { +# "allow_clients": [ +# "10.7.7.0/24", +# "10.8.6.0/24 +# ], +# "listen_addresses": [ +# "10.5.4.1", +# "10.7.9.1" +# ], +# "servers": [ +# { +# "server": "server45", +# "options": [ +# "noselect", +# "dynamic" +# +# ] +# }, +# { +# "server": "time1.vyos.net" +# }, +# { +# "server": "time2.vyos.net" +# }, +# { +# "server": "time3.vyos.net" +# } +# +# ] +# } +""" + +RETURN = """ +before: + description: The configuration prior to the module execution. + returned: when I(state) is C(merged), C(replaced), C(overridden), C(deleted) or C(purged) + 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), C(deleted) or C(purged) + type: list + sample: + - set system ntp server server1 dynamic + - set system ntp server server1 prefer + - set system ntp server server2 noselect + - set system ntp server server2 preempt + - set system ntp server server_add preempt +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 system ntp server server1 dynamic + - set system ntp server server1 prefer + - set system ntp server server2 noselect + - set system ntp server server2 preempt + - set system ntp server server_add preempt +gathered: + description: Facts about the network resource gathered from the remote device as structured data. + returned: when I(state) is C(gathered) + type: list + 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: list + 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.vrf.vrf import VrfArgs +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.vrf.vrf import Vrf + + +def main(): + """ + Main entry point for module execution + + :returns: the result form module invocation + """ + module = AnsibleModule( + argument_spec=VrfArgs.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 = Vrf(module).execute_module() + module.exit_json(**result) + + +if __name__ == "__main__": + main()