summaryrefslogtreecommitdiff
path: root/plugins/modules/vyos_configure.py
blob: 02fc7ad3539abf2625c3012ce9f2542db33fc53c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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()