89
89
"""
90
90
91
91
92
+ import re
92
93
from ansible .module_utils .basic import AnsibleModule
93
94
from ansible_collections .community .proxmox .plugins .module_utils .proxmox import (
94
95
proxmox_auth_argument_spec , ProxmoxAnsible )
@@ -99,24 +100,29 @@ def cluster_create(self):
99
100
cluster_name = self .module .params .get ("cluster_name" ) or self .module .params .get ("api_host" )
100
101
payload = {"clustername" : cluster_name }
101
102
103
+ # Get cluster data
104
+ cluster_data = self .proxmox_api .cluster .config .totem .get ()
105
+ # If we have data, check if we're already member of the desired cluster or a different one
106
+ if cluster_data and 'cluster_name' in cluster_data and cluster_data ['cluster_name' ] is not None :
107
+ if cluster_data ['cluster_name' ] == cluster_name :
108
+ self .module .exit_json (changed = False , msg = "Cluster '{}' already present." .format (cluster_name ), cluster = cluster_name )
109
+ else :
110
+ self .module .fail_json (msg = 'Error creating cluster: Node is already part of a different cluster - "{}"!' .format (cluster_data ['cluster_name' ]))
111
+
102
112
if self .module .params .get ("link0" ) is not None :
103
113
payload ["link0" ] = self .module .params .get ("link0" )
104
114
if self .module .params .get ("link1" ) is not None :
105
115
payload ["link1" ] = self .module .params .get ("link1" )
106
116
107
117
if self .module .check_mode :
108
- cluster_objects = self .proxmox_api .cluster .config .nodes .get ()
109
- if len (cluster_objects ) > 0 :
110
- self .module .fail_json (msg = "Error while creating cluster: Node is already part of a cluster!" )
111
- else :
112
- self .module .exit_json (changed = True , msg = "Cluster '{}' would be created (check mode)." .format (cluster_name ), cluster = cluster_name )
113
-
114
- try :
115
- self .proxmox_api .cluster .config .post (** payload )
116
- except Exception as e :
117
- self .module .fail_json (msg = "Error while creating cluster: {}" .format (str (e )))
118
+ self .module .exit_json (changed = True , msg = "Cluster '{}' would be created (check mode)." .format (cluster_name ), cluster = cluster_name )
119
+ else :
120
+ try :
121
+ self .proxmox_api .cluster .config .post (** payload )
122
+ except Exception as e :
123
+ self .module .fail_json (msg = "Error while creating cluster: {}" .format (str (e )))
118
124
119
- self .module .exit_json (changed = True , msg = "Cluster '{}' created." .format (cluster_name ), cluster = cluster_name )
125
+ self .module .exit_json (changed = True , msg = "Cluster '{}' created." .format (cluster_name ), cluster = cluster_name )
120
126
121
127
def cluster_join (self ):
122
128
master_ip = self .module .params .get ("master_ip" )
@@ -155,13 +161,15 @@ def proxmox_cluster_join_info_argument_spec():
155
161
return dict ()
156
162
157
163
158
- def validate_cluster_name (module , cluster_args , min_length = 1 , max_length = 15 ):
159
- if not isinstance (cluster_args ['cluster_name' ], str ):
160
- module .fail_json (msg = "Cluster name must be a string." )
164
+ def validate_cluster_name (module , min_length = 1 , max_length = 15 ):
165
+ cluster_name = module .params .get ("cluster_name" )
161
166
162
- if not (min_length <= len (cluster_args [ ' cluster_name' ] ) <= max_length ):
167
+ if not (min_length <= len (cluster_name ) <= max_length ):
163
168
module .fail_json (msg = "Cluster name must be between {} and {} characters long." .format (min_length , max_length ))
164
169
170
+ if not re .match (r"^[a-zA-Z0-9\-]+$" , cluster_name ):
171
+ module .fail_json (msg = "Cluster name must contain only letters, digits, or hyphens." )
172
+
165
173
166
174
def main ():
167
175
module_args = proxmox_auth_argument_spec ()
@@ -188,7 +196,7 @@ def main():
188
196
)
189
197
190
198
proxmox = ProxmoxClusterAnsible (module )
191
- validate_cluster_name (module , cluster_args )
199
+ validate_cluster_name (module )
192
200
193
201
# The Proxmox VE API currently does not support leaving a cluster
194
202
# or removing a node from a cluster. Therefore, we only support creating
0 commit comments