# Loop over dictionary when condition met

**URL:** <https://forum.ansible.com/t/loop-over-dictionary-when-condition-met/8601>\
**Category:** Get Help\
**Tags:** playbook\
**Created:** [September 18, 2024, 2:54pm UTC](https://forum.ansible.com/t/loop-over-dictionary-when-condition-met/8601 "2024-09-18T14:54:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dexrva](https://avatars.discourse-cdn.com/v4/letter/d/e47774/32.png) [@Dexrva](https://forum.ansible.com/u/Dexrva)\
**Post date:** [September 18, 2024, 2:54pm UTC](https://forum.ansible.com/t/loop-over-dictionary-when-condition-met/8601/1 "2024-09-18T14:54:41Z")

</div>

Hi all, I have a problem when I try to loop over dictionary. I need the execution of loop only occurs when the conditional is met but I can’t get it.

This is my dictionary:

```auto
---
remediation_scripts:
  - name: "Configure SMB driver"
    path: "HKEY_LOCAL_MACHINE\\XX\\XX\\XX\\XX"
    r_value: "Start"
    type: "Dword"
    value: "4"
  - name: "Allow ICMP"
    path: "HKEY_LOCAL_MACHINE\\XX\\XX\\XX\\XX\\XX"
    r_value: "Disabled"
    type: "Dword"
    value: "0"
  - name: "Allow TCP"
    path: "HKEY_LOCAL_MACHINE\\XX\\XX\\XX\\XX\\XX"
    r_value: "Disabled"
    type: "Dword"
    value: "0"

```

This is my playbook:

```auto
---
- name: Get remediation script for HC
  hosts: localhost
  gather_facts: false
  become: false
  vars_files:
    - dict_hc.yml 
  vars_prompt:
    - name: "rm_name"
      prompt: "Ingresar nombre de remediacion a ejecutar"
      private: no

  tasks:
  
   - name:
     set_fact:
       list_name: "{{ rm_name.split(',') }}"

   - name: Execute powershell
     win_shell: "Set-GPRegistryValue {{ item.name }}" -Key "{{ item.path }}" -ValueName "{{ item.r_value }} -Value {{ item.value}}"
     loop: "{{ remediation_scripts }}"
     when: -- Don't know how to put this conditional 

```

I need when the user enter for example: “Configure SMB driver” and “Allow ICMP” the loop only execute gettin the data of this two items.

I Hope you can help me with this.

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![shertel](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/shertel/32/1241_2.png) [@shertel](https://forum.ansible.com/u/shertel)\
**Post date:** [September 18, 2024, 7:57pm UTC](https://forum.ansible.com/t/loop-over-dictionary-when-condition-met/8601/2 "2024-09-18T19:57:37Z")

</div>

You could use the [selectattr Jinja2 filter](https://jinja.palletsprojects.com/en/3.1.x/templates/#jinja-filters.selectattr).

`loop: "{{ remediation_scripts | selectattr('name', 'in', list_name) }}"`

---

<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 18, 2024, 7:58pm UTC](https://forum.ansible.com/t/loop-over-dictionary-when-condition-met/8601/3 "2024-10-18T19:58:27Z")

</div>

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