I have a role that I would like to execute multiple times, each execution with a different var. However, I would also like some of those executions to be conditional.
Here is a main.yml:
- hosts: localhost
roles:
- { role: test, test_files_group: 'a'}
- { role: test, test_files_group: 'b', when: False}
Here is the main.yml from the 'test' role (roles/test/tasks/main.yml
):
- name: List files
command: "find . ! -path . -type f"
args:
chdir: "{{ role_path }}/files/{{ test_files_group }}"
register: files
- debug: var=files.stdout_lines
- name: do something with the files
shell: "echo {{ item }}"
with_items: "{{ files.stdout_lines }}"
And here is part of the ansible-playbook command output:
TASK [test : List files]
*******************************************************
changed: [localhost]
TASK [test : debug] ************************************************************
ok: [localhost] => {
"files.stdout_lines": [
"./testfile-a"
]
}
TASK [test : do something with the files] **************************************
changed: [localhost] => (item=./testfile-a)
TASK [test : List files] *******************************************************
skipping: [localhost]
TASK [test : debug] ************************************************************
skipping: [localhost]
TASK [test : do something with the files] **************************************
fatal: [localhost]: FAILED! => {"failed": true, "msg": "'dict object' has no attribute 'stdout_lines'"}
Everything works for 'a' as expected, but then the do something with the files
task is executed for b, even though I set when: False
.
I feel like I'm missing something - what I'm wanting is for everything in roles/test/tasks/main.yml
to be executed with the test_files_group
var set accordingly, or not at all. What am I doing wrong? :)