Interview Prep/Ansible/AWX, AAP and Cloud Integration

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_count with filters instead of count, which creates more instances on every run.
  • Match on tags you control, not on generated IDs.
  • Use _info modules 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.

All Ansible questions Roadmap: Ansible

More on AWX, AAP and Cloud Integration