# Using yaml anchors for loop items module parameters

**URL:** <https://forum.ansible.com/t/using-yaml-anchors-for-loop-items-module-parameters/5826>\
**Category:** Get Help\
**Tags:** playbook, ansible-core\
**Created:** [May 15, 2024, 12:59pm UTC](https://forum.ansible.com/t/using-yaml-anchors-for-loop-items-module-parameters/5826 "2024-05-15T12:59:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![netgab](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/netgab/32/3458_2.png) [@netgab](https://forum.ansible.com/u/netgab)\
**Post date:** [May 15, 2024, 12:59pm UTC](https://forum.ansible.com/t/using-yaml-anchors-for-loop-items-module-parameters/5826/1 "2024-05-15T12:59:07Z")

</div>

Hi Ansible community,  
is it possible to use YAML anchors, to populate module parameters in a loop?

Example 1: Doing it without anchors (currently working):

```yaml
- name: "Create a Meraki RF profile"
  vars:
    rf_profiles:
      - name: PROFILE1
        bandSelectionType: ap
        twoFourGhzSettings:
          minBitrate: 24
          channelWidth: auto
          maxPower: 17
          minPower: 5              
      - name: PROFILE2
        bandSelectionType: ap
        twoFourGhzSettings:
          minBitrate: 36
          channelWidth: auto
          maxPower: 19
          minPower: 2           
  cisco.meraki.networks_wireless_rf_profiles:
    name: "{{ item.name }}"
    bandSelectionType: "{{ item.bandSelectionType }}"
    # .... other parameters
  loop: "{{ rf_profiles }}"

```

The drawback of this solution is, that all possible module parameters must be included in the variable block and also the module block. Furthermore, optional parameters, which are defaulted by the target system, must be catched if they do not exist in the `vars` block (e.g. using `default` filter)

Example 2: Doing it with anchors (currently not working):

```yaml
- name: "Create a Meraki RF profile"
  vars:
    rf_profiles: &rf_profile_anchor
      - name: PROFILE1
        bandSelectionType: ap
        twoFourGhzSettings:
          minBitrate: 24
          channelWidth: auto
          maxPower: 17
          minPower: 5              
      - name: PROFILE2
        bandSelectionType: ap
        twoFourGhzSettings:
          minBitrate: 36
          channelWidth: auto
          maxPower: 19
          minPower: 2           
  cisco.meraki.networks_wireless_rf_profiles:
    <<: *rf_profile_anchor
  loop: "{{ rf_profiles }}"

```

With this solution I want to inject the module properties directly from the variable loop iteration. However, this does not really work - only the content of the first list item is transmitted in the actual module.

Is there an elegant way to do this?

---

<div class="post-metadata">

**Author:** ![kurokobo](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/kurokobo/32/182_2.png) [@kurokobo](https://forum.ansible.com/u/kurokobo)\
**Post date:** [May 15, 2024, 1:50pm UTC](https://forum.ansible.com/t/using-yaml-anchors-for-loop-items-module-parameters/5826/2 "2024-05-15T13:50:11Z")

</div>

Hi, the YAML anchors and aliases are supported, but AFAIK it’s a bit difficult to use with loop as you expected.

Since anchors are a feature of YAML, all aliases are resolved _before_ Ansible evaluates it as a playbook. This means that Ansible cannot know that the playbook contains anchors. Refer to:

[https://docs.ansible.com/ansible/latest/playbook\_guide/playbooks\_advanced\_syntax.html#yaml-anchors-and-aliases-sharing-variable-values](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_advanced_syntax.html#yaml-anchors-and-aliases-sharing-variable-values)

Alternatively, you can simply passing dict to modules:

```yaml
- name: "Create a Meraki RF profile"
  vars:
    rf_profiles:
      - name: PROFILE1
        bandSelectionType: ap
        twoFourGhzSettings:
          minBitrate: 24
          channelWidth: auto
          maxPower: 17
          minPower: 5              
      - name: PROFILE2
        bandSelectionType: ap
        twoFourGhzSettings:
          minBitrate: 36
          channelWidth: auto
          maxPower: 19
          minPower: 2           
  cisco.meraki.networks_wireless_rf_profiles: "{{ item }}"
  loop: "{{ rf_profiles }}"

```

This will probably result following `[WARNING]` to be shown, but may works as expected anyway.

```bash
[WARNING]: Using a variable for a task's 'args' is unsafe in some situations (see https://docs.ansible.com/ansible/devel/reference_appendices/faq.html#argsplat-unsafe)

```

Refer to the following FAQ to know why this may be _unsafe_: [Frequently Asked Questions — Ansible Community Documentation](https://docs.ansible.com/ansible/devel/reference_appendices/faq.html#argsplat-unsafe)
