Score:0

Return Values from Ansible Role Tasks in a loop are only registered for the last item

tr flag

I have a role ovirt_vm_add_disk_role that creates a new disk for a VM with ovirt_disk module.

In that role I register the variable disk_info which contains the results of the ovirt_disk module:

- name: "Update disk information"
  ovirt_disk:
    auth: "{{ ovirt_auth }}"
    activate: true
    name: "{{ disk_name }}"
    poll_interval: 10
    vm_name: "{{ HOSTNAME }}"
  delegate_to: localhost
  register: disk_info

The contents of disk_info looks like:

"msg": [
    {
        "active": true,
        "bootable": false,
        "disk": {
            "href": "/ovirt-engine/api/disks/6814659f-f89c-4b6e-b067-f09272b1095b",
            "id": "6814659f-f89c-4b6e-b067-f09272b1095b"
        },
        "href": "/ovirt-engine/api/vms/3d76975e-b3e2-4fa5-8c27-5b3eca1825eb/diskattachments/6814659f-f89c-4b6e-b067-f09272b1095b",
        "id": "6814659f-f89c-4b6e-b067-f09272b1095b",
        "interface": "virtio_scsi",
        "logical_name": "/dev/sde",
        "pass_discard": false,
        "read_only": false,
        "uses_scsi_reservation": false,
        "vm": {
            "href": "/ovirt-engine/api/vms/3d76975e-b3e2-4fa5-8c27-5b3eca1825eb",
            "id": "3d76975e-b3e2-4fa5-8c27-5b3eca1825eb"
        }
    }
]

When I call that role for a single disk everything is fine and I can use disk_info in the playbook that called the role.

However when I need to loop over the role to add more than one disk I can only see disk_info for the last iteration.

Loop:

  - name: Add DATA Disks loop
      vars:
        disk_name: "{{ HOSTNAME }}_{{ item }}"
        disk_size: "{{ disk_size }}"
        disk_storage: "{{ invstorage_domain }}"
      include_role:
        name: ovirt_vm_add_disk_role
      with_items: "{{ disks }}"
      register: asm_data_devices

When adding to Disks with variable disks: ['DATA01','DATA02'] then disk_info does only contain information about DATA02.

Tried to register the outcome of the loop in variable asm_data_devices but it does not contain return values of ovirt_disk module at all:

"msg": [
    {
        "changed": false,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "include_args": {
                    "name": "ovirt_vm_add_disk_role",
                    "public": true
                },
                "item": "DATA01"
            },
            {
                "ansible_loop_var": "item",
                "include_args": {
                    "name": "ovirt_vm_add_disk_role",
                    "public": true
                },
                "item": "DATA02"
            }
        ]
    }
]

So how can I get the return values from ovirt_disk module in outer playbook calling the role for all items of the loop?

Score:0
fr flag

Registering the result of an include returns the result of the include, not of the included tasks.

The easiest solution in your case is to move the loop from the include to the relevant task:

- name: "Update disks information"
  ovirt_disk:
    auth: "{{ ovirt_auth }}"
    activate: true
    name: "{{ HOSTNAME }}_{{ item }}"
    poll_interval: 10
    vm_name: "{{ HOSTNAME }}"
  delegate_to: localhost
  with_items: "{{ disks }}"
  register: asm_data_devices

Since you now register with a loop, asm_data_devises.results will be a list where each item is the result from a single iteration.

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.