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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
#!/usr/bin/python
# -*- coding: utf-8 -*-
# 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
DOCUMENTATION = r"""
---
module: vyos_hostname
short_description: Manage the system hostname on a VyOS device via the REST API.
description:
- Manages the C(set system host-name) configuration on a VyOS device
using the HTTPS REST API.
- Mirrors the behaviour of C(vyos.vyos.vyos_hostname) but uses the HTTP
API instead of SSH/network_cli.
- The states C(replaced), C(overridden) behave identically to C(merged)
for this single-value resource.
version_added: "1.0.0"
author:
- VyOS Community (@vyos)
options:
config:
description:
- Hostname configuration.
type: dict
suboptions:
hostname:
description:
- System hostname (max 63 characters, no underscores).
type: str
required: true
running_config:
description:
- Used only with state C(parsed).
- The value should be the output of
B(show configuration commands | grep host-name) from the device.
type: str
state:
description:
- C(merged) - Ensure the hostname is set to the value in I(config).
- C(replaced) - Identical to C(merged) for this single-value resource.
- C(overridden) - Identical to C(merged) for this single-value resource.
- C(deleted) - Remove the configured hostname (resets to default).
- C(gathered) - Read the current hostname from the device and return it
in I(gathered) without making changes.
- C(rendered) - Return the CLI commands for the given config without
connecting to the device.
- C(parsed) - Parse the C(running_config) string and return structured
data without connecting to the device.
type: str
choices:
- merged
- replaced
- overridden
- deleted
- gathered
- rendered
- parsed
default: merged
hostname:
description:
- IP address or FQDN of the VyOS device (not needed with httpapi inventory).
type: str
port:
description:
- HTTPS port for the REST API.
type: int
default: 443
api_key:
description:
- API key configured on the device.
type: str
timeout:
description:
- Request timeout in seconds.
type: int
default: 30
verify_ssl:
description:
- Validate the device's TLS certificate.
type: bool
default: false
requirements:
- VyOS 1.3+
seealso:
- module: vyos.vyos.vyos_hostname
"""
RETURN = r"""
before:
description: Configuration on the device before the module ran.
returned: always
type: dict
after:
description: Configuration on the device after the module ran.
returned: when changed
type: dict
gathered:
description: Hostname read from the device (state=gathered only).
returned: when state is gathered
type: dict
rendered:
description: CLI commands for the provided config (state=rendered only).
returned: when state is rendered
type: list
parsed:
description: Structured data parsed from running_config (state=parsed only).
returned: when state is parsed
type: dict
commands:
description: REST API commands dispatched.
returned: always
type: list
"""
EXAMPLES = r"""
- name: Set hostname
vyos.rest.vyos_hostname:
config:
hostname: vyos-core-01
state: merged
- name: Replace hostname
vyos.rest.vyos_hostname:
config:
hostname: vyos-core-02
state: replaced
- name: Gather current hostname
vyos.rest.vyos_hostname:
state: gathered
register: result
- name: Delete hostname configuration
vyos.rest.vyos_hostname:
state: deleted
- name: Render commands without connecting
vyos.rest.vyos_hostname:
config:
hostname: vyos-core-01
state: rendered
- name: Parse running config
vyos.rest.vyos_hostname:
running_config: "set system host-name 'vyos'"
state: parsed
"""
import re
from ansible.module_utils.basic import AnsibleModule
from ansible_collections.vyos.rest.plugins.module_utils.vyos_rest import (
VYOS_REST_CONNECTION_ARGSPEC,
VyOSRestClient,
VyOSRestError,
)
_PATH = ["system", "host-name"]
def _get_hostname(client):
try:
result = client.retrieve_return_value(_PATH)
return result.get("data", "")
except VyOSRestError:
return ""
def _parse_hostname(running_config):
"""Parse hostname from 'show configuration commands | grep host-name' output."""
match = re.search(r"host-name\s+['\"]?(\S+?)['\"]?\s*$", running_config, re.M)
return match.group(1) if match else ""
def main():
argument_spec = dict(
config=dict(
type="dict",
options=dict(
hostname=dict(type="str", required=True),
),
),
running_config=dict(type="str"),
state=dict(
type="str",
default="merged",
choices=[
"merged",
"replaced",
"overridden",
"deleted",
"gathered",
"rendered",
"parsed",
],
),
)
argument_spec.update(VYOS_REST_CONNECTION_ARGSPEC)
module = AnsibleModule(
argument_spec=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,
)
state = module.params["state"]
# rendered — offline, no device connection needed
if state == "rendered":
hostname = module.params["config"]["hostname"]
module.exit_json(
rendered=["set system host-name '{h}'".format(h=hostname)],
commands=[],
)
# parsed — offline, no device connection needed
if state == "parsed":
hostname = _parse_hostname(module.params["running_config"] or "")
module.exit_json(
parsed={"hostname": hostname},
commands=[],
)
# collapsed states — replaced and overridden are identical to merged
if state in ("replaced", "overridden"):
state = "merged"
client = VyOSRestClient(module)
commands = []
changed = False
current = _get_hostname(client)
before = {"hostname": current}
if state == "gathered":
module.exit_json(changed=False, gathered=before, before=before, commands=[])
if module.check_mode:
module.exit_json(changed=True, before=before, commands=["(check mode)"])
try:
if state == "merged":
desired = module.params["config"]["hostname"]
if current != desired:
client.configure_set(_PATH, desired)
commands.append("set system host-name '{h}'".format(h=desired))
changed = True
elif state == "deleted":
if current:
client.configure_delete(_PATH)
commands.append("delete system host-name")
changed = True
except VyOSRestError as exc:
module.fail_json(msg=str(exc))
after = {"hostname": _get_hostname(client)} if changed else before
module.exit_json(changed=changed, before=before, after=after, commands=commands)
if __name__ == "__main__":
main()
|