# Using registered variable in a loop

**URL:** <https://forum.ansible.com/t/using-registered-variable-in-a-loop/8806>\
**Category:** Get Help\
**Tags:** azure, variables, playbook, molecule, jinja2, ansible-core\
**Created:** [September 26, 2024, 10:06pm UTC](https://forum.ansible.com/t/using-registered-variable-in-a-loop/8806 "2024-09-26T22:06:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheSpy0](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/thespy0/32/496_2.png) [@TheSpy0](https://forum.ansible.com/u/TheSpy0)\
**Post date:** [September 26, 2024, 10:06pm UTC](https://forum.ansible.com/t/using-registered-variable-in-a-loop/8806/1 "2024-09-26T22:06:49Z")

</div>

I’m trying to configure Molecule to use Azure’s Devtest Lab virtual machines for testing playbooks using the `delegated|default` driver. The current issue that I’m running into is getting the necessary data from a registered variable from the [azure\_rm\_devtestlabvirtualmachine\_info](https://docs.ansible.com/ansible/latest/collections/azure/azcollection/azure_rm_devtestlabvirtualmachine_info_module.html#ansible-collections-azure-azcollection-azure-rm-devtestlabvirtualmachine-info-module) module that is being used in a `loop` to populate the `instances-config.yml` file.

Below is a snippet of the section in question. If necessary, the entire `create.yml` playbook can be provided. Please note that there is not a problem with the virtual machines being created.

```yaml
    - name: Get instance information to populate instance-config file
      azure.azcollection.azure_rm_devtestlabvirtualmachine_info:
        lab_name: "{{ molecule_yml.driver.azure_lab_name }}"
        resource_group: "{{ molecule_yml.driver.azure_resource_group }}"
        name: "{{ item.name }}"
      register: server
      changed_when: true # Task is always changed.
      loop: "{{ molecule_yml.platforms }}"

    - name: Display registered variable contents
      ansible.builtin.debug:
        var: server.results | selectattr('virtualmachines') | length

    - name: Create instance config
      when: server.changed | default(false) | bool # noqa no-handler
      block:
        - name: Populate instance config dict # noqa jinja
          ansible.builtin.set_fact:
            instance_conf_dict: {
              'instance': "{{ item.name }}",
              'address': "{{ item.fqdn }}",
              'user': "{{ item.user_name }}",
              'identity_file': "{{ molecule_yml.driver.ssh_key_file }}",
              'port': "22"
            }
          loop: "{{ server.results | selectattr('virtualmachines') | list }}"
          register: instance_config_dict

```

The `name`, `fqdn`, and `user_name` return as either not being defined or not being an attribute in a list or dictionary. Maybe there’s a different way that the `item` variable can be noted.

Here is a shortened version of the variable in question. The specific section of the result I’m trying to access is the `virtualmachines` section, which appears to be a list. Using `server.results.virtualmachines` produces a `VARIABLE IS NOT DEFINED` error. `server.results | selectattr('virtualmachines', 'defined') | map(attribute='virtualmachines')` and `server.results | selectattr('virtualmachines', 'defined')` produce output, but it looks the same as the original.

```json
[localhost] => {
    "server": {
        "changed": true,
        "msg": "All items completed",
        "results": [
            {
                "ansible_loop_var": "item",
                "changed": true,
                "failed": false,
                "invocation": {
                    "module_args": {
                     ...
                    }
                },
                "item": {
                ...
                },
                "virtualmachines": [
                    {
                        ...
                        "fqdn": "VM-NAME.example.com",
                        ...
                        "image": {
                        ...
                        },
                        ...
                        "name": "VM-NAME",
                        "notes": "Virtual machine notes, just something...",
                        "os_type": "linux",
                        "provisioning_state": "Succeeded",
            }
        ],
        "skipped": false
    }
}

```

In case this information is relevant.

```bash
molecule 24.2.0 using python 3.12 
    ansible:2.16.4
    default:24.2.0 from molecule

```

I’ve probably missed something very basic when working with more complex data structures.

---

<div class="post-metadata">

**Author:** ![flowerysong](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/flowerysong/32/2305_2.png) [@flowerysong](https://forum.ansible.com/u/flowerysong)\
**Post date:** [September 27, 2024, 2:21am UTC](https://forum.ansible.com/t/using-registered-variable-in-a-loop/8806/2 "2024-09-27T02:21:33Z")

</div>

`selectattr()` filters a list by testing whether it meets the criterion (either a test name like `'defined'` or if no test is provided it checks for truthiness.) It does not change the individual list entries in any way, so in this case it will probably still be the exact same list.

You want something more like this:

```yaml
loop: "{{ server.results | map(attribute='virtualmachines') | flatten }}"

```

---

<div class="post-metadata">

**Author:** ![TheSpy0](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/thespy0/32/496_2.png) [@TheSpy0](https://forum.ansible.com/u/TheSpy0)\
**Post date:** [September 27, 2024, 3:43am UTC](https://forum.ansible.com/t/using-registered-variable-in-a-loop/8806/3 "2024-09-27T03:43:06Z")

</div>

Fantastic! That did the trick. Thank you so much! I’ve been trying to wrap my head around this all day.

Could you please explain more about why this solution worked? More specifically regarding the `map` filter. I’ve read [Manipulating data](https://docs.ansible.com/ansible/latest/playbook_guide/complex_data_manipulation.html) and the [Jinja2 documentation](https://jinja.palletsprojects.com/en/3.0.x/templates/#jinja-filters.map) for it, and it appears that more than `attribute` can be passed to it, filters for example. Are there even more options that it uses? If you have any other sources that have helped learning about using filters, I would greatly appreciate it.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [October 27, 2024, 3:43am UTC](https://forum.ansible.com/t/using-registered-variable-in-a-loop/8806/4 "2024-10-27T03:43:49Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
