Using with_items in a block statement

529 views
Skip to first unread message

lift...@gmail.com

unread,
Feb 14, 2024, 1:18:56 PM2/14/24
to Ansible Project
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
    <play to retrieve folder info>

  - name: Take snapshot
    <play to take the snapshot>
with_items:
  - "vm1"
  - "vm2"

Is this possible?

Thanks,
Harry

Matt Martz

unread,
Feb 14, 2024, 1:38:50 PM2/14/24
to ansible...@googlegroups.com
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.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/6e23a052-353e-4936-a048-0f87b141e7c8n%40googlegroups.com.


--
Matt Martz
@sivel
sivel.net

lift...@gmail.com

unread,
Feb 15, 2024, 2:13:33 PM2/15/24
to Ansible Project
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
Reply all
Reply to author
Forward
0 new messages