Filter on Condition in Loop

Hi

  • pause:
    prompt: Do You want to Add this item “{{ item }}” ?
    register: item_list
    with_lines: cat file_name
    run_once: true

  • set_fact:
    my_list: “{{ item_list.results | map(attribute=‘item’) | list | join(‘,’) }}”

In my playbook, i want to set fact with name my_list with items from file file_filename for which item_list.user_input is yes.
Please help in this.

Appreciate your help

Thanks,
Utkarsh

It’s done using selectattr

my_list: “{{ item_list.results | selectattr(‘user_input’, ‘match’, ‘^yes$’) | map(attribute=‘item’) | list | join(‘,’) }}”

Regards,
Utkarsh