set_fact undefined variable issue when concat string to itself.

I need to set_fact variable and contact to itself a list of items as below:

`

  • set_fact:
    excludefolders: “{{ excludefolders + ’ -o -name ’ + item | default(‘’) }}”
    with_items: “{{ lookup(‘vars’, ‘MY_’ + Layer).split(‘,’) }}”
    `

Getting error:

FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'excludefolders' is undefined\n\nThe error appears to be in '/app/test.yml': line 86, column 6, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n   - set_fact:\n     ^ here\n"}

Is it neccessary to defined another set_fact before like below or is there a better solution to the issue ?

`

  • set_fact:
    excludefolders: “”

  • set_fact:
    excludefolders: “{{ excludefolders + ’ -o -name ’ + item | default(‘’) }}”
    with_items: “{{ lookup(‘vars’, ‘EXCLUDE_’ + Layer).split(‘,’) }}”

`

You can try this -

  • set_fact:
    excludefolders: “{{ excludefolders | default(‘’) + ’ -o -name ’ + item | default(‘’) }}”
    with_items: “{{ lookup(‘vars’, ‘EXCLUDE_’ + Layer).split(‘,’) }}”

It helped. Thank you @Abhijeet