summaryrefslogtreecommitdiff
path: root/plugins/modules/vyos_system.py
blob: 474a804d026e30dcde56c4911749a1f6ad272c00 (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
138
139
140
141
142
143
144
145
#!/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_system
short_description: Manage system settings on VyOS devices using REST API
description:
  - Manages basic system settings on VyOS devices via the REST API.
  - Uses REST API (C(connection=httpapi)) instead of CLI.
version_added: "1.0.0"
author:
  - VyOS Community (@vyos)
options:
  host_name:
    description: Device hostname.
    type: str
  domain_name:
    description: Device domain name.
    type: str
  name_server:
    description: List of DNS name servers.
    type: list
    elements: str
    aliases: [name_servers]
  domain_search:
    description: List of domain search suffixes.
    type: list
    elements: str
  state:
    description:
      - C(present) applies the configuration.
      - C(absent) removes the configuration.
    type: str
    choices: [present, absent]
    default: present
notes:
  - Requires C(ansible_connection=httpapi) with the VyOS httpapi plugin.
  - C(ansible_network_os) must be set to C(vyos.rest.vyos).
"""

EXAMPLES = r"""
- name: Configure hostname and domain
  vyos.rest.vyos_system:
    host_name: router1
    domain_name: example.com
    name_server:
      - 8.8.8.8
      - 8.8.4.4
    state: present

- name: Remove domain name and name servers
  vyos.rest.vyos_system:
    domain_name: example.com
    name_server:
      - 8.8.8.8
    state: absent
"""

RETURN = r"""
before:
  description: Module-owned system configuration before this module ran.
  returned: always
  type: dict
after:
  description: Module-owned system configuration after this module ran.
  returned: when changed
  type: dict
commands:
  description: List of API command tuples sent to the device.
  returned: always
  type: list
saved:
  description: Whether the config was saved after changes.
  returned: when changes are applied
  type: bool
response:
  description: Raw API response.
  returned: when changes are applied
  type: dict
"""

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.vyos.rest.plugins.module_utils.vyos import (
    VyOSModule,
    dict_op,
    owned_config,
)


_BASE = ["system"]

ARGUMENT_SPEC = dict(
    host_name=dict(type="str"),
    domain_name=dict(type="str"),
    name_server=dict(type="list", elements="str", aliases=["name_servers"]),
    domain_search=dict(type="list", elements="str"),
    state=dict(type="str", default="present", choices=["present", "absent"]),
)


def main():
    module = AnsibleModule(ARGUMENT_SPEC, supports_check_mode=True)
    vyos = VyOSModule(module)

    state = module.params["state"]

    # want: snake_case keys from YAML, nulls removed
    want = {k: v for k, v in module.params.items() if k != "state" and v is not None}

    # have: raw kebab-case keys from device, scoped by _BASE
    have = vyos.get_config(_BASE)

    # before/after: only keys owned by this module (declared in argspec)
    before = owned_config(have, ARGUMENT_SPEC)

    op = "set" if state == "present" else "delete"
    commands = dict_op(want, have, _BASE, op=op)

    if module.check_mode:
        module.exit_json(changed=bool(commands), commands=commands, before=before)

    if commands:
        response = vyos.apply_commands(commands)
        saved = vyos.save_config()
        after = owned_config(vyos.get_config(_BASE), ARGUMENT_SPEC)
        module.exit_json(
            changed=True,
            before=before,
            after=after,
            commands=commands,
            saved=saved,
            response=response,
        )

    module.exit_json(changed=False, before=before, after=before, commands=[])


if __name__ == "__main__":
    main()