Bootstrap FreeKB - Ansible - Suppress output using no_log and loop_control
Ansible - Suppress output using no_log and loop_control

Updated:   |  Ansible articles

There will inevitably be scenarios where you are looping through JSON or a dictionary that contains many, many key value pairs. The output can be vast, making it difficult to display only the data you care about.

Fortunately, no_log or loop_control and label can be used to suppress output. 

For example, let's say vars is used to define the "food" dictionary and debug is used to loop through the "food" dictionary.

---
- hosts: localhost
  vars:
    food: 
      - { key: 'fruit', value: 'banana' }
      - { key: 'fruit', value: 'orange' }
      - { key: 'veggy', value: 'onion' }
      - { key: 'veggy', value: 'pepper' }
  tasks:
  - debug:
      msg: "{{ item.value }}"
    with_items: "{{ food }}"
...

 

Which should return the following. Imagine this was instead of dictionary with thousands of key value pairs!

ok: [localhost] => (item={u'value': u'apple', u'key': u'fruit'}) => {
    "msg": "apple"
}
ok: [localhost] => (item={u'value': u'banana', u'key': u'fruit'}) => {
    "msg": "banana"
}
ok: [localhost] => (item={u'value': u'onion', u'key': u'veggy'}) => {
    "msg": "onion"
}
ok: [localhost] => (item={u'value': u'pepper', u'key': u'veggy'}) => {
    "msg": "pepper"
}

 

no_log can be used to suppress the output.

---
- hosts: localhost
  vars:
    food: 
      - { key: 'fruit', value: 'banana' }
      - { key: 'fruit', value: 'orange' }
      - { key: 'veggy', value: 'onion' }
      - { key: 'veggy', value: 'pepper' }
  tasks:
  - debug:
      msg: "{{ item.value }}"
    with_items: "{{ food }}"
    no_log: true
...

 

Which should return the following. However, I almost never use no_log because then it will be impossible to debug since "None" will be returned.

ok: [localhost] => (item=None)
ok: [localhost] => (item=None)
ok: [localhost] => (item=None)
ok: [localhost] => (item=None)

 

loop_control and label can be used to limit the output to only the item being parsed.

---
- hosts: localhost
  vars:
    food:
      - { key: 'fruit', value: 'apple' }
      - { key: 'fruit', value: 'banana' }
      - { key: 'veggy', value: 'onion' }
      - { key: 'veggy', value: 'pepper' }
  tasks:
  - debug:
      msg: "{{ item.value }}"
    with_items: "{{ food }}"
    loop_control:
      label: "{{ item.value }}"
...

 

Which should return the following. Now you still get some valuable data being returned that can assist when debugging.

ok: [localhost] => (item=apple) => {
    "msg": "apple"
}
ok: [localhost] => (item=banana) => {
    "msg": "banana"
}
ok: [localhost] => (item=onion) => {
    "msg": "onion"
}
ok: [localhost] => (item=pepper) => {
    "msg": "pepper"
}



Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


January 09 2023 by sanjay
loop_control works fine when using with_items but how do we make it work without that. for an instance, user creation (multiple users hence loop) - name: user creation user: name: user1 uid: password: group: loop: no_log: true We need to be able to see the output for user creation with groups, name etc. but password should not be visible. Note: No_log is masking entire terminal output Any inputs are very helpful. thank you.

September 21 2023 by J
Nope. It doesn't work properly. I have a loop with label set, like: with_items: "{{ _checkstatus.results }}" loop_control: label: "SomeLabel" and i still get the whole item being dumped: TASK [shutdown-restart : Report service check results:] ************************************************************************************************************************************************************************************* ok: [MYHOST] => (item=SomeLabel.) => { "ansible_loop_var": "item", "failed_when_result": false, "item": { <SNIP many many lines of useless output of item vars i dont want to see> Like almost everything in ansible, it is a useless, broken hack, that promises the world and then fails to deliver even a bare acceptable minimum.

Add a Comment


Please enter 5899cd in the box below so that we can be sure you are a human.