diff options
author | YVarshitha <63169351+YVarshitha@users.noreply.github.com> | 2021-10-02 13:28:30 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-02 17:28:30 +0000 |
commit | 85ea49f10a6167dea4c08e71a1e8e5a62cb57395 (patch) | |
tree | 0bfd9aa0bc06c1d342944fef3bc18f7c3d7ec047 | |
parent | 88689227265b7f6b272b695a275f4a47735ddcc2 (diff) | |
download | vyos.vyos-85ea49f10a6167dea4c08e71a1e8e5a62cb57395.tar.gz vyos.vyos-85ea49f10a6167dea4c08e71a1e8e5a62cb57395.zip |
Add vyos_ntp resource module (#175)
Add vyos_ntp resource module
SUMMARY
Resource module vyos_ntp
ISSUE TYPE
New Module Pull Request
COMPONENT NAME
vyos_ntp
ADDITIONAL INFORMATION
Reviewed-by: GomathiselviS <None>
Reviewed-by: Nilashish Chakraborty <nilashishchakraborty8@gmail.com>
Reviewed-by: None <None>
32 files changed, 3229 insertions, 11 deletions
@@ -53,6 +53,7 @@ Name | Description [vyos.vyos.vyos_lldp_interfaces](https://github.com/ansible-collections/vyos.vyos/blob/main/docs/vyos.vyos.vyos_lldp_interfaces_module.rst)|LLDP interfaces resource module [vyos.vyos.vyos_logging](https://github.com/ansible-collections/vyos.vyos/blob/main/docs/vyos.vyos.vyos_logging_module.rst)|Manage logging on network devices [vyos.vyos.vyos_logging_global](https://github.com/ansible-collections/vyos.vyos/blob/main/docs/vyos.vyos.vyos_logging_global_module.rst)|Logging resource module +[vyos.vyos.vyos_ntp_global](https://github.com/ansible-collections/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ntp_global_module.rst)|Manages ntp modules of Vyos network devices [vyos.vyos.vyos_ospf_interfaces](https://github.com/ansible-collections/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ospf_interfaces_module.rst)|OSPF Interfaces Resource Module. [vyos.vyos.vyos_ospfv2](https://github.com/ansible-collections/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ospfv2_module.rst)|OSPFv2 resource module [vyos.vyos.vyos_ospfv3](https://github.com/ansible-collections/vyos.vyos/blob/main/docs/vyos.vyos.vyos_ospfv3_module.rst)|OSPFV3 resource module diff --git a/changelogs/fragments/vyos_ntp.yml b/changelogs/fragments/vyos_ntp.yml new file mode 100644 index 00000000..76829060 --- /dev/null +++ b/changelogs/fragments/vyos_ntp.yml @@ -0,0 +1,4 @@ +--- + +minor_changes: + - Add vyos_ntp Resource Module diff --git a/docs/vyos.vyos.vyos_ntp_global_module.rst b/docs/vyos.vyos.vyos_ntp_global_module.rst new file mode 100644 index 00000000..7f27493c --- /dev/null +++ b/docs/vyos.vyos.vyos_ntp_global_module.rst @@ -0,0 +1,974 @@ +.. _vyos.vyos.vyos_ntp_global_module: + + +************************* +vyos.vyos.vyos_ntp_global +************************* + +**Manages ntp modules of Vyos network devices** + + +Version added: 2.4.0 + +.. contents:: + :local: + :depth: 1 + + +Synopsis +-------- +- This module manages ntp 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 configurations for ntp module</div> + </td> + </tr> + <tr> + <td class="elbow-placeholder"></td> + <td colspan="2"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>allow_clients</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=string</span> + </div> + </td> + <td> + </td> + <td> + <div>Network Time Protocol (NTP) server options</div> + </td> + </tr> + <tr> + <td class="elbow-placeholder"></td> + <td colspan="2"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>listen_addresses</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=string</span> + </div> + </td> + <td> + </td> + <td> + <div>local IP addresses for service to listen on</div> + </td> + </tr> + <tr> + <td class="elbow-placeholder"></td> + <td colspan="2"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>servers</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>Network Time Protocol (NTP) server</div> + </td> + </tr> + <tr> + <td class="elbow-placeholder"></td> + <td class="elbow-placeholder"></td> + <td colspan="1"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>options</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=string</span> + </div> + </td> + <td> + <ul style="margin: 0; padding: 0"><b>Choices:</b> + <li>noselect</li> + <li>dynamic</li> + <li>preempt</li> + <li>prefer</li> + </ul> + </td> + <td> + <div>server options for NTP</div> + </td> + </tr> + <tr> + <td class="elbow-placeholder"></td> + <td class="elbow-placeholder"></td> + <td colspan="1"> + <div class="ansibleOptionAnchor" id="parameter-"></div> + <b>server</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>server name for NTP</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.3 + - This module works with connection ``network_cli``. + + + +Examples +-------- + +.. code-block:: yaml + + # # ------------------- + # # 1. Using merged + # # ------------------- + + # # Before state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set system ntp server time1.vyos.net + # set system ntp server time2.vyos.net + # set system 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 system ntp allow-clients address 10.6.6.0/24", + # "set system ntp listen-address 10.1.3.1", + # "set system ntp server 203.0.113.0 prefer" + # ] + + # After state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set system ntp allow-clients address '10.6.6.0/24' + # set system ntp listen-address '10.1.3.1' + # set system ntp server 203.0.113.0 prefer, + # set system ntp server time1.vyos.net + # set system ntp server time2.vyos.net + # set system ntp server time3.vyos.net + # vyos@vyos:~$ + + + # # ------------------- + # # 2. Using replaced + # # ------------------- + + # # Before state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set system ntp allow-clients address '10.4.9.0/24' + # set system ntp allow-clients address '10.4.7.0/24' + # set system ntp allow-clients address '10.1.2.0/24' + # set system ntp allow-clients address '10.2.3.0/24' + # set system ntp listen-address '10.1.9.16' + # set system ntp listen-address '10.5.3.2' + # set system ntp listen-address '10.7.9.21' + # set system ntp listen-address '10.8.9.4' + # set system ntp listen-address '10.4.5.1' + # set system ntp server 10.3.6.5 noselect + # set system ntp server 10.3.6.5 dynamic + # set system ntp server 10.3.6.5 preempt + # set system ntp server 10.3.6.5 prefer + # set system ntp server server4 noselect + # set system ntp server server4 dynamic + # set system ntp server server5 + # set system ntp server time1.vyos.net + # set system ntp server time2.vyos.net + # set system 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 system ntp allow-clients address 10.4.7.0/24", + # "delete system ntp allow-clients address 10.2.3.0/24", + # "delete system ntp allow-clients address 10.1.2.0/24", + # "delete system ntp allow-clients address 10.4.9.0/24", + # "delete system ntp listen-address 10.7.9.21", + # "delete system ntp listen-address 10.4.5.1", + # "delete system ntp listen-address 10.5.3.2", + # "delete system ntp listen-address 10.8.9.4", + # "delete system ntp listen-address 10.1.9.16", + # "delete system ntp server 10.3.6.5", + # "delete system ntp server server4", + # "delete system ntp server server5", + # "set system ntp allow-clients address 10.6.6.0/24", + # "set system ntp listen-address 10.1.3.1", + # "set system ntp server 203.0.113.0 prefer" + # ] + + # After state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set system ntp allow-clients address '10.6.6.0/24' + # set system ntp listen-address '10.1.3.1' + # set system ntp server 203.0.113.0 prefer, + # set system ntp server time1.vyos.net + # set system ntp server time2.vyos.net + # set system ntp server time3.vyos.net + # vyos@vyos:~$ + + + + # # ------------------- + # # 3. Using overridden + # # ------------------- + + # # Before state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set system ntp allow-clients address '10.6.6.0/24' + # set system ntp listen-address '10.1.3.1' + # set system ntp server 203.0.113.0 prefer, + # set system ntp server time1.vyos.net + # set system ntp server time2.vyos.net + # set system 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 system ntp allow-clients address 10.6.6.0/24", + # "delete system ntp listen-address 10.1.3.1", + # "delete system ntp server ser", + # "set system ntp allow-clients address 10.3.3.0/24", + # "set system ntp listen-address 10.7.8.1", + # "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 serv" + # ] + + # After state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set system ntp allow-clients address '10.3.3.0/24' + # set system ntp listen-address '10.7.8.1' + # set system ntp server serv + # 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 time1.vyos.net + # set system ntp server time2.vyos.net + # set system ntp server time3.vyos.net + # vyos@vyos:~$ + + + + # # ------------------- + # # 4. Using gathered + # # ------------------- + + # # Before state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set system ntp allow-clients address '10.3.3.0/24' + # set system ntp listen-address '10.7.8.1' + # set system ntp server serv + # 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 time1.vyos.net + # set system ntp server time2.vyos.net + # set system 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 system ntp allow-clients address '10.3.3.0/24' + # set system ntp listen-address '10.7.8.1' + # set system ntp server serv + # 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 time1.vyos.net + # set system ntp server time2.vyos.net + # set system ntp server time3.vyos.net + # vyos@vyos:~$ + + + # # ------------------- + # # 5. Using deleted + # # ------------------- + + # # Before state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set system ntp allow-clients address '10.3.3.0/24' + # set system ntp listen-address '10.7.8.1' + # set system ntp server serv + # 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 time1.vyos.net + # set system ntp server time2.vyos.net + # set system 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 system ntp allow-clients", + # "delete system ntp listen-address", + # "delete system ntp server serv", + # "delete system ntp server server1", + # "delete system ntp server server2" + # + # ] + + # After state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set system ntp server time1.vyos.net + # set system ntp server time2.vyos.net + # set system ntp server time3.vyos.net + # vyos@vyos:~$ + + + # # ------------------- + # # 6. Using rendered + # # ------------------- + + # # Before state: + # # ------------- + # vyos@vyos:~$ show configuration commands | grep ntp + # set system ntp server time1.vyos.net + # set system ntp server time2.vyos.net + # set system ntp server time3.vyos.net + # vyos@vyos:~$ + + # # Task + # # ------------- + - name: Gather 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 + - server: time1.vyos.net + + - server: time2.vyos.net + + - server: time3.vyos.net + + state: rendered + + + # # Task output: + # # ------------- + # "rendered": [ + # "set system ntp allow-clients address 10.7.7.0/24", + # "set system ntp allow-clients address 10.8.8.0/24", + # "set system ntp listen-address 10.7.9.1", + # "set system ntp server server7", + # "set system ntp server server45 noselect", + # "set system ntp server server45 prefer", + # "set system ntp server time1.vyos.net", + # "set system ntp server time2.vyos.net", + # "set system ntp server time3.vyos.net" + # ] + + + # # ------------------- + # # 7. Using parsed + # # ------------------- + + # # sample_config.cfg: + # # ------------- + # "set system ntp allow-clients address 10.7.7.0/24", + # "set system ntp listen-address 10.7.9.1", + # "set system ntp server server45 noselect", + # "set system ntp allow-clients addres 10.8.6.0/24", + # "set system ntp listen-address 10.5.4.1", + # "set system ntp server server45 dynamic", + # "set system ntp server time1.vyos.net", + # "set system ntp server time2.vyos.net", + # "set system 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 +~~~~~~~ + +- Varshitha Yataluru (@YVarshitha) diff --git a/meta/runtime.yml b/meta/runtime.yml index 92cb2605..35dbf506 100644 --- a/meta/runtime.yml +++ b/meta/runtime.yml @@ -86,6 +86,10 @@ plugin_routing: redirect: vyos.vyos.vyos logging: redirect: vyos.vyos.vyos + vyos_ntp_global: + redirect: vyos.vyos.vyos + ntp_global: + redirect: vyos.vyos.vyos vyos_ospfv2: redirect: vyos.vyos.vyos ospfv2: @@ -211,6 +215,8 @@ plugin_routing: warning_text: See the plugin documentation for more details logging_global: redirect: vyos.vyos.vyos_logging_global + ntp_global: + redirect: vyos.vyos.vyos_ntp_global ospfv2: redirect: vyos.vyos.vyos_ospfv2 ospfv3: diff --git a/plugins/module_utils/network/vyos/argspec/ntp_global/__init__.py b/plugins/module_utils/network/vyos/argspec/ntp_global/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/plugins/module_utils/network/vyos/argspec/ntp_global/__init__.py diff --git a/plugins/module_utils/network/vyos/argspec/ntp_global/ntp_global.py b/plugins/module_utils/network/vyos/argspec/ntp_global/ntp_global.py new file mode 100644 index 00000000..63262154 --- /dev/null +++ b/plugins/module_utils/network/vyos/argspec/ntp_global/ntp_global.py @@ -0,0 +1,72 @@ +# -*- 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 + +############################################# +# 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_ntp module +""" + + +class Ntp_globalArgs(object): # pylint: disable=R0903 + """The arg spec for the vyos_ntp module""" + + argument_spec = { + "config": { + "type": "dict", + "options": { + "allow_clients": {"type": "list", "elements": "str"}, + "listen_addresses": {"type": "list", "elements": "str"}, + "servers": { + "type": "list", + "elements": "dict", + "options": { + "server": {"type": "str"}, + "options": { + "type": "list", + "elements": "str", + "choices": [ + "noselect", + "dynamic", + "preempt", + "prefer", + ], + }, + }, + }, + }, + }, + "running_config": {"type": "str"}, + "state": { + "type": "str", + "choices": [ + "deleted", + "merged", + "overridden", + "replaced", + "gathered", + "rendered", + "parsed", + ], + "default": "merged", + }, + } # pylint: disable=C0301 diff --git a/plugins/module_utils/network/vyos/config/ntp_global/__init__.py b/plugins/module_utils/network/vyos/config/ntp_global/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/plugins/module_utils/network/vyos/config/ntp_global/__init__.py diff --git a/plugins/module_utils/network/vyos/config/ntp_global/ntp_global.py b/plugins/module_utils/network/vyos/config/ntp_global/ntp_global.py new file mode 100644 index 00000000..0a0e389f --- /dev/null +++ b/plugins/module_utils/network/vyos/config/ntp_global/ntp_global.py @@ -0,0 +1,197 @@ +# +# -*- 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.utils import ( + dict_merge, +) +from ansible_collections.ansible.netcommon.plugins.module_utils.network.common.rm_base.resource_module import ( + ResourceModule, +) +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.ntp_global import ( + NtpTemplate, +) + + +class Ntp_global(ResourceModule): + """ + The vyos_ntp config class + """ + + def __init__(self, module): + super(Ntp_global, self).__init__( + empty_fact_val={}, + facts_module=Facts(module), + module=module, + resource="ntp_global", + tmplt=NtpTemplate(), + ) + 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. + """ + + 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 + 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"]) + + # remove existing config for overridden,replaced and deleted + # 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 and "server" not in have: + self._compareoverride(want={}, have=have) + # removing the servername from the list after deleting it from haved + elif k not in wantd and 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 == "server" and sval not in [ + "0.pool.ntp.org", + "1.pool.ntp.org", + "2.pool.ntp.org", + ]: + if 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 ab074b00..e560a48a 100644 --- a/plugins/module_utils/network/vyos/facts/facts.py +++ b/plugins/module_utils/network/vyos/facts/facts.py @@ -64,6 +64,9 @@ from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.prefi 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.legacy.base import ( Default, Neighbors, @@ -90,6 +93,7 @@ FACT_RESOURCE_SUBSETS = dict( route_maps=Route_mapsFacts, prefix_lists=Prefix_listsFacts, logging_global=Logging_globalFacts, + ntp_global=Ntp_globalFacts, ) diff --git a/plugins/module_utils/network/vyos/facts/ntp_global/__init__.py b/plugins/module_utils/network/vyos/facts/ntp_global/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/plugins/module_utils/network/vyos/facts/ntp_global/__init__.py diff --git a/plugins/module_utils/network/vyos/facts/ntp_global/ntp_global.py b/plugins/module_utils/network/vyos/facts/ntp_global/ntp_global.py new file mode 100644 index 00000000..bb20e2d6 --- /dev/null +++ b/plugins/module_utils/network/vyos/facts/ntp_global/ntp_global.py @@ -0,0 +1,99 @@ +# -*- 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 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.rm_templates.ntp_global import ( + NtpTemplate, +) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.argspec.ntp_global.ntp_global import ( + Ntp_globalArgs, +) + + +class Ntp_globalFacts(object): + """The vyos ntp facts class""" + + def __init__(self, module, subspec="config", options="options"): + self._module = module + self.argument_spec = Ntp_globalArgs.argument_spec + + def get_config(self, connection): + return connection.get("show configuration commands | grep ntp") + + def populate_facts(self, connection, ansible_facts, data=None): + """Populate the facts for Ntp 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)) + # parse native config using the Ntp template + ntp_parser = NtpTemplate(lines=config_lines, module=self._module) + + objs = ntp_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( + ntp_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/ntp_global.py b/plugins/module_utils/network/vyos/rm_templates/ntp_global.py new file mode 100644 index 00000000..ac0bcbbf --- /dev/null +++ b/plugins/module_utils/network/vyos/rm_templates/ntp_global.py @@ -0,0 +1,129 @@ +# -*- 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 NtpTemplate(NetworkTemplate): + def __init__(self, lines=None, module=None): + prefix = {"set": "set", "remove": "delete"} + super(NtpTemplate, self).__init__( + lines=lines, tmplt=self, prefix=prefix, module=module + ) + + # fmt: off + PARSERS = [ + + # set system ntp allow_clients address <address> + { + "name": "allow_clients", + "getval": re.compile( + r""" + ^set\ssystem\sntp\sallow-clients\saddress (\s(?P<ipaddress>\S+))? + $""", + re.VERBOSE), + "setval": "system ntp allow-clients address {{allow_clients}}", + "result": { + "allow_clients": ["{{ipaddress}}"] + } + }, + + # set system ntp allow_clients + { + "name": "allow_clients_delete", + "getval": re.compile( + r""" + ^set\ssystem\sntp\sallow-clients + $""", + re.VERBOSE), + "setval": "system ntp allow-clients", + "result": { + + } + + }, + + # set system ntp listen_address <address> + { + "name": "listen_addresses", + "getval": re.compile( + r""" + ^set\ssystem\sntp\slisten-address (\s(?P<ip_address>\S+))? + $""", + re.VERBOSE), + "setval": "system ntp listen-address {{listen_addresses}}", + "result": { + "listen_addresses": ["{{ip_address}}"] + } + }, + + # set system ntp listen_address + { + "name": "listen_addresses_delete", + "getval": re.compile( + r""" + ^set\ssystem\sntp\slisten-address + $""", + re.VERBOSE), + "setval": "system ntp listen-address", + "result": { + } + }, + + # set system ntp server <name> + { + "name": "server", + "getval": re.compile( + r""" + ^set\ssystem\sntp\sserver (\s(?P<name>\S+))? + $""", + re.VERBOSE), + "setval": "system ntp server {{server}}", + "result": { + "servers": { + "{{name}}": { + "server": "{{name}}" + } + } + + } + }, + + # set system ntp server <name> <options> + { + "name": "options", + "getval": re.compile( + r""" + ^set\ssystem\sntp\sserver + \s(?P<name>\S+) + \s(?P<options>noselect|dynamic|preempt|prefer)? + $""", + re.VERBOSE), + "setval": "system ntp server {{server}} {{options}}", + "result": { + "servers": { + "{{name}}": { + "server": "{{name}}", + "options": ["{{options}}"] + } + } + } + } + ] + # fmt: on diff --git a/plugins/modules/vyos_ntp_global.py b/plugins/modules/vyos_ntp_global.py new file mode 100644 index 00000000..6686aa41 --- /dev/null +++ b/plugins/modules/vyos_ntp_global.py @@ -0,0 +1,812 @@ +#!/usr/bin/python +# -*- 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) + +""" +The module file for vyos_ntp_global +""" + +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + + +DOCUMENTATION = """ + module: vyos_ntp_global + version_added: 2.4.0 + short_description: Manages ntp modules of Vyos network devices + description: + - This module manages ntp configuration on devices running Vyos + author: Varshitha Yataluru (@YVarshitha) + notes: + - Tested against vyos 1.3 + - This module works with connection C(network_cli). + options: + config: + description: List of configurations for ntp module + type: dict + suboptions: + allow_clients: + description: Network Time Protocol (NTP) server options + type: list + elements: str + listen_addresses: + description: local IP addresses for service to listen on + type: list + elements: str + servers: + description: Network Time Protocol (NTP) server + type: list + elements: dict + suboptions: + server: + description: server name for NTP + type: str + options: + description: server options for NTP + type: list + elements: str + choices: + - noselect + - dynamic + - preempt + - prefer + 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 system ntp server time1.vyos.net +# set system ntp server time2.vyos.net +# set system 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 system ntp allow-clients address 10.6.6.0/24", +# "set system ntp listen-address 10.1.3.1", +# "set system ntp server 203.0.113.0 prefer" +# ] + +# After state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set system ntp allow-clients address '10.6.6.0/24' +# set system ntp listen-address '10.1.3.1' +# set system ntp server 203.0.113.0 prefer, +# set system ntp server time1.vyos.net +# set system ntp server time2.vyos.net +# set system ntp server time3.vyos.net +# vyos@vyos:~$ + + +# # ------------------- +# # 2. Using replaced +# # ------------------- + +# # Before state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set system ntp allow-clients address '10.4.9.0/24' +# set system ntp allow-clients address '10.4.7.0/24' +# set system ntp allow-clients address '10.1.2.0/24' +# set system ntp allow-clients address '10.2.3.0/24' +# set system ntp listen-address '10.1.9.16' +# set system ntp listen-address '10.5.3.2' +# set system ntp listen-address '10.7.9.21' +# set system ntp listen-address '10.8.9.4' +# set system ntp listen-address '10.4.5.1' +# set system ntp server 10.3.6.5 noselect +# set system ntp server 10.3.6.5 dynamic +# set system ntp server 10.3.6.5 preempt +# set system ntp server 10.3.6.5 prefer +# set system ntp server server4 noselect +# set system ntp server server4 dynamic +# set system ntp server server5 +# set system ntp server time1.vyos.net +# set system ntp server time2.vyos.net +# set system 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 system ntp allow-clients address 10.4.7.0/24", +# "delete system ntp allow-clients address 10.2.3.0/24", +# "delete system ntp allow-clients address 10.1.2.0/24", +# "delete system ntp allow-clients address 10.4.9.0/24", +# "delete system ntp listen-address 10.7.9.21", +# "delete system ntp listen-address 10.4.5.1", +# "delete system ntp listen-address 10.5.3.2", +# "delete system ntp listen-address 10.8.9.4", +# "delete system ntp listen-address 10.1.9.16", +# "delete system ntp server 10.3.6.5", +# "delete system ntp server server4", +# "delete system ntp server server5", +# "set system ntp allow-clients address 10.6.6.0/24", +# "set system ntp listen-address 10.1.3.1", +# "set system ntp server 203.0.113.0 prefer" +# ] + +# After state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set system ntp allow-clients address '10.6.6.0/24' +# set system ntp listen-address '10.1.3.1' +# set system ntp server 203.0.113.0 prefer, +# set system ntp server time1.vyos.net +# set system ntp server time2.vyos.net +# set system ntp server time3.vyos.net +# vyos@vyos:~$ + + + +# # ------------------- +# # 3. Using overridden +# # ------------------- + +# # Before state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set system ntp allow-clients address '10.6.6.0/24' +# set system ntp listen-address '10.1.3.1' +# set system ntp server 203.0.113.0 prefer, +# set system ntp server time1.vyos.net +# set system ntp server time2.vyos.net +# set system 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 system ntp allow-clients address 10.6.6.0/24", +# "delete system ntp listen-address 10.1.3.1", +# "delete system ntp server ser", +# "set system ntp allow-clients address 10.3.3.0/24", +# "set system ntp listen-address 10.7.8.1", +# "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 serv" +# ] + +# After state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set system ntp allow-clients address '10.3.3.0/24' +# set system ntp listen-address '10.7.8.1' +# set system ntp server serv +# 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 time1.vyos.net +# set system ntp server time2.vyos.net +# set system ntp server time3.vyos.net +# vyos@vyos:~$ + + + +# # ------------------- +# # 4. Using gathered +# # ------------------- + +# # Before state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set system ntp allow-clients address '10.3.3.0/24' +# set system ntp listen-address '10.7.8.1' +# set system ntp server serv +# 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 time1.vyos.net +# set system ntp server time2.vyos.net +# set system 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 system ntp allow-clients address '10.3.3.0/24' +# set system ntp listen-address '10.7.8.1' +# set system ntp server serv +# 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 time1.vyos.net +# set system ntp server time2.vyos.net +# set system ntp server time3.vyos.net +# vyos@vyos:~$ + + +# # ------------------- +# # 5. Using deleted +# # ------------------- + +# # Before state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set system ntp allow-clients address '10.3.3.0/24' +# set system ntp listen-address '10.7.8.1' +# set system ntp server serv +# 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 time1.vyos.net +# set system ntp server time2.vyos.net +# set system 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 system ntp allow-clients", +# "delete system ntp listen-address", +# "delete system ntp server serv", +# "delete system ntp server server1", +# "delete system ntp server server2" +# +# ] + +# After state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set system ntp server time1.vyos.net +# set system ntp server time2.vyos.net +# set system ntp server time3.vyos.net +# vyos@vyos:~$ + + +# # ------------------- +# # 6. Using rendered +# # ------------------- + +# # Before state: +# # ------------- +# vyos@vyos:~$ show configuration commands | grep ntp +# set system ntp server time1.vyos.net +# set system ntp server time2.vyos.net +# set system ntp server time3.vyos.net +# vyos@vyos:~$ + +# # Task +# # ------------- +- name: Gather 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 + - server: time1.vyos.net + + - server: time2.vyos.net + + - server: time3.vyos.net + + state: rendered + + +# # Task output: +# # ------------- +# "rendered": [ +# "set system ntp allow-clients address 10.7.7.0/24", +# "set system ntp allow-clients address 10.8.8.0/24", +# "set system ntp listen-address 10.7.9.1", +# "set system ntp server server7", +# "set system ntp server server45 noselect", +# "set system ntp server server45 prefer", +# "set system ntp server time1.vyos.net", +# "set system ntp server time2.vyos.net", +# "set system ntp server time3.vyos.net" +# ] + + +# # ------------------- +# # 7. Using parsed +# # ------------------- + +# # sample_config.cfg: +# # ------------- +# "set system ntp allow-clients address 10.7.7.0/24", +# "set system ntp listen-address 10.7.9.1", +# "set system ntp server server45 noselect", +# "set system ntp allow-clients addres 10.8.6.0/24", +# "set system ntp listen-address 10.5.4.1", +# "set system ntp server server45 dynamic", +# "set system ntp server time1.vyos.net", +# "set system ntp server time2.vyos.net", +# "set system 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.ntp_global.ntp_global import ( + Ntp_globalArgs, +) +from ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.config.ntp_global.ntp_global import ( + Ntp_global, +) + + +def main(): + """ + Main entry point for module execution + + :returns: the result form module invocation + """ + module = AnsibleModule( + argument_spec=Ntp_globalArgs.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 = Ntp_global(module).execute_module() + module.exit_json(**result) + + +if __name__ == "__main__": + main() diff --git a/tests/integration/targets/vyos_ntp_global/defaults/main.yaml b/tests/integration/targets/vyos_ntp_global/defaults/main.yaml new file mode 100644 index 00000000..852a6bee --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/defaults/main.yaml @@ -0,0 +1,3 @@ +--- +testcase: '[^_].*' +test_items: [] diff --git a/tests/integration/targets/vyos_ntp_global/tasks/cli.yaml b/tests/integration/targets/vyos_ntp_global/tasks/cli.yaml new file mode 100644 index 00000000..93eb2fe4 --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/tasks/cli.yaml @@ -0,0 +1,19 @@ +--- +- name: Collect all cli test cases + find: + paths: '{{ role_path }}/tests/cli' + patterns: '{{ testcase }}.yaml' + use_regex: true + register: test_cases + delegate_to: localhost + +- name: Set test_items + set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}" + +- name: Run test case (connection=ansible.netcommon.network_cli) + include: '{{ test_case_to_run }}' + vars: + ansible_connection: ansible.netcommon.network_cli + with_items: '{{ test_items }}' + loop_control: + loop_var: test_case_to_run diff --git a/tests/integration/targets/vyos_ntp_global/tasks/main.yaml b/tests/integration/targets/vyos_ntp_global/tasks/main.yaml new file mode 100644 index 00000000..b957d2f4 --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/tasks/main.yaml @@ -0,0 +1,4 @@ +--- +- include: cli.yaml + tags: + - network_cli diff --git a/tests/integration/targets/vyos_ntp_global/tests/cli/_parsed.cfg b/tests/integration/targets/vyos_ntp_global/tests/cli/_parsed.cfg new file mode 100644 index 00000000..0608aef6 --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/tests/cli/_parsed.cfg @@ -0,0 +1,5 @@ +set system ntp server 10.3.6.5 noselect +set system ntp server 10.3.6.5 dynamic +set system ntp server 0.pool.ntp.org +set system ntp server 1.pool.ntp.org +set system ntp server 2.pool.ntp.org diff --git a/tests/integration/targets/vyos_ntp_global/tests/cli/_populate_config.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/_populate_config.yaml new file mode 100644 index 00000000..035fcba7 --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/tests/cli/_populate_config.yaml @@ -0,0 +1,13 @@ +--- +- name: setup + vyos.vyos.vyos_config: + lines: + - set system ntp server server4 dynamic + - set system ntp server server5 preempt + - set system ntp server 10.3.6.5 noselect + - set system ntp server 10.3.6.5 preempt + - set system ntp server server4 noselect + - set system ntp server 10.3.6.5 prefer + ignore_errors: true + vars: + ansible_connection: ansible.netcommon.network_cli diff --git a/tests/integration/targets/vyos_ntp_global/tests/cli/_remove_config.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/_remove_config.yaml new file mode 100644 index 00000000..6017bdb3 --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/tests/cli/_remove_config.yaml @@ -0,0 +1,8 @@ +--- +- name: Remove pre-existing ntp_global config + vyos.vyos.vyos_ntp_global: + config: + state: deleted + ignore_errors: true + vars: + ansible_connection: ansible.netcommon.network_cli diff --git a/tests/integration/targets/vyos_ntp_global/tests/cli/deleted.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/deleted.yaml new file mode 100644 index 00000000..530e2add --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/tests/cli/deleted.yaml @@ -0,0 +1,40 @@ +--- +- debug: + msg: START vyos_ntp_global deleted integration tests on connection={{ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + + - name: Delete the provided configuration + register: result + vyos.vyos.vyos_ntp_global: &id001 + config: + state: deleted + + - name: Assert that before dicts were correctly generated + assert: + that: + - result.changed == true + - result.commands == deleted.commands + + - name: Assert that the after dicts were correctly generated + assert: + that: + - result.after == populate.config + + - name: Delete the existing configuration with the provided running configuration + (IDEMPOTENT) + register: result + vyos.vyos.vyos_ntp_global: *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_ntp_global/tests/cli/empty_config.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/empty_config.yaml new file mode 100644 index 00000000..ec29f161 --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/tests/cli/empty_config.yaml @@ -0,0 +1,60 @@ +--- +- debug: + msg: START vyos_ntp_global empty_config integration tests on connection={{ + ansible_connection }} + +- name: Merged with empty config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_ntp_global: + config: + state: merged + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state merged' + +- name: Replaced with empty config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_ntp_global: + config: + state: replaced + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state replaced' + +- name: Overridden with empty config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_ntp_global: + config: + state: overridden + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state overridden' + +- name: Parsed with empty running_config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_ntp_global: + running_config: + state: parsed + +- assert: + that: + - result.msg == 'value of running_config parameter must not be empty for state + parsed' + +- name: Rendered with empty config should give appropriate error message + register: result + ignore_errors: true + vyos.vyos.vyos_ntp_global: + config: + state: rendered + +- assert: + that: + - result.msg == 'value of config parameter must not be empty for state rendered' diff --git a/tests/integration/targets/vyos_ntp_global/tests/cli/gathered.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/gathered.yaml new file mode 100644 index 00000000..5ac350d4 --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/tests/cli/gathered.yaml @@ -0,0 +1,24 @@ +--- +- debug: + msg: START vyos_ntp_global gathered integration tests on connection={{ + ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + + - name: Gather config from the device in structured format. + register: result + vyos.vyos.vyos_ntp_global: + state: gathered + + - vyos.vyos.vyos_facts: + gather_network_resources: ntp_global + + - name: Assert that facts are correctly generated + assert: + that: + - result.changed == false + - result.gathered == ansible_facts['network_resources']['ntp_global'] diff --git a/tests/integration/targets/vyos_ntp_global/tests/cli/merged.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/merged.yaml new file mode 100644 index 00000000..2a22ad8f --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/tests/cli/merged.yaml @@ -0,0 +1,58 @@ +--- +- debug: + msg: START vyos_ntp_global 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_ntp_global: &id001 + config: + servers: + - server: server5 + + - server: server4 + options: + - noselect + - dynamic + + - server: 10.3.6.5 + options: + - noselect + - preempt + - dynamic + - prefer + state: merged + + - vyos.vyos.vyos_facts: + gather_network_resources: ntp_global + + - assert: + that: + - result.commands|length == 7 + - result.changed == true + - result.commands|symmetric_difference(merged.commands) == [] + - result.after == ansible_facts['network_resources']['ntp_global'] + - result.after == merged.after + + + - name: Assert that before dicts were correctly generated + assert: + that: + - result.before == populate.config + + - name: + Merge the provided configuration with the existing running configuration + (IDEMPOTENT) + register: result + vyos.vyos.vyos_ntp_global: *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_ntp_global/tests/cli/overridden.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/overridden.yaml new file mode 100644 index 00000000..2269e747 --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/tests/cli/overridden.yaml @@ -0,0 +1,53 @@ +--- +- debug: + msg: START vyos_ntp_global overridden integration tests on connection={{ + ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + + - name: Override the existing configuration with the provided running configuration + register: result + vyos.vyos.vyos_ntp_global: &id001 + config: + servers: + - server: server1 + options: + - dynamic + - prefer + + - server: server2 + options: + - noselect + - preempt + + - server: server_add + options: + - preempt + state: overridden + + - vyos.vyos.vyos_facts: + gather_network_resources: ntp_global + + - assert: + that: + - result.changed == true + - result.after == ansible_facts['network_resources']['ntp_global'] + - result.after == overridden.after + + - name: Override the existing configuration with the provided running configuration + (IDEMPOTENT) + register: result + vyos.vyos.vyos_ntp_global: *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_ntp_global/tests/cli/parsed.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/parsed.yaml new file mode 100644 index 00000000..c660ac47 --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/tests/cli/parsed.yaml @@ -0,0 +1,17 @@ +--- +- debug: + msg: START vyos_ntp_global parsed integration tests on connection={{ ansible_connection + }} + +- name: Provide the running configuration for parsing (config to be parsed) + register: result + vyos.vyos.vyos_ntp_global: + running_config: "{{ lookup('file', '_parsed.cfg') }}" + state: parsed + + +- name: Assert that config was correctly parsed + assert: + that: + - result.changed == false + - result.parsed == parsed.after diff --git a/tests/integration/targets/vyos_ntp_global/tests/cli/rendered.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/rendered.yaml new file mode 100644 index 00000000..1c1edd7e --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/tests/cli/rendered.yaml @@ -0,0 +1,33 @@ +--- +- debug: + msg: START vyos_ntp_global rendered integration tests on connection={{ + ansible_connection }} + +- include_tasks: _remove_config.yaml + +- block: + + - name: Structure provided configuration into device specific commands + register: result + vyos.vyos.vyos_ntp_global: &id001 + config: + servers: + - server: server5 + + - server: server4 + options: + - noselect + - dynamic + + - server: 10.3.6.5 + options: + - noselect + - preempt + - dynamic + - prefer + state: rendered + + - assert: + that: + - result.changed == false + - result.rendered|symmetric_difference(merged.commands) == [] diff --git a/tests/integration/targets/vyos_ntp_global/tests/cli/replaced.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/replaced.yaml new file mode 100644 index 00000000..8d1241b3 --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/tests/cli/replaced.yaml @@ -0,0 +1,42 @@ +--- +- debug: + msg: START vyos_ntp_global replaced integration tests on connection={{ + ansible_connection }} + +- include_tasks: _remove_config.yaml + +- include_tasks: _populate_config.yaml + +- block: + - name: Replace the provided configuration with the existing running configuration + register: result + vyos.vyos.vyos_ntp_global: &id001 + config: + servers: + - server: server_new + options: + - prefer + state: replaced + + - vyos.vyos.vyos_facts: + gather_network_resources: ntp_global + + - assert: + that: + - result.changed == true + - result.after == ansible_facts['network_resources']['ntp_global'] + - result.after == replaced.after + + - name: + Replace the provided configuration with the existing running configuration + (IDEMPOTENT) + register: result + vyos.vyos.vyos_ntp_global: *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_ntp_global/tests/cli/rtt.yaml b/tests/integration/targets/vyos_ntp_global/tests/cli/rtt.yaml new file mode 100644 index 00000000..86f75f42 --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/tests/cli/rtt.yaml @@ -0,0 +1,70 @@ +--- +- debug: + msg: START vyos_ntp_global rtt integration tests on connection={{ + ansible_connection }} + +- include_tasks: _remove_config.yaml + +- block: + - name: Merge the provided configuration with the existing running configuration + register: baseconfig + vyos.vyos.vyos_ntp_global: + config: + servers: + - server: 10.3.6.5 + options: + - noselect + - dynamic + - preempt + - prefer + + - server: server4 + options: + - noselect + - dynamic + + - server: 0.pool.ntp.org + + - server: 1.pool.ntp.org + + - server: 2.pool.ntp.org + + state: merged + + - vyos.vyos.vyos_facts: + gather_network_resources: ntp_global + + - assert: + that: + - baseconfig.commands|length == 6 + - baseconfig.changed == true + - baseconfig.commands|symmetric_difference(rtt.commands) == [] + - baseconfig.after == ansible_facts['network_resources']['ntp_global'] + + - name: Merge the existing configuration with the provided running configuration + register: result + vyos.vyos.vyos_ntp_global: + config: + servers: + - server: server1 + options: + - dynamic + - prefer + + - server: server2 + options: + - noselect + - preempt + + - name: Revert back to base config using facts round trip + register: revert + vyos.vyos.vyos_ntp_global: + config: "{{ ansible_facts['network_resources']['ntp_global'] }}" + state: overridden + + - name: Assert that config was reverted + assert: + that: baseconfig.after == revert.after + + always: + - include_tasks: _remove_config.yaml diff --git a/tests/integration/targets/vyos_ntp_global/vars/main.yaml b/tests/integration/targets/vyos_ntp_global/vars/main.yaml new file mode 100644 index 00000000..3b4dbc4d --- /dev/null +++ b/tests/integration/targets/vyos_ntp_global/vars/main.yaml @@ -0,0 +1,108 @@ +--- +rtt: + commands: + - set system ntp server server4 dynamic + - set system ntp server 10.3.6.5 noselect + - set system ntp server 10.3.6.5 dynamic + - set system ntp server 10.3.6.5 preempt + - set system ntp server server4 noselect + - set system ntp server 10.3.6.5 prefer + +merged: + commands: + - set system ntp server server5 + - set system ntp server server4 dynamic + - set system ntp server 10.3.6.5 noselect + - set system ntp server 10.3.6.5 dynamic + - set system ntp server 10.3.6.5 preempt + - set system ntp server server4 noselect + - set system ntp server 10.3.6.5 prefer + + after: + servers: + - server: 0.pool.ntp.org + - server: 1.pool.ntp.org + - server: 10.3.6.5 + options: + - dynamic + - noselect + - preempt + - prefer + - server: 2.pool.ntp.org + + - server: server4 + options: + - dynamic + - noselect + + - server: server5 + +replaced: + commands: + - delete system ntp server 10.3.6.5 + - delete system ntp server server4 + - delete system ntp server server5 + - set system ntp server server_new prefer + + after: + servers: + - server: 0.pool.ntp.org + - server: 1.pool.ntp.org + - server: 2.pool.ntp.org + - options: + - prefer + server: server_new + +overridden: + commands: + - delete system ntp server server_new + - 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 + + + after: + servers: + - server: 0.pool.ntp.org + - server: 1.pool.ntp.org + - server: 2.pool.ntp.org + - server: server1 + options: + - dynamic + - prefer + + - server: server2 + options: + - noselect + - preempt + - server: server_add + options: + - preempt + +deleted: + commands: + - delete system ntp server 10.3.6.5 + - delete system ntp server server4 + - delete system ntp server server5 + +parsed: + after: + servers: + - server: 0.pool.ntp.org + + - server: 1.pool.ntp.org + - server: 10.3.6.5 + options: + - dynamic + - noselect + - server: 2.pool.ntp.org + +populate: + config: + servers: + - server: 0.pool.ntp.org + + - server: 1.pool.ntp.org + - server: 2.pool.ntp.org diff --git a/tests/sanity/ignore-2.13.txt b/tests/sanity/ignore-2.13.txt index 496cf86a..c835eef8 100644 --- a/tests/sanity/ignore-2.13.txt +++ b/tests/sanity/ignore-2.13.txt @@ -1,12 +1 @@ plugins/action/vyos.py action-plugin-docs # base class for deprecated network platform modules using `connection: local` -plugins/module_utils/network/vyos/config/ospf_interfaces/ospf_interfaces.py compile-2.6!skip -plugins/module_utils/network/vyos/config/ospf_interfaces/ospf_interfaces.py import-2.6!skip -plugins/module_utils/network/vyos/config/route_maps/route_maps.py compile-2.6!skip -plugins/module_utils/network/vyos/config/route_maps/route_maps.py import-2.6!skip -plugins/modules/vyos_route_maps.py import-2.6!skip -plugins/modules/vyos_prefix_lists.py import-2.6!skip -plugins/module_utils/network/vyos/config/prefix_lists/prefix_lists.py import-2.6!skip -plugins/module_utils/network/vyos/config/prefix_lists/prefix_lists.py compile-2.6!skip -plugins/modules/vyos_logging_global.py import-2.6!skip -plugins/module_utils/network/vyos/config/logging_global/logging_global.py import-2.6!skip -plugins/module_utils/network/vyos/config/logging_global/logging_global.py compile-2.6!skip diff --git a/tests/unit/modules/network/vyos/fixtures/vyos_ntp_config.cfg b/tests/unit/modules/network/vyos/fixtures/vyos_ntp_config.cfg new file mode 100644 index 00000000..ec9bd7f4 --- /dev/null +++ b/tests/unit/modules/network/vyos/fixtures/vyos_ntp_config.cfg @@ -0,0 +1,10 @@ +set system ntp allow-clients address '10.1.1.0/24' +set system ntp allow-clients address '10.1.2.0/24' +set system ntp listen-address '10.2.3.1' +set system ntp listen-address '10.4.3.1' +set system ntp server server1 +set system ntp server server3 noselect +set system ntp server server3 dynamic +set system ntp server time1.vyos.net +set system ntp server time2.vyos.net +set system ntp server time3.vyos.net diff --git a/tests/unit/modules/network/vyos/test_vyos_ntp_global.py b/tests/unit/modules/network/vyos/test_vyos_ntp_global.py new file mode 100644 index 00000000..c27704f9 --- /dev/null +++ b/tests/unit/modules/network/vyos/test_vyos_ntp_global.py @@ -0,0 +1,364 @@ +# (c) 2021 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/>. + +# Make coding more python3-ish +from __future__ import absolute_import, division, print_function + +__metaclass__ = type + +from ansible_collections.vyos.vyos.tests.unit.compat.mock import patch +from ansible_collections.vyos.vyos.plugins.modules import vyos_ntp_global +from ansible_collections.vyos.vyos.tests.unit.modules.utils import ( + set_module_args, +) +from .vyos_module import TestVyosModule, load_fixture + + +class TestVyosNTPModule(TestVyosModule): + + module = vyos_ntp_global + + def setUp(self): + super(TestVyosNTPModule, 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_execute_show_command = patch( + "ansible_collections.vyos.vyos.plugins.module_utils.network.vyos.facts.ntp_global.ntp_global.Ntp_globalFacts.get_config" + ) + + self.execute_show_command = self.mock_execute_show_command.start() + + def tearDown(self): + super(TestVyosNTPModule, self).tearDown() + self.mock_get_resource_connection_config.stop() + self.mock_get_resource_connection_facts.stop() + self.mock_execute_show_command.stop() + + def load_fixtures(self, commands=None, transport="cli", filename=None): + if filename is None: + filename = "vyos_ntp_config.cfg" + + def load_from_file(*args, **kwargs): + output = load_fixture(filename) + return output + + self.execute_show_command.side_effect = load_from_file + + def test_ntp_merged_idempotent(self): + set_module_args( + dict( + config=dict( + allow_clients=["10.1.1.0/24", "10.1.2.0/24"], + listen_addresses=["10.2.3.1", "10.4.3.1"], + servers=[ + dict(server="server1"), + dict( + server="server3", options=["noselect", "dynamic"] + ), + dict(server="time1.vyos.net"), + dict(server="time2.vyos.net"), + dict(server="time3.vyos.net"), + ], + ), + state="merged", + ) + ) + self.execute_module(changed=False, commands=[]) + + def test_ntp_merged(self): + set_module_args( + dict( + config=dict( + allow_clients=["10.2.2.0/24", "10.3.3.0/24"], + listen_addresses=["10.3.4.1", "10.4.5.1"], + servers=[ + dict(server="server4", options=["dynamic", "preempt"]), + dict( + server="server5", + options=[ + "noselect", + "dynamic", + "preempt", + "prefer", + ], + ), + ], + ), + state="merged", + ) + ) + + commands = [ + "set system ntp allow-clients address 10.2.2.0/24", + "set system ntp allow-clients address 10.3.3.0/24", + "set system ntp listen-address 10.3.4.1", + "set system ntp listen-address 10.4.5.1", + "set system ntp server server4 dynamic", + "set system ntp server server4 preempt", + "set system ntp server server5 dynamic", + "set system ntp server server5 noselect", + "set system ntp server server5 preempt", + "set system ntp server server5 prefer", + ] + + self.execute_module(changed=True, commands=commands) + + def test_ntp_replaced(self): + set_module_args( + dict( + config=dict( + allow_clients=["10.3.4.0/24", "10.4.5.0/24"], + listen_addresses=["10.3.3.1", "10.4.4.1"], + servers=[ + dict(server="server4", options=["noselect", "prefer"]), + dict( + server="server6", + options=[ + "noselect", + "dynamic", + "prefer", + "preempt", + ], + ), + dict(server="time1.vyos.net"), + dict(server="time2.vyos.net"), + dict(server="time3.vyos.net"), + ], + ), + state="replaced", + ) + ) + commands = [ + "delete system ntp allow-clients address 10.1.1.0/24", + "delete system ntp allow-clients address 10.1.2.0/24", + "delete system ntp listen-address 10.2.3.1", + "delete system ntp listen-address 10.4.3.1", + "delete system ntp server server1", + "delete system ntp server server3", + "set system ntp allow-clients address 10.3.4.0/24", + "set system ntp allow-clients address 10.4.5.0/24", + "set system ntp listen-address 10.3.3.1", + "set system ntp listen-address 10.4.4.1", + "set system ntp server server4 noselect", + "set system ntp server server4 prefer", + "set system ntp server server6 noselect", + "set system ntp server server6 dynamic", + "set system ntp server server6 prefer", + "set system ntp server server6 preempt", + ] + self.execute_module(changed=True, commands=commands) + + def test_ntp_replaced_idempotent(self): + set_module_args( + dict( + config=dict( + allow_clients=["10.1.1.0/24", "10.1.2.0/24"], + listen_addresses=["10.2.3.1", "10.4.3.1"], + servers=[ + dict(server="server1"), + dict( + server="server3", options=["noselect", "dynamic"] + ), + dict(server="time1.vyos.net"), + dict(server="time2.vyos.net"), + dict(server="time3.vyos.net"), + ], + ), + state="replaced", + ) + ) + self.execute_module(changed=False, commands=[]) + + def test_ntp_overridden(self): + set_module_args( + dict( + config=dict( + allow_clients=["10.9.9.0/24"], + listen_addresses=["10.9.9.1"], + servers=[ + dict(server="server9"), + dict( + server="server6", options=["noselect", "dynamic"] + ), + dict(server="time1.vyos.net"), + dict(server="time2.vyos.net"), + dict(server="time3.vyos.net"), + ], + ), + state="overridden", + ) + ) + commands = [ + "delete system ntp allow-clients address 10.1.1.0/24", + "delete system ntp allow-clients address 10.1.2.0/24", + "delete system ntp listen-address 10.2.3.1", + "delete system ntp listen-address 10.4.3.1", + "delete system ntp server server1", + "delete system ntp server server3", + "set system ntp allow-clients address 10.9.9.0/24", + "set system ntp listen-address 10.9.9.1", + "set system ntp server server9", + "set system ntp server server6 noselect", + "set system ntp server server6 dynamic", + ] + self.execute_module(changed=True, commands=commands) + + def test_ntp_overridden_idempotent(self): + set_module_args( + dict( + config=dict( + allow_clients=["10.1.1.0/24", "10.1.2.0/24"], + listen_addresses=["10.2.3.1", "10.4.3.1"], + servers=[ + dict(server="server1"), + dict( + server="server3", options=["noselect", "dynamic"] + ), + dict(server="time1.vyos.net"), + dict(server="time2.vyos.net"), + dict(server="time3.vyos.net"), + ], + ), + state="overridden", + ) + ) + self.execute_module(changed=False, commands=[]) + + def test_ntp_rendered(self): + set_module_args( + dict( + config=dict( + allow_clients=["10.7.7.0/24", "10.8.8.0/24"], + listen_addresses=["10.7.9.1"], + servers=[ + dict(server="server79"), + dict( + server="server46", options=["noselect", "dynamic"] + ), + dict(server="time1.vyos.net"), + dict(server="time2.vyos.net"), + dict(server="time3.vyos.net"), + ], + ), + state="rendered", + ) + ) + rendered_commands = [ + "set system ntp allow-clients address 10.7.7.0/24", + "set system ntp allow-clients address 10.8.8.0/24", + "set system ntp listen-address 10.7.9.1", + "set system ntp server server79", + "set system ntp server server46 noselect", + "set system ntp server server46 dynamic", + "set system ntp server time1.vyos.net", + "set system ntp server time2.vyos.net", + "set system ntp server time3.vyos.net", + ] + result = self.execute_module(changed=False) + self.assertEqual( + sorted(result["rendered"]), + sorted(rendered_commands), + result["rendered"], + ) + + def test_ntp_parsed(self): + + commands = ( + "set system ntp allow-clients address 10.7.7.0/24", + "set system ntp allow-clients address 10.6.7.0/24", + "set system ntp listen-address 10.7.9.1", + "set system ntp listen-address 10.7.7.1", + "set system ntp server check", + "set system ntp server server46 noselect", + "set system ntp server server46 prefer", + "set system ntp server time1.vyos.net", + "set system ntp server time2.vyos.net", + "set system ntp server time3.vyos.net", + ) + parsed_str = "\n".join(commands) + set_module_args(dict(running_config=parsed_str, state="parsed")) + result = self.execute_module(changed=False) + parsed_list = { + "allow_clients": ["10.6.7.0/24", "10.7.7.0/24"], + "listen_addresses": ["10.7.7.1", "10.7.9.1"], + "servers": [ + {"server": "check"}, + {"server": "server46", "options": ["noselect", "prefer"]}, + {"server": "time1.vyos.net"}, + {"server": "time2.vyos.net"}, + {"server": "time3.vyos.net"}, + ], + } + self.assertEqual(parsed_list, result["parsed"]) + + def test_ntp_gathered(self): + set_module_args(dict(state="gathered")) + result = self.execute_module(changed=False) + gathered_list = { + "allow_clients": ["10.1.1.0/24", "10.1.2.0/24"], + "listen_addresses": ["10.2.3.1", "10.4.3.1"], + "servers": [ + {"server": "server1"}, + {"server": "server3", "options": ["dynamic", "noselect"]}, + {"server": "time1.vyos.net"}, + {"server": "time2.vyos.net"}, + {"server": "time3.vyos.net"}, + ], + } + + self.assertEqual(gathered_list, result["gathered"]) + + def test_ntp_deleted(self): + set_module_args( + dict( + config=dict( + allow_clients=["10.1.1.0/24"], + listen_addresses=["10.2.3.1"], + servers=[ + dict(server="server1"), + dict(server="server3", options=["noselect"]), + dict(server="time1.vyos.net"), + dict(server="time2.vyos.net"), + dict(server="time3.vyos.net"), + ], + ), + state="deleted", + ) + ) + commands = [ + "delete system ntp allow-clients", + "delete system ntp listen-address", + "delete system ntp server server1", + "delete system ntp server server3", + "delete system ntp server time1.vyos.net", + "delete system ntp server time2.vyos.net", + "delete system ntp server time3.vyos.net", + ] + self.execute_module(changed=True, commands=commands) |