summaryrefslogtreecommitdiff
path: root/plugins/modules/vyos_system.py.preview
blob: d1d696e0e32b7b7de78daa929e87601985da3085 (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
146
147
#!/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 via the REST API.
description:
  - Manages system-level settings — hostname, domain name, name servers,
    domain search list — using the VyOS HTTPS REST API.
  - Mirrors C(vyos.vyos.vyos_system) but uses the HTTP API.
version_added: "1.0.0"
author:
  - VyOS Community (@vyos)
options:
  host_name:
    description: System hostname.
    type: str
  domain_name:
    description: System domain name.
    type: str
  name_server:
    description: List of DNS name servers. Mutually exclusive with domain_search.
    type: list
    elements: str
    aliases: [name_servers]
  domain_search:
    description: List of domain search suffixes. Mutually exclusive with name_server.
    type: list
    elements: str
  state:
    description: C(present) to apply, C(absent) to remove.
    type: str
    choices: [present, absent]
    default: present
  hostname:
    type: str
    required: true
  port:
    type: int
    default: 443
  api_key:
    type: str
    required: true
    no_log: true
  timeout:
    type: int
    default: 30
  verify_ssl:
    type: bool
    default: false
"""

RETURN = r"""
commands:
  description: set/delete commands issued.
  returned: always
  type: list
"""

from ansible.module_utils.basic import AnsibleModule
from ansible_collections.vyos.rest.plugins.module_utils.vyos_rest import (
    VYOS_REST_CONNECTION_ARGSPEC,
    VyOSRestClient,
    VyOSRestError,
)


def main():
    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"]),
    )
    argument_spec.update(VYOS_REST_CONNECTION_ARGSPEC)
    module = AnsibleModule(
        argument_spec=argument_spec,
        mutually_exclusive=[["name_server", "domain_search"]],
        supports_check_mode=True,
    )

    if module.check_mode:
        module.exit_json(changed=True, commands=["(check mode)"])

    client = VyOSRestClient(module)
    state = module.params["state"]
    commands = []
    changed = False

    try:
        if state == "present":
            if module.params.get("host_name"):
                client.configure_set(["system", "host-name"], module.params["host_name"])
                commands.append("set system host-name '{h}'".format(h=module.params["host_name"]))
                changed = True
            if module.params.get("domain_name"):
                client.configure_set(["system", "domain-name"], module.params["domain_name"])
                commands.append(
                    "set system domain-name '{d}'".format(d=module.params["domain_name"]),
                )
                changed = True
            for ns in module.params.get("name_server") or []:
                client.configure_set(["system", "name-server"], ns)
                commands.append("set system name-server {ns}".format(ns=ns))
                changed = True
            for ds in module.params.get("domain_search") or []:
                client.configure_set(["system", "domain-search", "domain"], ds)
                commands.append("set system domain-search domain {ds}".format(ds=ds))
                changed = True
        else:
            if module.params.get("host_name"):
                try:
                    client.configure_delete(["system", "host-name"])
                    commands.append("delete system host-name")
                    changed = True
                except VyOSRestError:
                    pass
            if module.params.get("domain_name"):
                try:
                    client.configure_delete(["system", "domain-name"])
                    commands.append("delete system domain-name")
                    changed = True
                except VyOSRestError:
                    pass
            if module.params.get("name_server"):
                try:
                    client.configure_delete(["system", "name-server"])
                    commands.append("delete system name-server")
                    changed = True
                except VyOSRestError:
                    pass
    except VyOSRestError as exc:
        module.fail_json(msg=str(exc))

    module.exit_json(changed=changed, commands=commands)


if __name__ == "__main__":
    main()