How do you keep cloud modules idempotent?
IntermediateProductionAWX, AAP and Cloud Integration
Answer
Cloud resources often have no natural unique name, so give each one a stable identity and let the module match on it.
- amazon.aws.ec2_instance:
region: ap-south-1
name: "bastion-{{ env }}"
image_id: "{{ ami_id }}"
instance_type: t3.small
exact_count: 1
filters:
"tag:Name": "bastion-{{ env }}"
instance-state-name: [pending, running, stopped]
tags:
env: "{{ env }}"
- Use
exact_countwith filters instead ofcount, which creates more instances on every run. - Match on tags you control, not on generated IDs.
- Use
_infomodules first when the "create" module cannot match by name. - Avoid changing immutable properties in place; many modules replace or fail.
If you find yourself fighting this a lot, the resource probably belongs in Terraform, which tracks identity in state.
More on AWX, AAP and Cloud Integration
AWX or Red Hat Ansible Automation Platform: what is the difference and what do you pay for?BeginnerWhat are the core objects in AWX and how do they fit together?BeginnerHow do you manage AWX configuration as code?IntermediateHow do custom credential types work in AWX?AdvancedHow do you connect AWX to an external secrets manager?IntermediateHow do you trigger AWX jobs from CI or other systems?Intermediate