4141 required: false
4242 type: int
4343 default: 10
44+ capacity_providers:
45+ version_added: 5.2.0
46+ description:
47+ - List of capacity providers to use for the cluster.
48+ required: false
49+ type: list
50+ elements: str
51+ capacity_provider_strategy:
52+ version_added: 5.2.0
53+ description:
54+ - List of capacity provider strategies to use for the cluster.
55+ required: false
56+ type: list
57+ elements: dict
58+ suboptions:
59+ capacity_provider:
60+ description:
61+ - Name of capacity provider.
62+ type: str
63+ weight:
64+ description:
65+ - The relative percentage of the total number of launched tasks that should use the specified provider.
66+ type: int
67+ base:
68+ description:
69+ - How many tasks, at a minimum, should use the specified provider.
70+ type: int
71+ default: 0
72+ purge_capacity_providers:
73+ version_added: 5.2.0
74+ description:
75+ - Toggle overwriting of existing capacity providers or strategy. This is needed for backwards compatibility.
76+ - By default I(purge_capacity_providers=false). In a release after 2024-06-01 this will be changed to I(purge_capacity_providers=true).
77+ required: false
78+ type: bool
79+ default: false
4480extends_documentation_fragment:
4581- amazon.aws.aws
4682- amazon.aws.ec2
5692 name: default
5793 state: present
5894
95+ - name: Cluster creation with capacity providers and strategies.
96+ community.aws.ecs_cluster:
97+ name: default
98+ state: present
99+ capacity_providers:
100+ - FARGATE
101+ - FARGATE_SPOT
102+ capacity_provider_strategy:
103+ - capacity_provider: FARGATE
104+ base: 1
105+ weight: 1
106+ - capacity_provider: FARGATE_SPOT
107+ weight: 100
108+ purge_capacity_providers: True
109+
59110- name: Cluster deletion
60111 community.aws.ecs_cluster:
61112 name: default
75126 description: how many services are active in this cluster
76127 returned: 0 if a new cluster
77128 type: int
129+ capacityProviders:
130+ version_added: 5.2.0
131+ description: list of capacity providers used in this cluster
132+ returned: always
133+ type: list
134+ defaultCapacityProviderStrategy:
135+ version_added: 5.2.0
136+ description: list of capacity provider strategies used in this cluster
137+ returned: always
138+ type: list
78139clusterArn:
79140 description: the ARN of the cluster just created
80141 type: str
112173 pass # Handled by AnsibleAWSModule
113174
114175from ansible_collections .amazon .aws .plugins .module_utils .core import AnsibleAWSModule
176+ from ansible .module_utils .common .dict_transformations import snake_dict_to_camel_dict
177+ from ansible .module_utils .common .dict_transformations import camel_dict_to_snake_dict
115178
116179
117180class EcsClusterManager :
@@ -145,8 +208,26 @@ def describe_cluster(self, cluster_name):
145208 return c
146209 raise Exception ("Unknown problem describing cluster %s." % cluster_name )
147210
148- def create_cluster (self , clusterName = 'default' ):
149- response = self .ecs .create_cluster (clusterName = clusterName )
211+ def create_cluster (self , cluster_name , capacity_providers , capacity_provider_strategy ):
212+ params = dict (clusterName = cluster_name )
213+ if capacity_providers :
214+ params ['capacityProviders' ] = snake_dict_to_camel_dict (capacity_providers )
215+ if capacity_provider_strategy :
216+ params ['defaultCapacityProviderStrategy' ] = snake_dict_to_camel_dict (capacity_provider_strategy )
217+ response = self .ecs .create_cluster (** params )
218+ return response ['cluster' ]
219+
220+ def update_cluster (self , cluster_name , capacity_providers , capacity_provider_strategy ):
221+ params = dict (cluster = cluster_name )
222+ if capacity_providers :
223+ params ['capacityProviders' ] = snake_dict_to_camel_dict (capacity_providers )
224+ else :
225+ params ['capacityProviders' ] = []
226+ if capacity_provider_strategy :
227+ params ['defaultCapacityProviderStrategy' ] = snake_dict_to_camel_dict (capacity_provider_strategy )
228+ else :
229+ params ['defaultCapacityProviderStrategy' ] = []
230+ response = self .ecs .put_cluster_capacity_providers (** params )
150231 return response ['cluster' ]
151232
152233 def delete_cluster (self , clusterName ):
@@ -159,7 +240,17 @@ def main():
159240 state = dict (required = True , choices = ['present' , 'absent' , 'has_instances' ]),
160241 name = dict (required = True , type = 'str' ),
161242 delay = dict (required = False , type = 'int' , default = 10 ),
162- repeat = dict (required = False , type = 'int' , default = 10 )
243+ repeat = dict (required = False , type = 'int' , default = 10 ),
244+ purge_capacity_providers = dict (required = False , type = 'bool' , default = False ),
245+ capacity_providers = dict (required = False , type = 'list' , elements = 'str' ),
246+ capacity_provider_strategy = dict (required = False ,
247+ type = 'list' ,
248+ elements = 'dict' ,
249+ options = dict (capacity_provider = dict (type = 'str' ),
250+ weight = dict (type = 'int' ),
251+ base = dict (type = 'int' , default = 0 )
252+ )
253+ ),
163254 )
164255 required_together = [['state' , 'name' ]]
165256
@@ -177,12 +268,53 @@ def main():
177268
178269 results = dict (changed = False )
179270 if module .params ['state' ] == 'present' :
271+ # Pull requested and existing capacity providers and strategies.
272+ purge_capacity_providers = module .params ['purge_capacity_providers' ]
273+ requested_cp = module .params ['capacity_providers' ]
274+ requested_cps = module .params ['capacity_provider_strategy' ]
180275 if existing and 'status' in existing and existing ['status' ] == "ACTIVE" :
181- results ['cluster' ] = existing
276+ existing_cp = existing ['capacityProviders' ]
277+ existing_cps = existing ['defaultCapacityProviderStrategy' ]
278+
279+ if requested_cp is None :
280+ requested_cp = []
281+
282+ # Check if capacity provider strategy needs to trigger an update.
283+ cps_update_needed = False
284+ if requested_cps is not None :
285+ for strategy in requested_cps :
286+ if snake_dict_to_camel_dict (strategy ) not in existing_cps :
287+ cps_update_needed = True
288+ for strategy in existing_cps :
289+ if camel_dict_to_snake_dict (strategy ) not in requested_cps :
290+ cps_update_needed = True
291+ elif requested_cps is None and existing_cps != []:
292+ cps_update_needed = True
293+
294+ # Unless purge_capacity_providers is true, we will not be updating the providers or strategy.
295+ if not purge_capacity_providers :
296+ module .deprecate ('After 2024-06-01 the default value of purge_capacity_providers will change from false to true.'
297+ ' To maintain the existing behaviour explicitly set purge_capacity_providers=true' ,
298+ date = '2024-06-01' , collection_name = 'community.aws' )
299+ cps_update_needed = False
300+ requested_cp = existing_cp
301+ requested_cps = existing_cps
302+
303+ # If either the providers or strategy differ, update the cluster.
304+ if requested_cp != existing_cp or cps_update_needed :
305+ if not module .check_mode :
306+ results ['cluster' ] = cluster_mgr .update_cluster (cluster_name = module .params ['name' ],
307+ capacity_providers = requested_cp ,
308+ capacity_provider_strategy = requested_cps )
309+ results ['changed' ] = True
310+ else :
311+ results ['cluster' ] = existing
182312 else :
183313 if not module .check_mode :
184314 # doesn't exist. create it.
185- results ['cluster' ] = cluster_mgr .create_cluster (module .params ['name' ])
315+ results ['cluster' ] = cluster_mgr .create_cluster (cluster_name = module .params ['name' ],
316+ capacity_providers = requested_cp ,
317+ capacity_provider_strategy = requested_cps )
186318 results ['changed' ] = True
187319
188320 # delete the cluster
0 commit comments