diff options
11 files changed, 151 insertions, 7 deletions
@@ -12,6 +12,7 @@ Name | Description Name | Description --- | --- [vyos.rest.vyos_banner](https://github.com/vyos/vyos.rest/blob/main/docs/vyos.rest.vyos_banner_module.rst)|Manage multiline banners on VyOS devices via REST API. +[vyos.rest.vyos_configure](https://github.com/vyos/vyos.rest/blob/main/docs/vyos.rest.vyos_configure_module.rst)|Send raw set/delete commands to a VyOS device via REST API. [vyos.rest.vyos_hostname](https://github.com/vyos/vyos.rest/blob/main/docs/vyos.rest.vyos_hostname_module.rst)|Manage the system hostname on a VyOS device via the REST API. [vyos.rest.vyos_lldp_global](https://github.com/vyos/vyos.rest/blob/main/docs/vyos.rest.vyos_lldp_global_module.rst)|Manage LLDP global configuration on VyOS via REST API. [vyos.rest.vyos_logging_global](https://github.com/vyos/vyos.rest/blob/main/docs/vyos.rest.vyos_logging_global_module.rst)|Manage syslog configuration on VyOS devices using REST API diff --git a/plugins/modules/vyos_configure.py b/plugins/modules/vyos_configure.py new file mode 100644 index 0000000..02fc7ad --- /dev/null +++ b/plugins/modules/vyos_configure.py @@ -0,0 +1,137 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +# GNU General Public License v3.0+ + +from __future__ import absolute_import, division, print_function + + +__metaclass__ = type + +DOCUMENTATION = r""" +--- +module: vyos_configure +short_description: Send raw set/delete commands to a VyOS device via REST API. +description: + - Sends one or more set/delete configuration commands to a VyOS device + via the HTTPS REST API as a single atomic batch commit. + - Useful for configuration not covered by dedicated resource modules, + or for test setup and teardown tasks. + - Commands are parsed from CLI-style strings (C(set ...) / C(delete ...)). +version_added: "1.0.0" +author: + - VyOS Community (@vyos) +options: + commands: + description: + - List of CLI-style configuration commands. + - Each command must start with C(set) or C(delete). + type: list + elements: str + required: true + save: + description: + - Whether to save the configuration after applying commands. + type: bool + default: false +seealso: + - module: vyos.vyos.vyos_config +""" + +EXAMPLES = r""" +- name: Add loopback address for testing + vyos.rest.vyos_configure: + commands: + - set interfaces loopback lo address 20.1.1.1/32 + save: false + +- name: Remove loopback address after testing + vyos.rest.vyos_configure: + commands: + - delete interfaces loopback lo address 20.1.1.1/32 + save: false + +- name: Multiple commands in one atomic commit + vyos.rest.vyos_configure: + commands: + - set system host-name vyos-test + - set system domain-name example.com + save: true +""" + +RETURN = r""" +commands: + description: Parsed command payloads sent to the device. + returned: always + type: list +response: + description: Raw API response. + returned: when commands are applied + type: dict +""" + +from ansible.module_utils.basic import AnsibleModule +from ansible_collections.vyos.rest.plugins.module_utils.vyos import VyOSModule + + +def _parse_command(line): + """Parse a CLI-style command string into (op, path) tuple. + + Examples: + "set interfaces loopback lo address 20.1.1.1/32" + -> ("set", ["interfaces", "loopback", "lo", "address", "20.1.1.1/32"]) + "delete service snmp" + -> ("delete", ["service", "snmp"]) + """ + line = line.strip() + if line.startswith("set "): + parts = line[4:].split() + return ("set", parts) + elif line.startswith("delete "): + parts = line[7:].split() + return ("delete", parts) + else: + return None + + +def main(): + module = AnsibleModule( + argument_spec=dict( + commands=dict(type="list", elements="str", required=True), + save=dict(type="bool", default=False), + ), + supports_check_mode=True, + ) + + vyos = VyOSModule(module) + commands_raw = module.params["commands"] + do_save = module.params["save"] + + commands = [] + for line in commands_raw: + parsed = _parse_command(line) + if parsed is None: + module.fail_json( + msg="Invalid command '{c}' — must start with 'set' or 'delete'".format(c=line), + ) + commands.append(parsed) + + if not commands: + module.exit_json(changed=False, commands=[]) + + if module.check_mode: + module.exit_json(changed=True, commands=commands) + + response = vyos.apply_commands(commands) + + if do_save: + vyos.save_config() + + module.exit_json( + changed=True, + commands=commands, + response=response, + ) + + +if __name__ == "__main__": + main() diff --git a/tests/integration/targets/prepare_vyos_tests/meta/main.yaml b/tests/integration/targets/prepare_vyos_tests/meta/main.yaml new file mode 100644 index 0000000..61d3ffe --- /dev/null +++ b/tests/integration/targets/prepare_vyos_tests/meta/main.yaml @@ -0,0 +1,2 @@ +--- +allow_duplicates: true diff --git a/tests/integration/targets/prepare_vyos_tests/tasks/main.yaml b/tests/integration/targets/prepare_vyos_tests/tasks/main.yaml new file mode 100644 index 0000000..9527685 --- /dev/null +++ b/tests/integration/targets/prepare_vyos_tests/tasks/main.yaml @@ -0,0 +1,5 @@ +--- +- name: Add dummy loopback address for SNMP listen test + vyos.rest.vyos_configure: + commands: + - set interfaces loopback lo address 20.1.1.1/32 diff --git a/tests/integration/targets/vyos_snmp_server/meta/main.yaml b/tests/integration/targets/vyos_snmp_server/meta/main.yaml new file mode 100644 index 0000000..7413320 --- /dev/null +++ b/tests/integration/targets/vyos_snmp_server/meta/main.yaml @@ -0,0 +1,3 @@ +--- +dependencies: + - prepare_vyos_tests diff --git a/tests/integration/targets/vyos_snmp_server/tests/httpapi/_populate_config.yaml b/tests/integration/targets/vyos_snmp_server/tests/httpapi/_populate_config.yaml index c847765..d3762c1 100644 --- a/tests/integration/targets/vyos_snmp_server/tests/httpapi/_populate_config.yaml +++ b/tests/integration/targets/vyos_snmp_server/tests/httpapi/_populate_config.yaml @@ -1,4 +1,6 @@ --- +- ansible.builtin.include_tasks: _remove_config.yaml + - name: Populate snmp_server config for testing vyos.rest.vyos_snmp_server: config: @@ -12,4 +14,3 @@ listen_addresses: - address: 20.1.1.1 state: merged - ignore_errors: true diff --git a/tests/integration/targets/vyos_snmp_server/tests/httpapi/_remove_config.yaml b/tests/integration/targets/vyos_snmp_server/tests/httpapi/_remove_config.yaml index c77493a..e954df7 100644 --- a/tests/integration/targets/vyos_snmp_server/tests/httpapi/_remove_config.yaml +++ b/tests/integration/targets/vyos_snmp_server/tests/httpapi/_remove_config.yaml @@ -1,5 +1,5 @@ +# _remove_config.yaml --- - name: Remove pre-existing snmp_server config vyos.rest.vyos_snmp_server: state: deleted - ignore_errors: true diff --git a/tests/integration/targets/vyos_snmp_server/tests/httpapi/deleted.yaml b/tests/integration/targets/vyos_snmp_server/tests/httpapi/deleted.yaml index 90ee0ae..a626a26 100644 --- a/tests/integration/targets/vyos_snmp_server/tests/httpapi/deleted.yaml +++ b/tests/integration/targets/vyos_snmp_server/tests/httpapi/deleted.yaml @@ -2,7 +2,6 @@ - debug: msg: START vyos_snmp_server deleted integration tests on connection={{ ansible_connection }} -- include_tasks: _remove_config.yaml - include_tasks: _populate_config.yaml - block: diff --git a/tests/integration/targets/vyos_snmp_server/tests/httpapi/gathered.yaml b/tests/integration/targets/vyos_snmp_server/tests/httpapi/gathered.yaml index 740446f..770845c 100644 --- a/tests/integration/targets/vyos_snmp_server/tests/httpapi/gathered.yaml +++ b/tests/integration/targets/vyos_snmp_server/tests/httpapi/gathered.yaml @@ -2,7 +2,6 @@ - debug: msg: START vyos_snmp_server gathered integration tests on connection={{ ansible_connection }} -- include_tasks: _remove_config.yaml - include_tasks: _populate_config.yaml - block: diff --git a/tests/integration/targets/vyos_snmp_server/tests/httpapi/merged.yaml b/tests/integration/targets/vyos_snmp_server/tests/httpapi/merged.yaml index 475b5fe..e1c6531 100644 --- a/tests/integration/targets/vyos_snmp_server/tests/httpapi/merged.yaml +++ b/tests/integration/targets/vyos_snmp_server/tests/httpapi/merged.yaml @@ -2,8 +2,6 @@ - debug: msg: START vyos_snmp_server merged integration tests on connection={{ ansible_connection }} -- include_tasks: _remove_config.yaml - - block: - name: Merge snmp_server configuration register: result diff --git a/tests/integration/targets/vyos_snmp_server/tests/httpapi/replaced.yaml b/tests/integration/targets/vyos_snmp_server/tests/httpapi/replaced.yaml index cd9bef2..d989272 100644 --- a/tests/integration/targets/vyos_snmp_server/tests/httpapi/replaced.yaml +++ b/tests/integration/targets/vyos_snmp_server/tests/httpapi/replaced.yaml @@ -2,7 +2,6 @@ - debug: msg: START vyos_snmp_server replaced integration tests on connection={{ ansible_connection }} -- include_tasks: _remove_config.yaml - include_tasks: _populate_config.yaml - block: |
