0

I have a ansible role with the simple task of a finding some files in multiple paths and a debug statement showing what it did find, looks like this:

- name: "find smth"
  find:
    paths: "{{ search_path }}"
    file_type: file
    patterns: "*.gz"
    recurse: no
  loop_control:
    loop_var: search_path
  loop: "{{ search_paths }}"
  register: find_result

- debug:
    msg: "{{ file_path }}"
  loop_control:
    loop_var: file_path
  loop: "{{ find_result.results | map(attribute='files') | sum(start=[]) | map(attribute='path') | list }}"

This works when i use a playbook BUT it does not work when i call the role via commandline like this:

ansible HOST -u REMOTE_USER -m include_role -a name=ROLE_NAME -e '{"include": {"step1": "06_test"}}' -e '{"search_paths":["/tmp","/opt/tmp"]}'

main.yml (just to explain the step1 include):

- include: "{{ item.value }}.yml"
  loop: "{{ lookup('dict', include, wantlist=True) }}"

In this case the debug task always prints:

HOST | SUCCESS => {
    "msg": "All items completed"
}

Which is the result message of the loop, the registered find_result structure is there but i can not access it via the loop in the debug task. Accessing a value however works (e.g. msg: "{{ find_result.results[0].files[0].path }}").

I am using ansible in version 2.9.9 with python 3.5.7 on debian stretch. The tasks above are only a broke down test of the real scenario.

  • 2
    [`include_role` module is not meant to be played as an ad-hoc task](https://github.com/ansible/ansible/issues/22983#issuecomment-289445554) – Zeitounator May 29 '20 at 19:52
  • 1
    @Zeitounator That is old information, it was proposed and integrated https://github.com/ansible/proposals/issues/131 – user13641697 May 29 '20 at 23:07
  • Ha ! Interesting thanks for the feedback. I have to admit your use case is actually quite useful. – Zeitounator May 29 '20 at 23:22
  • Well it happens that [I had already tested this a while ago](https://stackoverflow.com/questions/55832732/debug-statement-in-ansible-playbook-behaves-differently-than-in-ansible-role). The result is still the same today: debugging messages in a loop will not show up (just tested with your above structure). Debugging your result directly works though => `msg: "{{ find_result.results | map(attribute='files') | sum(start=[]) | map(attribute='path') | list }}"` and running a loop on this on other task will also work. – Zeitounator May 30 '20 at 00:01
  • 1
    just fyi - maybe someone else is interested - i did file a report with the guys at ansible and got an answer why it is behaving like this - https://github.com/ansible/ansible/issues/69777#issuecomment-637610175 – user13641697 Oct 20 '20 at 17:00

0 Answers0