terminating instances

hello all,

I run “ec2_instance_facts” to get some instance_ids:

  • ec2_instance_facts:
    region: us-east-1
    filters:
    “tag:Name”: “{{ cluster }}
    register: ec2_res

Then i can properly print the instance_ids only for those running:

  • name: print ips
    debug: “msg={{ item.instance_id }}”
    with_items: “{{ ec2_res.instances }}”
    when: item.state.name == “running”

BUT I can’t terminate them, i trying like this:

  • name: Terminate instances that were previously launched
    ec2:
    state: “absent”
    instance_ids: “{{ item.instance_id }}”
    when: item.state.name == “running”
    with_items: “{{ ec2_res.instances }}”

But this fails with:

fatal: [127.0.0.1]: FAILED! => {
“msg”: “The task includes an option with an undefined variable. The error was: ‘item’ is undefined\n\nThe error appears to have been in ‘/home/admin/voice-conf/playbooks/add_fs_to_cluster.yml’: line 34, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n - name: Terminate instances that were previously launched\n ^ here\n”
}

some help is appreciated!

David

Anyone able to help?

Anyone able to help?

Probably, but as you can see below your mail screwed up any formatting...

hello all,

I run "ec2_instance_facts" to get some instance_ids:

- ec2_instance_facts:
region: us-east-1
filters:
"tag:Name": "*{{ cluster }}*"
register: ec2_res

as you can see , no indentation at all.

But this fails with:

fatal: [127.0.0.1]: FAILED! => {
    "msg": "The task includes an option with an undefined variable. The
error was: 'item' is undefined\n\nThe error appears to have been in
'/home/admin/voice-conf/playbooks/add_fs_to_cluster.yml': line 34, column
5, but may\nbe elsewhere in the file depending on the exact syntax
problem.\n\nThe offending line appears to be:\n\n\n - name: Terminate
instances that were previously launched\n ^ here\n"

This is probably a indentation problem, but since your mail all the indentation is gone it's very hard to help.

It seems the “when” for the ec2 task is not lined up. “when” should be indented at the same level as ec2 task similar to the debug task

Shit. Ok I’m not home now, but I’ll try asap. And resend the email unformatted.

Thanks for replying, i appreciate it!