# Using with\_items in a block statement

**URL:** <https://forum.ansible.com/t/using-with-items-in-a-block-statement/38689>\
**Category:** Ansible Project\
**Tags:** vmware\
**Created:** [February 14, 2024, 6:18pm UTC](https://forum.ansible.com/t/using-with-items-in-a-block-statement/38689 "2024-02-14T18:18:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [February 14, 2024, 6:18pm UTC](https://forum.ansible.com/t/using-with-items-in-a-block-statement/38689/1 "2024-02-14T18:18:55Z")

</div>

I’d like to do an operation on several VMs that have 2 plays for each. Those are using the VMware collection to find the VMs folder in the datacenter, then use that folder to take a snapshot of the VM.

What I was hoping to do was something like this:

block:

- name: Find folder  

- name: Take snapshot  
  
with\_items:

- “vm1”

- “vm2”

Is this possible?

Thanks,  
Harry

---

<div class="post-metadata">

**Author:** ![sivel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/sivel/32/1198_2.png) [@sivel](https://forum.ansible.com/u/sivel)\
**Post date:** [February 14, 2024, 6:38pm UTC](https://forum.ansible.com/t/using-with-items-in-a-block-statement/38689/2 "2024-02-14T18:38:02Z")

</div>

No, you cannot loop on a block. You can put the tasks in another YAML file, and use `include_tasks` instead, which you can loop on.

---

<div class="post-metadata">

**Author:** ![harry\_devine](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/harry_devine/32/5293_2.png) [@harry\_devine](https://forum.ansible.com/u/harry_devine)\
**Post date:** [February 15, 2024, 7:13pm UTC](https://forum.ansible.com/t/using-with-items-in-a-block-statement/38689/3 "2024-02-15T19:13:33Z")

</div>

A follow-up question:

I have the “include\_tasks” working, now I’m trying to expand on that. Each iteration through retrieve’s a VM’s snapshot info and registers that into a variable. This is within the “loop” task for each VM. When I try to print that out, all I get is:

TASK [Print results] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
ok: [localhost] =\> {  
“msg”: {  
“changed”: false,  
“failed”: false,  
“guest\_snapshots”: {}  
}  
}

Eventually I’d like to put that into a template and email it, but how can I retrieve that variable for each iteration?

Thanks,  
Harry
