Using with_items in a block statement

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

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.

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