<p>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:</p>
<pre><code>- debug:
    msg: &quot;{{ item }}&quot;
  loop:
    - &quot;{{ [1,2,3] }}&quot;
</code></pre>
<p>Which is different than both:</p>
<pre><code>- debug:
    msg: &quot;{{ item }}&quot;
  loop: &quot;{{ [1,2,3] }}&quot;

- debug:
    msg: &quot;{{ item }}&quot;
  loop:
    - 1
    - 2
    - 3
</code></pre>
<p>But the real answer was adding a label as suggested:</p>
<pre><code>- name: check dir stat debug
  debug:
    msg:
      - &quot;path {{ item.item.name }} exists? {{ item.stat.exists }}&quot;
  loop: &quot;{{ stats.results | flatten(1) }}&quot;
  loop_control:
    label: &quot;{{ item.item }}&quot;
</code></pre>
<p>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.</p>
<p>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.</p>

CC BY-SA 4.0
2023-02-04T21:30:27.197Z
2023-02-04T21:30:27.197Z
chipfall
<p>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 <strong>with_items</strong> and <strong>loop</strong> 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:</p>
<p><strong>ok: {hostname} =&gt; &quot;multi-line spanning string of more information than wanted&quot;</strong></p>
<p>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.</p>
<p>I see that under other circumstances, having this print is useful but not in this case. Is there a way to control this?</p>
<pre><code>vars:
  mydirdict:
    - { id: 1, name: /tmp/foo }
    - { id: 2, name: /tmp/bar }
tasks:
  - name: check dirs
    stat: path=&quot;{{ item.name }}&quot;
    with_items: &quot;{{ mydirdict }}&quot;
    register: stats

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

  - name: check dir stat debug
    debug:
      msg:
        - &quot;path {{ item[0].item.name }} exists? {{ item[0].stat.exists }}&quot;
        - &quot;path {{ item[1].item.name }} exists? {{ item[1].stat.exists }}&quot;
    loop:
      - &quot;{{ stats.results }}&quot;
</code></pre>

CC BY-SA 4.0
2023-02-03T23:31:01.607Z
See [Limiting loop output with label](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html#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.
Zeitounator
CC BY-SA 4.0
2023-02-04T17:57:17.027Z
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
CC BY-SA 4.0
2023-02-04T17:59:09.560Z
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 }}"`.
Zeitounator
CC BY-SA 4.0
2023-02-04T18:12:39.087Z
I see my mistake. Thank you
chipfall
<ansible>
How to Control Ansible Loop or Task Default Output
chipfall
# How to Control Ansible Loop or Task Default Output

(Asked by: username_0 on 2023-02-03)

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 }}"

**Tags:** ansible

> **Comments:**
> **username_1**: See [Limiting loop output with label](https://docs.ansible.com/ansible/latest/playbook_guide/playbooks_loops.html#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.  
> **username_0**: 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?  
> **username_1**: 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 }}"`.  
> **username_0**: I see my mistake. Thank you  

---

## 1 answers

### Answer by username_0 on 2023-02-04. Score: 1

Answering my own question based on understanding after username_1 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.


---

