summaryrefslogtreecommitdiff
path: root/plugins/modules
diff options
context:
space:
mode:
authoromnom62 <omnom62@outlook.com>2026-03-19 07:35:55 +1000
committeromnom62 <omnom62@outlook.com>2026-03-19 07:35:55 +1000
commit9dc03bb92e08455812e0969505495309456a4bfb (patch)
treec8299446d9f17d3caa544c844471a1f27b799965 /plugins/modules
parentde3d09eaa33d3553e656362ee398d0848c2fad01 (diff)
downloadrest.vyos-9dc03bb92e08455812e0969505495309456a4bfb.tar.gz
rest.vyos-9dc03bb92e08455812e0969505495309456a4bfb.zip
Fixed vyos_hostname
Diffstat (limited to 'plugins/modules')
-rw-r--r--plugins/modules/vyos_hostname.py120
1 files changed, 120 insertions, 0 deletions
diff --git a/plugins/modules/vyos_hostname.py b/plugins/modules/vyos_hostname.py
new file mode 100644
index 0000000..f1c901e
--- /dev/null
+++ b/plugins/modules/vyos_hostname.py
@@ -0,0 +1,120 @@
+#!/usr/bin/python
+
+from ansible.module_utils.basic import AnsibleModule
+from ansible_collections.vyos.rest.plugins.module_utils.vyos import VyOSModule
+
+
+def get_running_config(vyos):
+
+ raw = vyos.get_config(["system", "host-name"])
+
+ hostname = None
+
+ if isinstance(raw, dict):
+ hostname = raw.get("host-name")
+
+ elif isinstance(raw, str):
+ hostname = raw.strip()
+ else:
+ hostname = None
+
+ return {"hostname": hostname}
+
+def build_commands(want, have, state):
+
+ commands = []
+
+ want_host = want.get("hostname")
+ have_host = have.get("hostname")
+
+ if state in ["merged", "replaced", "overridden"]:
+
+ if want_host and want_host != have_host:
+ commands.append({
+ "op": "set",
+ "path": ["system", "host-name", want_host]
+ })
+
+ elif state == "deleted":
+
+ if have_host:
+ commands.append({
+ "op": "delete",
+ "path": ["system", "host-name"]
+ })
+
+ return commands
+
+
+def main():
+
+ argument_spec = dict(
+ config=dict(
+ type="dict",
+ options=dict(
+ hostname=dict(type="str")
+ )
+ ),
+ state=dict(
+ default="merged",
+ choices=[
+ "merged",
+ "replaced",
+ "overridden",
+ "deleted",
+ "gathered",
+ ],
+ ),
+ )
+
+ module = AnsibleModule(
+ argument_spec=argument_spec,
+ supports_check_mode=True,
+ )
+
+ vyos = VyOSModule(module)
+
+ state = module.params["state"]
+ want = module.params.get("config") or {}
+
+ have = get_running_config(vyos)
+
+ if state == "gathered":
+ module.exit_json(
+ changed=False,
+ gathered=have
+ )
+
+ commands = build_commands(want, have, state)
+
+ if module.check_mode:
+ module.exit_json(
+ changed=bool(commands),
+ commands=commands
+ )
+
+ if commands:
+
+ response = vyos.apply_commands(commands)
+
+ # save config if change applied
+ saved = vyos.save_config()
+
+ module.exit_json(
+ changed=True,
+ before=have,
+ after=want,
+ commands=commands,
+ saved=saved,
+ response=response
+ )
+
+ module.exit_json(
+ changed=False,
+ before=have,
+ after=have
+ )
+
+
+if __name__ == "__main__":
+ main() \ No newline at end of file