0

I am trying to get my playbook to print readable debug output for stat on a number of directories when executing. The below shows the with_items and loop way and both work but the real problem is the output of Ansible itself printing the entire dictionary or list of dictionaries for stat with each task, eg:

ok: {hostname} => "multi-line spanning string of more information than wanted"

The loop version is slightly more readable in that in prints a longer list of dicts only once, but requires modification every time I add a new directory to mydirdict. with_items does not, but it prints the stat dict for each item in mydirdict each time and is far less legible.

I see that under other circumstances, having this print is useful but not in this case. Is there a way to control this?

vars:
  mydirdict:
    - { id: 1, name: /tmp/foo }
    - { id: 2, name: /tmp/bar }
tasks:
  - name: check dirs
    stat: path="{{ item.name }}"
    with_items: "{{ mydirdict }}"
    register: stats

  - name: check dir stat debug
    debug:
      msg:
       - "path {{ item.item.name }} exists? {{ item.stat.exists }}"
    with_items:
      - "{{ stats.results }}"

  - name: check dir stat debug
    debug:
      msg:
        - "path {{ item[0].item.name }} exists? {{ item[0].stat.exists }}"
        - "path {{ item[1].item.name }} exists? {{ item[1].stat.exists }}"
    loop:
      - "{{ stats.results }}"
4
  • See Limiting loop output with label. Note: your use of loop is wrong as you should not put your list inside a list (i.e. loop: "{{ stats.results }}") in which case you will get the exact same result as with_item. Including the list in a list "works" with with_item as this lookup automatically flattens what it receives as a parameter. Commented Feb 3, 2023 at 23:31
  • Thank you. With respect to Python, I would understand what you mean but how else would I pass this list in Ansible than the above?
    – chipfall
    Commented Feb 4, 2023 at 17:57
  • Exactly as I showed you in my above comment. To say things a bit differently, with_item: "{{ some_possibly_leveled_list }}" is identical to loop: "{{ some_possibly_leveled_list | flatten }}". Commented Feb 4, 2023 at 17:59
  • I see my mistake. Thank you
    – chipfall
    Commented Feb 4, 2023 at 18:12

1 Answer 1

1

Answering my own question based on understanding after Zeitounator pointed out my mistake. First off, I made a mistake that was hard to see even though I read the loop documentation. I was effectively doing this:

- debug:
    msg: "{{ item }}"
  loop:
    - "{{ [1,2,3] }}"

Which is different than both:

- debug:
    msg: "{{ item }}"
  loop: "{{ [1,2,3] }}"

- debug:
    msg: "{{ item }}"
  loop:
    - 1
    - 2
    - 3

But the real answer was adding a label as suggested:

- name: check dir stat debug
  debug:
    msg:
      - "path {{ item.item.name }} exists? {{ item.stat.exists }}"
  loop: "{{ stats.results | flatten(1) }}"
  loop_control:
    label: "{{ item.item }}"

which gives me just the list I started with on stdout and the message. I had mistakenly though label would mean supplying only part of the data structure but really it is about the stdout display only.

Lastly, there is some control one can have on the deeper parts of ansible output with the community.ansible.diy callback module as applied to Ansible config but not through a playbook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.