Loop over dictionary when condition met

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:

---
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:

---
- 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.

You could use the selectattr Jinja2 filter.

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

2 Likes

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