Debian Codegraph @

ansible_collections/community/proxmox/plugins/modules/proxmox_domain_sync.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2026, (@teslamania) <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7
8 DOCUMENTATION = r"""
9 module: proxmox_domain_sync
10 version_added: 2.0.0
11 short_description: Sync realms.
12 description: Sync domain realms, LDAP or AD.
13 author: Vial Nicolas (@teslamania)
14 attributes:
15 check_mode:
16 support: full
17 diff_mode:
18 support: none
19
20 options:
21 enable_new:
22 description: Enable creation of new users.
23 required: false
24 type: bool
25 scope:
26 description: Select what to sync.
27 required: false
28 choices: ['users', 'groups', 'both']
29 type: str
30 realm:
31 description: Authentication domain ID.
32 required: true
33 type: str
34 remove_vanished:
35 description:
36 - A semicolon-separated list of things to remove when they or the user vanishes during a sync.
37 - The following values are possible
38 - C(remove_vanished=acl) removes acls when the user/group is not returned from the sync.
39 - C(remove_vanished=properties) removes the set properties on existing user/group that do not appear in the source (even custom ones).
40 - C(remove_vanished=entry) removes the user/group when not returned from the sync.
41 - Instead of a list it also can be C(remove_vanishe=none).
42 - Exemple C(remove_vanished="acl;properties;entry")
43 required: false
44 type: str
45
46 extends_documentation_fragment:
47 - community.proxmox.proxmox.actiongroup_proxmox
48 - community.proxmox.proxmox.documentation
49 - community.proxmox.attributes
50 - community.proxmox.attributes.info_module
51 """
52
53 EXAMPLES = r"""
54 - name: Sync LDAP domain
55 community.proxmox.proxmox_domain_sync:
56 realm: "example.test"
57 enable_new: true
58 scope: both
59 removed_vanished: "acl;properties;entry"
60 """
61
62 RETURN = r"""
63 msg:
64 description: The output message that the module generates.
65 type: str
66 returned: always
67 """
68
69
70 from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
71 ProxmoxAnsible,
72 ansible_to_proxmox_bool,
73 create_proxmox_module,
74 )
75
76
77 def module_args():
78 return dict(
79 enable_new=dict(type="bool"),
80 realm=dict(type="str", required=True),
81 remove_vanished=dict(type="str"),
82 scope=dict(choices=["users", "groups", "both"]),
83 )
84
85
86 def module_options():
87 return {}
88
89
90 class ProxmoxDomainSyncAnsible(ProxmoxAnsible):
91 def __init__(self, module):
92 super().__init__(module)
93 self.params = module.params
94
95 def check_domain(self, realm):
96 domains = self.proxmox_api.access.domains.get()
97 return realm in [item["realm"] for item in domains]
98
99 def build_arg(self):
100 options = {}
101 if self.params.get("enable_new") is not None:
102 options["enable-new"] = ansible_to_proxmox_bool(self.params["enable_new"])
103 if self.params.get("remove_vanished") is not None:
104 options["remove-vanished"] = self.params["remove_vanished"]
105 if self.params.get("scope") is not None:
106 options["scope"] = self.params["scope"]
107
108 return options
109
110 def sync_domain(self):
111 options = self.build_arg()
112 if self.check_domain(self.params["realm"]):
113 if not self.module.check_mode:
114 self.proxmox_api.access.domains(self.params["realm"]).sync.post(**options)
115 msg = f"Domain {self.params['realm']} synced."
116 else:
117 msg = f"Domain {self.params['realm']} would be synced."
118
119 self.module.exit_json(changed=True, msg=msg)
120 else:
121 self.module.fail_json(msg=f"Domain {self.params['realm']} not present.")
122
123
124 def main():
125 module = create_proxmox_module(module_args(), **module_options())
126 proxmox = ProxmoxDomainSyncAnsible(module)
127
128 try:
129 proxmox.sync_domain()
130 except Exception as e:
131 module.fail_json(msg=f"An error occurred during sync: {e}")
132
133
134 if __name__ == "__main__":
135 main()