Debian Codegraph @

ansible_collections/community/general/plugins/modules/cobbler_sync.py

raw
1 #!/usr/bin/python
2
3 # Copyright (c) 2018, Dag Wieers (dagwieers) <[email protected]>
4 # GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7 from __future__ import annotations
8
9 DOCUMENTATION = r"""
10 module: cobbler_sync
11 short_description: Sync Cobbler
12 description:
13 - Sync Cobbler to commit changes.
14 extends_documentation_fragment:
15 - community.general._attributes
16 attributes:
17 check_mode:
18 support: full
19 diff_mode:
20 support: none
21 options:
22 host:
23 description:
24 - The name or IP address of the Cobbler system.
25 default: 127.0.0.1
26 type: str
27 port:
28 description:
29 - Port number to be used for REST connection.
30 - The default value depends on parameter O(use_ssl).
31 type: int
32 username:
33 description:
34 - The username to log in to Cobbler.
35 default: cobbler
36 type: str
37 password:
38 description:
39 - The password to log in to Cobbler.
40 type: str
41 use_ssl:
42 description:
43 - If V(false), an HTTP connection is used instead of the default HTTPS connection.
44 type: bool
45 default: true
46 validate_certs:
47 description:
48 - If V(false), SSL certificates are not validated.
49 - This should only set to V(false) when used on personally controlled sites using self-signed certificates.
50 type: bool
51 default: true
52 author:
53 - Dag Wieers (@dagwieers)
54 todo:
55 notes:
56 - Concurrently syncing Cobbler is bound to fail with weird errors.
57 """
58
59 EXAMPLES = r"""
60 - name: Commit Cobbler changes
61 community.general.cobbler_sync:
62 host: cobbler01
63 username: cobbler
64 password: MySuperSecureP4sswOrd
65 run_once: true
66 delegate_to: localhost
67 """
68
69 RETURN = r"""
70 # Default return values
71 """
72
73 import ssl
74 import xmlrpc.client as xmlrpc_client
75
76 from ansible.module_utils.basic import AnsibleModule
77
78 from ansible_collections.community.general.plugins.module_utils._datetime import (
79 now,
80 )
81
82
83 def main():
84 module = AnsibleModule(
85 argument_spec=dict(
86 host=dict(type="str", default="127.0.0.1"),
87 port=dict(type="int"),
88 username=dict(type="str", default="cobbler"),
89 password=dict(type="str", no_log=True),
90 use_ssl=dict(type="bool", default=True),
91 validate_certs=dict(type="bool", default=True),
92 ),
93 supports_check_mode=True,
94 )
95
96 username = module.params["username"]
97 password = module.params["password"]
98 port = module.params["port"]
99 use_ssl = module.params["use_ssl"]
100 validate_certs = module.params["validate_certs"]
101
102 proto = "https" if use_ssl else "http"
103 if not port:
104 port = "443" if use_ssl else "80"
105
106 result = dict(
107 changed=True,
108 )
109
110 start = now()
111
112 ssl_context = None if validate_certs or not use_ssl else ssl._create_unverified_context()
113
114 url = f"{proto}://{module.params['host']}:{port}/cobbler_api"
115 conn = xmlrpc_client.ServerProxy(url, context=ssl_context)
116
117 try:
118 token = conn.login(username, password)
119 except xmlrpc_client.Fault as e:
120 module.fail_json(msg=f"Failed to log in to Cobbler '{url}' as '{username}'. {e}")
121 except Exception as e:
122 module.fail_json(msg=f"Connection to '{url}' failed. {e}")
123
124 if not module.check_mode:
125 try:
126 conn.sync(token)
127 except Exception as e:
128 module.fail_json(msg=f"Failed to sync Cobbler. {e}")
129
130 elapsed = now() - start
131 module.exit_json(elapsed=elapsed.seconds, **result)
132
133
134 if __name__ == "__main__":
135 main()