Hi ansible squad!
I’ve been using amazon.aws.ec2 (or community.aws.ec2_instance - ive been switching back and forth) to ensure there is an ec2 instance created for each {{ inventory_hostname }} by using tags. It doesn’t seem to work.
Every time I run the playbook with the below play, it just creates a new ec2 instance? How can I make it more idempotent? If the instance already exists, dont create a new one?
Preferably without other plays?
- name: Ensure EC2 instance exists
amazon.aws.ec2:
aws_access_key: ‘{{ aws_access_key }}’
aws_secret_key: ‘{{ aws_secret_key }}’
region: ‘{{ aws_region }}’
image: ‘{{ aws_ec2_ami }}’
instance_type: ‘{{ aws_instance_type_web }}’
vpc_subnet_id: ‘{{ aws_subnet_id }}’
group: ‘SG-{{ environment_designator }}’
key_name: ‘Keypair-{{ environment_designator }}’
instance_tags:
Mytag.MyEnvironment: “”
Mytag.MyEnvironment.Environment: ‘{{ environment_designator }}’
Mytag.MyEnvironment.Roles: web1
Mytag.MyEnvironment.Hostname: ‘{{ inventory_hostname }}’
wait: yes
assign_public_ip: no
register: ec2_instances
delegate_to: localhost