Hello,
When creating ec2 instances we have the “id” field for idempotency. I was wondering how long previously-used ids linger for and, if they auto-prune, whether they can be removed early/manually.
Thank you,
Mark
Hello,
When creating ec2 instances we have the “id” field for idempotency. I was wondering how long previously-used ids linger for and, if they auto-prune, whether they can be removed early/manually.
Thank you,
Mark
You can see links to the documentation for the ID here:
http://docs.ansible.com/ec2_module.html
However, a better way to do it is to use the “exact_count” parameter and specify a tag.
We should likely mark the id field in the documentation as legacy and non-preferred.
Thanks, and sorry! I’d seen that page some time ago but iirc it has more info than before and I didn’t think to recheck it.
Will definitely be switching to exact_count as well.
Thanks again,
Mark
Okay, so in the interest of moving to exact_count I switched from:
- name: Set up a new host instance on EC2
ec2: >
region=“{{ ec2vars.region }}”
zone=“{{ ec2vars.zone }}”
id=“{{ serverPod + '’ + type }}"
ec2_access_key=“{{ ec2vars.ec2_access_key }}”
ec2_secret_key=“{{ ec2vars.ec2_secret_key }}”
key_name=“{{ ec2vars.key_name }}”
instance_type=“{{ ec2vars.instance_type }}”
image=“{{ ec2vars.image }}”
group=“{{ ec2vars.group }}”
wait=“{{ wait }}”
wait_timeout=600
user_data=“{{ type }}”
instance_tags='{“ec2serverPod”:“{{ serverPod }}”, “ec2type”:“{{ type }}”, “ec2serverPod_and_ec2type”:"{{ serverPod }}{{ type }}”, “provisioned”:“False”, “Name”:“{{ virtualhostFullName }}_{{ type }}”}’
count=“{{ count }}”
vpc_subnet_id=“{{ ec2vars.vpc_subnet_id }}”
register: ec2
to:
- name: Set up a new host instance on EC2
ec2: >
region=“{{ ec2vars.region }}”
zone=“{{ ec2vars.zone }}”
ec2_access_key=“{{ ec2vars.ec2_access_key }}”
ec2_secret_key=“{{ ec2vars.ec2_secret_key }}”
key_name=“{{ ec2vars.key_name }}”
instance_type=“{{ ec2vars.instance_type }}”
image=“{{ ec2vars.image }}”
group=“{{ ec2vars.group }}”
wait=“{{ wait }}”
wait_timeout=600
user_data=“{{ type }}”
instance_tags=‘{“ec2serverPod”:“{{ serverPod }}”, “ec2type”:“{{ type }}”, “ec2serverPod_and_ec2type”:“{{ serverPod }}{{ type }}“, “provisioned”:“False”, “Name”:”{{ virtualhostFullName }}{{ type }}”}’
exact_count=“{{ count }}”
count_tag=“ec2serverPod_and_ec2type”:“{{ serverPod }}_{{ type }}”
vpc_subnet_id=“{{ ec2vars.vpc_subnet_id }}”
register: ec2
but I seem to have lost idempotence because I get new instances every time it runs. I’ve since reformatted it to drop the quoted block and make it look more like the docs but there was no improvement:
- name: Set up a new host instance on EC2
local_action:
module: ec2
region: “{{ ec2vars.region }}”
zone: “{{ ec2vars.zone }}”
ec2_access_key: “{{ ec2vars.ec2_access_key }}”
ec2_secret_key: “{{ ec2vars.ec2_secret_key }}”
key_name: “{{ ec2vars.key_name }}”
instance_type: “{{ ec2vars.instance_type }}”
image: “{{ ec2vars.image }}”
group: “{{ ec2vars.group }}”
wait: “{{ wait }}”
wait_timeout: 600
user_data: “{{ type }}”
instance_tags: ‘{“ec2serverPod”:“{{ serverPod }}”, “ec2type”:“{{ type }}”, “ec2serverPod_and_ec2type”:“{{ serverPod }}{{ type }}“, “provisioned”:“False”, “Name”:”{{ virtualhostFullName }}{{ type }}”}’
exact_count: “{{ count }}”
count_tag: ec2serverPod_and_ec2type:“{{ serverPod }}_{{ type }}”
vpc_subnet_id: “{{ ec2vars.vpc_subnet_id }}”
register: ec2
Anyone see anything obvious with that last example? Do I have to list all of the instance’s tags in count_tag?
Thank you,
Mark
I would first try to make the instance_tags a dictionary instead of a "dictionary-like" string:
instance_tags:
foo: bar
baz: bang
who: doo
And use the same pattern for the count tag.
I had to leave one of the tags out of count_tag since it just indicates whether the host has been “provisioned” by our definition, and I was getting new instances again when it changed. So I didn’t have to do the whole set of tags, but adding the balance of them in the format you suggested appears to have worked! Thanks!
I did also have to change some tasks’ “with_items: ec2.instances” to “with_items: ec2.tagged_instances” to correct errors to the effect of:
TASK: [ec2/ec2_add | Add new instance(s) to group "ec2hosts"] ***************** fatal: [localhost] => One or more undefined variables: 'unicode object' has no attribute 'private_dns_name'
(and that’s a logical progression. I’m just mentioning it here in case it helps someone switching over in the future.)
Thanks again!