# terminating instances

**URL:** <https://forum.ansible.com/t/terminating-instances/27508>\
**Category:** Ansible Project\
**Created:** [May 10, 2018, 9:27pm UTC](https://forum.ansible.com/t/terminating-instances/27508 "2018-05-10T21:27:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [May 10, 2018, 9:27pm UTC](https://forum.ansible.com/t/terminating-instances/27508/1 "2018-05-10T21:27:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [May 11, 2018, 5:51pm UTC](https://forum.ansible.com/t/terminating-instances/27508/2 "2018-05-11T17:51:39Z")

</div>

Anyone able to help?

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [May 11, 2018, 6:24pm UTC](https://forum.ansible.com/t/terminating-instances/27508/3 "2018-05-11T18:24:37Z")

</div>

> 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! =\> {  
> > &nbsp;&nbsp;&nbsp;&nbsp;"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.

---

<div class="post-metadata">

**Author:** ![Tchia04](https://avatars.discourse-cdn.com/v4/letter/t/fbc32d/32.png) [@Tchia04](https://forum.ansible.com/u/Tchia04)\
**Post date:** [May 11, 2018, 10:38pm UTC](https://forum.ansible.com/t/terminating-instances/27508/4 "2018-05-11T22:38:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![David\_Villasmil](https://avatars.discourse-cdn.com/v4/letter/d/a5b964/32.png) [@David\_Villasmil](https://forum.ansible.com/u/David_Villasmil)\
**Post date:** [May 11, 2018, 10:46pm UTC](https://forum.ansible.com/t/terminating-instances/27508/5 "2018-05-11T22:46:09Z")

</div>

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

Thanks for replying, i appreciate it!
