@@ -77,8 +77,20 @@ def __init__(self, resource_name, supports_check_mode=True):
7777 default = "present" ,
7878 ),
7979 )
80+ module_remove_non_updatable_params = dict (
81+ remove_non_updatable_params = dict (
82+ type = "str" ,
83+ choices = ["yes" , "no" ],
84+ default = "no" ,
85+ help = (
86+ "If set to `true`, the module will remove non-updatable attributes from the module params "
87+ "and update the resource. If set to `false`, the module will return an error if non-updatable "
88+ "attributes are present in the module params."
89+ ),
90+ ),
91+ )
8092 argument_spec .update (module_state_argument )
81-
93+ argument_spec . update ( module_remove_non_updatable_params )
8294 self .module = AnsibleModule (
8395 argument_spec = argument_spec ,
8496 supports_check_mode = supports_check_mode ,
@@ -421,6 +433,7 @@ def install(self):
421433 @trace
422434 def create_or_update (self ):
423435 desired_state = self .module .params ["state" ]
436+ remove_non_updatable_params = self .module .params .get ("remove_non_updatable_params" , "no" )
424437 if (
425438 desired_state == "present" and
426439 self .resource_name .endswith ("_binding" ) and
@@ -494,7 +507,8 @@ def create_or_update(self):
494507 else :
495508 # Update process will go through 2 iterations of is_resource_identical
496509 # 1. First iteration will check if the resource is identical. If not, it will give the list of non-updatable attributes that exists
497- # in the user playbook. If non-updatable attributes are present, it will remove them from the module_params and update the resource
510+ # in the user playbook. If non-updatable attributes are present and user marks `remove_non_updatable_params` as yes,
511+ # it will remove them from the module_params and update the resource
498512 # 2. Second iteration will check if the resource is identical. If not, it will update the resource after ignoring the
499513 # non-updatable resources
500514 is_identical , immutable_keys_list = self .is_resource_identical ()
@@ -541,8 +555,9 @@ def create_or_update(self):
541555 ok , err = create_resource (
542556 self .client , self .resource_name , self .resource_module_params
543557 )
544-
545- elif immutable_keys_list is None :
558+ # Here we are checking if resource has immutable keys
559+ # if yes, we will check if the user wants to keep the non-updatable params (which in turn will return error)
560+ elif immutable_keys_list is None or (immutable_keys_list and remove_non_updatable_params == "no" ):
546561 self .module_result ["changed" ] = True
547562 log (
548563 "INFO: Resource %s:%s exists and is different. Will be UPDATED."
@@ -554,6 +569,7 @@ def create_or_update(self):
554569 if not ok :
555570 self .return_failure (err )
556571 else :
572+ # in case user wants to remove non-updatable params, we will remove them from the module_params
557573 for key in immutable_keys_list :
558574 self .resource_module_params .pop (key )
559575
0 commit comments