diff options
Diffstat (limited to 'plugins')
| -rw-r--r-- | plugins/httpapi/vyos.py | 13 | ||||
| -rw-r--r-- | plugins/modules/vyos_hostname.py | 120 |
2 files changed, 131 insertions, 2 deletions
diff --git a/plugins/httpapi/vyos.py b/plugins/httpapi/vyos.py index 049ef97..83a753f 100644 --- a/plugins/httpapi/vyos.py +++ b/plugins/httpapi/vyos.py @@ -12,24 +12,33 @@ description: options: api_key: description: VyOS API key - required: true type: str vars: - name: ansible_httpapi_api_key env: - name: ANSIBLE_HTTPAPI_API_KEY + ini: + - section: httpapi + key: api_key ''' class HttpApi(HttpApiBase): def set_options(self, task_keys=None, var_options=None, direct=None): - super().set_options(task_keys=task_keys, var_options=var_options, direct=direct) + super().set_options(task_keys=task_keys, var_options=var_options, direct=direct) def send_request(self, path, data=None, method="POST", headers=None): api_key = self.get_option("api_key") + if not api_key: + # fallback to inventory vars + api_key = self._play_context.vars.get("ansible_httpapi_api_key") + + if not api_key: + raise ValueError("api_key is required") + form_data = {} if data is not None: 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 |
