Is there a way to hide/stop some output generated by debug - ansible

Looking for help to understand why I have the additional output from the debug line below:
- name: Check kernel diff
become: true
shell: "sdiff {{ item }}/pre-kernel.out {{ item }}/post-kernel.out | grep '|' | awk -F'|' '{print $2}' | xargs"
register: "kernel"
with_items: "{{work_dir}}"
- debug:
msg: "The system is now running Kernel Version {{ item.stdout }}"
when: "{{ item.changed == true }}"
with_items: "{{ kernel.results}}"
The output from msg is correct, but how can I stop/hide all the detail that appears before it:
TASK [patching : debug] ************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ item.changed == true }}
ok: [test02] => (item={'stderr_lines': [], 'ansible_loop_var': u'item', u'end': u'2020-05-26 16:23:37.068718', u'stderr': u'', u'stdout': u'4.14.35-1902.302.2.el7uek.x86_64', u'changed': True, 'failed': False, u'delta': u'0:00:00.008894', u'cmd': u"sdiff /var/tmp/patching_2020-05-26/pre-kernel.out /var/tmp/patching_2020-05-26/post-kernel.out | grep '|' | awk -F'|' '{print $2}' | xargs", 'item': u'/var/tmp/patching_2020-05-26', u'rc': 0, u'invocation': {u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'strip_empty_ends': True, u'_raw_params': u"sdiff /var/tmp/patching_2020-05-26/pre-kernel.out /var/tmp/patching_2020-05-26/post-kernel.out | grep '|' | awk -F'|' '{print $2}' | xargs", u'removes': None, u'argv': None, u'creates': None, u'chdir': None, u'stdin_add_newline': True, u'stdin': None}}, 'stdout_lines': [u'4.14.35-1902.302.2.el7uek.x86_64'], u'start': u'2020-05-26 16:23:37.059824'}) => {
"msg": "The system is now running Kernel Version 4.14.35-1902.302.2.el7uek.x86_64"
}
So it looks more like just:
TASK [patching : debug] ************************
ok: [test02] => {
"msg": "The system is now running Kernel Version 4.14.35-1902.302.2.el7uek.x86_64"
}
var_file.yml for reference:
---
work_dir:
- /var/tmp/patching_{{ansible_date_time.date}}
I've been using this as guidance.

TL;DR;
Based on your exact example, this should be, your debug task:
- debug:
msg: "The system is now running Kernel Version {{ item.stdout }}"
loop: "{{ kernel.results }}"
when: item.changed
loop_control:
label: 'kernel'
Which will output
TASK [debug] *******************************************************************
ok: [local] => (item=kernel) => {
"msg": "The system is now running Kernel Version 4.14.35-1902.302.2.el7uek.x86_64"
}
You can't reduce it totally, as you want but you can indeed make loops less annoyingly verbose by defining a label in the loop_control attribute.
In this label you can select one of the attributes of your dictionary to be displayed when Ansible loops over it rather than the full dictionary.
- debug:
msg: "The system is now running Kernel Version {{ item.stdout }}"
loop:
- some: foo
dict: foo
with: foo
an: foo
annoyingly: foo
long: foo
list: foo
of: foo
attributes: foo
name: bar
stdout: bar
loop_control:
label: "{{ item.name }}"
This will give a recap looking like this
TASK [debug] *******************************************************************
ok: [local] => (item=bar) => {
"msg": "The system is now running Kernel Version bar"
}
Versus the cluttered one without loop_control:
TASK [debug] *******************************************************************
ok: [local] => (item={'some': 'foo', 'dict': 'foo', 'with': 'foo', 'an': 'foo', 'annoyingly': 'foo', 'long': 'foo', 'list': 'foo', 'of': 'foo', 'attributes': 'foo', 'name': 'bar', 'stdout': 'bar'}) => {
"msg": "The system is now running Kernel Version bar"
}
Also, in order to fix your warning, you just need to remove the Jinja curly bracers, as when always assume you are going to pass a Jinja expression.
And testing that something == true is an uneeded extra verbosity too, when: something-that-evaluates-to-true is enough.
So you should use
when: item.changed
Instead of your actual
when: "{{ item.changed == true }}"
PS: since Ansible 2.5 loop should be preferred rather than with_items.

without "loop_control" ansible code
- name: 'Print message if CPU utilization become abnormal'
debug:
msg:
- -------------------------------------------------------
- CPU Utilization = ( 100 - idle time ) = "{{ item.stdout }}"% is idle
- -------------------------------------------------------
#loop:
with_items:
- "{{ incidentcpuworknote_cpu }}"
- "{{ incidentcpuworknote_cpu1 }}"
- "{{ incidentcpuworknote_cpu2 }}"
when: item.stdout| int <= 10
Below is the ansible execution result before using the loop_control
TASK [Print message if CPU utilization become abnormal] ******************************************************************************************
ok: [ansiblenode] => (item={u'stderr_lines': [], u'cmd': u"mpstat -u 1 3|tail -1| awk '{print $NF}'\n", u'end': u'2021-04-03 03:37:38.417169', u'stdout': u'0.00', u'changed': True, u'failed': False, u'delta': u'0:00:03.259453', u'stderr': u'', u'rc': 0, u'stdout_lines': [u'0.00'], u'start': u'2021-04-03 03:37:35.157716'}) =>
msg:
- '-------------------------------------------------------'
- CPU Utilization = ( 100 - idle time ) = "0.00"% is idle
- '-------------------------------------------------------
with "loop_control" ansible code
Note: "loop_control" helped to avoid ansible debug messages while executing the loop
- name: 'Print message if CPU utilization become abnormal'
debug:
msg:
- -------------------------------------------------------
- CPU Utilization = ( 100 - idle time ) = "{{ item.stdout }}"% is idle
- -------------------------------------------------------
#loop:
with_items:
- "{{ incidentcpuworknote_cpu }}"
- "{{ incidentcpuworknote_cpu1 }}"
- "{{ incidentcpuworknote_cpu2 }}"
when: item.stdout| int <= 10
loop_control:
label: "{{ item.stdout }}"
Below is the ansible execution result after using the loop_control
TASK [Print message if CPU utilization become abnormal] *****************************************************
ok: [ansiblenode] => (item=0.00) =>
msg:
- '-------------------------------------------------------'
- CPU Utilization = ( 100 - idle time ) = "0.00"% is idle
- '-------------------------------------------------------'

Related

Unicode error while increment variable value

When I work with static variables it works absolutely fine. But when I try to use dynamic it does not work.
The playbook:
---
- hosts: Swi1
vars:
NewOne: 0
provider:
host: "192.168.0.30"
transport: "cli"
username: "cisco"
password: "cisco"
tasks:
- name: gather facts
register: iosfacts
ios_facts:
provider: "{{ provider }}"
- name: Display the value of the counter
debug:
msg: "NewOne={{ NewOne }} / Data type={{ NewOne | type_debug }}"
- name: interface description
set_fact:
NewOne: " {{ NewOne + 1 }}"
parents: "interface {{ item.key }}"
with_dict: "{{ iosfacts.ansible_facts.ansible_net_interfaces }}"
when: item.value.operstatus == "up"
- debug:
msg: " This is Debug {{ NewOne }}"
Gives the error:
fatal: [Swi1]: FAILED! => {"msg": "Unexpected templating type error
occurred on ({{ NewOne + 1 }}): coercing to Unicode: need string or
buffer, int found"}
If you want to do an increment on a variable, you need to recast it as an int, as set_fact will always make you end up with a string.
As an example, the two tasks:
- set_fact:
NewOne: "{{ NewOne | d(0) + 1 }}"
- debug:
var: NewOne | type_debug
Are giving
TASK [set_fact] ***************************************************************
ok: [localhost]
TASK [debug] ******************************************************************
ok: [localhost] =>
NewOne | type_debug: str
The fix is, then, to use the int filter.
Given:
- set_fact:
NewOne: "{{ NewOne | d(0) | int + 1 }}"
loop: "{{ range(1, 4) }}"
- debug:
var: NewOne
This yields the expected
TASK [set_fact] ***************************************************************
ok: [localhost] => (item=1)
ok: [localhost] => (item=2)
ok: [localhost] => (item=3)
TASK [debug] ******************************************************************
ok: [localhost] =>
NewOne: '3'
But then with your use case, there are more elaborated and shorter way to achieve the same:
- set_fact:
NewOne: >-
{{
iosfacts
.ansible_facts
.ansible_net_interfaces
| selectattr('value.operstatus', '==', 'up')
| length
}}
Given:
- debug:
msg: >-
{{
iosfacts
.ansible_facts
.ansible_net_interfaces
| selectattr('value.operstatus', '==', 'up')
| length
}}
vars:
iosfacts:
ansible_facts:
ansible_net_interfaces:
- value:
operstatus: up
- value:
operstatus: down
- value:
operstatus: up
This yields:
ok: [localhost] =>
msg: '2'
It seems you are trying to implement a loop counter with a programming paradigm, which isn't plain possible in that way since Ansible is not a programming language but a Configuration Management Tool in which you declare a state.
Your current issue is reproducible in the following way:
---
- hosts: localhost
become: false
gather_facts: false
vars:
NewOne: 0
tasks:
- name: Show var
debug:
msg: "{{ NewOne | type_debug }}"
- name: Add value
set_fact:
NewOne: " {{ NewOne + 1 }}"
loop: [1, 2, 3]
- name: Show result
debug:
msg: "{{ NewOne }}
resulting into an output of
TASK [Add value] *************
ok: [localhost] => (item=1)
fatal: [localhost]: FAILED! =>
msg: 'Unexpected templating type error occurred on ( {{ NewOne + 1 }}): coercing to Unicode: need string or buffer, int found'
Possible Solutions
You may have a look into Migrating from with_X to loop and Extended loop variables as an iteration counter is already provided there.
An other approach is given via type casting with filter in the answer of #β.εηοιτ.βε.
There as well if you are just interested in the amount of occurrences of certain status, like interface status up or down.
Further Q&A
Ansible set_fact type cast
Further Documentation
Discovering the data type
Forcing the data type

Ansible debug output [duplicate]

Looking for help to understand why I have the additional output from the debug line below:
- name: Check kernel diff
become: true
shell: "sdiff {{ item }}/pre-kernel.out {{ item }}/post-kernel.out | grep '|' | awk -F'|' '{print $2}' | xargs"
register: "kernel"
with_items: "{{work_dir}}"
- debug:
msg: "The system is now running Kernel Version {{ item.stdout }}"
when: "{{ item.changed == true }}"
with_items: "{{ kernel.results}}"
The output from msg is correct, but how can I stop/hide all the detail that appears before it:
TASK [patching : debug] ************************
[WARNING]: conditional statements should not include jinja2 templating delimiters such as {{ }} or {% %}. Found: {{ item.changed == true }}
ok: [test02] => (item={'stderr_lines': [], 'ansible_loop_var': u'item', u'end': u'2020-05-26 16:23:37.068718', u'stderr': u'', u'stdout': u'4.14.35-1902.302.2.el7uek.x86_64', u'changed': True, 'failed': False, u'delta': u'0:00:00.008894', u'cmd': u"sdiff /var/tmp/patching_2020-05-26/pre-kernel.out /var/tmp/patching_2020-05-26/post-kernel.out | grep '|' | awk -F'|' '{print $2}' | xargs", 'item': u'/var/tmp/patching_2020-05-26', u'rc': 0, u'invocation': {u'module_args': {u'warn': True, u'executable': None, u'_uses_shell': True, u'strip_empty_ends': True, u'_raw_params': u"sdiff /var/tmp/patching_2020-05-26/pre-kernel.out /var/tmp/patching_2020-05-26/post-kernel.out | grep '|' | awk -F'|' '{print $2}' | xargs", u'removes': None, u'argv': None, u'creates': None, u'chdir': None, u'stdin_add_newline': True, u'stdin': None}}, 'stdout_lines': [u'4.14.35-1902.302.2.el7uek.x86_64'], u'start': u'2020-05-26 16:23:37.059824'}) => {
"msg": "The system is now running Kernel Version 4.14.35-1902.302.2.el7uek.x86_64"
}
So it looks more like just:
TASK [patching : debug] ************************
ok: [test02] => {
"msg": "The system is now running Kernel Version 4.14.35-1902.302.2.el7uek.x86_64"
}
var_file.yml for reference:
---
work_dir:
- /var/tmp/patching_{{ansible_date_time.date}}
I've been using this as guidance.
TL;DR;
Based on your exact example, this should be, your debug task:
- debug:
msg: "The system is now running Kernel Version {{ item.stdout }}"
loop: "{{ kernel.results }}"
when: item.changed
loop_control:
label: 'kernel'
Which will output
TASK [debug] *******************************************************************
ok: [local] => (item=kernel) => {
"msg": "The system is now running Kernel Version 4.14.35-1902.302.2.el7uek.x86_64"
}
You can't reduce it totally, as you want but you can indeed make loops less annoyingly verbose by defining a label in the loop_control attribute.
In this label you can select one of the attributes of your dictionary to be displayed when Ansible loops over it rather than the full dictionary.
- debug:
msg: "The system is now running Kernel Version {{ item.stdout }}"
loop:
- some: foo
dict: foo
with: foo
an: foo
annoyingly: foo
long: foo
list: foo
of: foo
attributes: foo
name: bar
stdout: bar
loop_control:
label: "{{ item.name }}"
This will give a recap looking like this
TASK [debug] *******************************************************************
ok: [local] => (item=bar) => {
"msg": "The system is now running Kernel Version bar"
}
Versus the cluttered one without loop_control:
TASK [debug] *******************************************************************
ok: [local] => (item={'some': 'foo', 'dict': 'foo', 'with': 'foo', 'an': 'foo', 'annoyingly': 'foo', 'long': 'foo', 'list': 'foo', 'of': 'foo', 'attributes': 'foo', 'name': 'bar', 'stdout': 'bar'}) => {
"msg": "The system is now running Kernel Version bar"
}
Also, in order to fix your warning, you just need to remove the Jinja curly bracers, as when always assume you are going to pass a Jinja expression.
And testing that something == true is an uneeded extra verbosity too, when: something-that-evaluates-to-true is enough.
So you should use
when: item.changed
Instead of your actual
when: "{{ item.changed == true }}"
PS: since Ansible 2.5 loop should be preferred rather than with_items.
without "loop_control" ansible code
- name: 'Print message if CPU utilization become abnormal'
debug:
msg:
- -------------------------------------------------------
- CPU Utilization = ( 100 - idle time ) = "{{ item.stdout }}"% is idle
- -------------------------------------------------------
#loop:
with_items:
- "{{ incidentcpuworknote_cpu }}"
- "{{ incidentcpuworknote_cpu1 }}"
- "{{ incidentcpuworknote_cpu2 }}"
when: item.stdout| int <= 10
Below is the ansible execution result before using the loop_control
TASK [Print message if CPU utilization become abnormal] ******************************************************************************************
ok: [ansiblenode] => (item={u'stderr_lines': [], u'cmd': u"mpstat -u 1 3|tail -1| awk '{print $NF}'\n", u'end': u'2021-04-03 03:37:38.417169', u'stdout': u'0.00', u'changed': True, u'failed': False, u'delta': u'0:00:03.259453', u'stderr': u'', u'rc': 0, u'stdout_lines': [u'0.00'], u'start': u'2021-04-03 03:37:35.157716'}) =>
msg:
- '-------------------------------------------------------'
- CPU Utilization = ( 100 - idle time ) = "0.00"% is idle
- '-------------------------------------------------------
with "loop_control" ansible code
Note: "loop_control" helped to avoid ansible debug messages while executing the loop
- name: 'Print message if CPU utilization become abnormal'
debug:
msg:
- -------------------------------------------------------
- CPU Utilization = ( 100 - idle time ) = "{{ item.stdout }}"% is idle
- -------------------------------------------------------
#loop:
with_items:
- "{{ incidentcpuworknote_cpu }}"
- "{{ incidentcpuworknote_cpu1 }}"
- "{{ incidentcpuworknote_cpu2 }}"
when: item.stdout| int <= 10
loop_control:
label: "{{ item.stdout }}"
Below is the ansible execution result after using the loop_control
TASK [Print message if CPU utilization become abnormal] *****************************************************
ok: [ansiblenode] => (item=0.00) =>
msg:
- '-------------------------------------------------------'
- CPU Utilization = ( 100 - idle time ) = "0.00"% is idle
- '-------------------------------------------------------'

Ansible sum register value

How to get the sum of two hosts with Jinja2 filtering ansible
host1 and host 2
---
- name: Count Check
hosts: MYGROUP
gather_facts: true
user: sv_admin
tasks:
- name: count check
shell: cat /etc/hosts | wc -l
register: command_result
- debug:
var: command_result.stdout
- set_fact:
total_result: "{{ command_result.stdout | map('int') | sum(start=0) }}"
- debug:
msg: "Total count: {{ total_result }}"
Playbook Output
TASK [debug] *****************************************************************
ok: [Host-01] => {
"msg": "Total count: 134"
}
ok: [Host-02] => {
"msg": "Total count: 133"
}
Use extract and sum. For example, the playbook below
shell> cat playbook.yml
- hosts: test_01:test_03
gather_facts: false
tasks:
- shell: cat /etc/hosts | wc -l
register: command_result
- debug:
var: command_result.stdout
- set_fact:
total_result: "{{ ansible_play_hosts_all|
map('extract', hostvars, ['command_result', 'stdout'])|
map('int')|
sum }}"
run_once: true
- debug:
var: total_result
gives (abridged)
shell> ansible-playbook playbook.yml
PLAY [test_01:test_03] ****
TASK [shell] ****
changed: [test_01]
changed: [test_03]
TASK [debug] ****
ok: [test_01] => {
"command_result.stdout": " 62"
}
ok: [test_03] => {
"command_result.stdout": " 31"
}
TASK [set_fact] ****
ok: [test_01]
TASK [debug] ****
ok: [test_03] => {
"total_result": "93"
}
ok: [test_01] => {
"total_result": "93"
}
See serial
See the difference between ansible_play_hosts and ansible_play_hosts_all
You can use custom stats to do that: https://docs.ansible.com/ansible/latest/modules/set_stats_module.html
So for your case it would look like
---
- name: Count Check
hosts: MYGROUP
gather_facts: true
user: sv_admin
tasks:
- name: count check
shell: cat /etc/hosts | wc -l
register: command_result
- debug:
var: command_result.stdout
- set_fact:
host_result: "{{ command_result.stdout }}"
- debug:
msg: "Count for this host: {{ host_result }}"
- set_stats:
data: "{{ { 'total_count': host_result | int } }}"
Then if you run it with ANSIBLE_SHOW_CUSTOM_STATS=yes it will show you the result at the end:
$ ANSIBLE_SHOW_CUSTOM_STATS=yes ansible-playbook -i inventory pb.yml
... (usual output)
CUSTOM STATS: *************************************************************
RUN: { "total_count": 267}
The set_stats task adds results together from all the hosts by default, which is what you are looking for. You need to make sure the values are integers though, because if they are strings it will just concatenate them and you will end up with something like RUN: { "total_count": "134133"}. That's why I have put the data: bit the way I have - if you try to create the dictionary in regular yaml, like
data:
total_count: "{{ host_result | int }}"
you will see that the value is still a string (due to the way yaml/jinja works) and it won't work properly.

Conditional set_fact doesn't evaluate

I'm looking to set a variable based on the outcome of the following evaluation: ({{ found_files.files|length > 0 }}), however, it doesn't appear to be evaluated fully. What could be the issue?
Desired outcome:found_files2: /dev/null
Outcome:
TASK [debug] ********************************************************************************************************************************************************************************
ok: [localhost] => {
"found_files2": "{'files': [], 'changed': False, 'msg': '', 'matched': 0, 'examined': 0, 'failed': False} if (False) else '/dev/null'"
}
Playbook:
---
- hosts: localhost
gather_facts: no
vars:
backup_dir: /home/user1/projects/ansible/modules/backups/router2
tasks:
- name: get files in backups/<router name>/
delegate_to: localhost
find:
paths: "{{ backup_dir }}"
register: found_files
- set_fact:
found_files2: "{{ found_files }} if ({{ found_files.files|length > 0 }}) else '/dev/null'"
- debug: var=found_files2
The correct syntax is below
- set_fact:
found_files2: "{{ found_files if (found_files.files|length > 0) else '/dev/null' }}"
The filter ternary gives the same result
- set_fact:
found_files2: "{{ (found_files.files|length > 0)|ternary(found_files, '/dev/null') }}"

Ansible, loop, register, and stdout

I have a playbook that looks like this:
- hosts: host1
gather_facts: false
tasks:
- name: "Loop"
command: "echo {{ item }}"
with_items: [ 0, 2, 4, 6, 8, 10 ]
register: hello
- debug: "msg={{ hello.results }}"
Everything works correctly, and the output is returned, but there is tons and tons of output. It turns out that this:
- debug: "msg={{ hello.results.1.stdout }}"
does exactly what I want -- just grab the stdout from the command -- but only for one of the six times through the loop.
What I really want/need to do is this:
- debug: "msg={{ hello.results.*.stdout }}"
where it goes into the hello structure, accesses the results entry, goes to each member of that array, and pulls out the stdout value.
Is this possible?
UPDATE
- hosts: host1
gather_facts: false
tasks:
- name: "Loop"
command: "echo {{ item }}"
with_items: [ 0, 2, 4, 6, 8, 10 ]
register: hello
- debug:
msg: "{{item.stdout}}"
with_items: "{{hello.results}}"
is no less verbose than my original example.
TASK [debug] *******************************************************************
ok: [host1] => (item={'_ansible_parsed': True, 'stderr_lines': [], u'cmd': [
u'echo', u'0'], u'end': u'2018-01-02 20:53:08.916774', '_ansible_no_log': False
, u'stdout': u'0', '_ansible_item_result': True, u'changed': True, 'item': 0,
u'delta': u'0:00:00.002137', u'stderr': u'', u'rc': 0, u'invocation': {u'module_
args': {u'warn': True, u'executable': None, u'_uses_shell': False, u'_raw_params
': u'echo 0', u'removes': None, u'creates': None, u'chdir': None, u'stdin': Non
e}}, 'stdout_lines': [u'0'], u'start': u'2018-01-02 20:53:08.914637', 'failed':
False}) => {
"item": {
"changed": true,
"cmd": [
"echo",
"0"
],
"delta": "0:00:00.002137",
"end": "2018-01-02 20:53:08.916774",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "echo 0",
"_uses_shell": false,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": 0,
"rc": 0,
"start": "2018-01-02 20:53:08.914637",
"stderr": "",
"stderr_lines": [],
"stdout": "0",
"stdout_lines": [
"0"
]
},
"msg": "0"
}
I get 6 copies of the above construct.
It feels like I'm close but I'm still doing something wrong. I see "msg": "0" at the bottom, which is what I want. I just don't want the rest of it.
Solution:
- debug: "msg={{ hello.results | map(attribute='stdout') | join('\n') }}"
Remark:
By default, Ansible will print visible \n two-character sequences instead of wrapping the lines, so either use a callback plugin for a human readable output (example) or verify the method with:
- copy:
content: "{{ hello.results | map(attribute='stdout') | join('\n') }}"
dest: ./result.txt
and check the contents of the result.txt.
I have used the keyword loop to get stdout from all iterations of the previous loop:
loop: "{{ hello | json_query('results[*].stdout') }}"
I find json_query easiest to use in such register-loop situations. Official documentation can be found here ==> json-query-filter
Sure. The ansible website has documentation that explains how to use register in a loop. You just need to iterate over the hello.results array, as in:
- debug:
msg: "{{item.stdout}}"
with_items: "{{hello.results}}"
What about:
- debug: "msg={{ item.stdout }}"
with_items: "{{ hello.results }}"
I think this construct works well enough for my needs.
- hosts: localhost
gather_facts: false
vars:
stuff: [ 0,2,4,6,8,10 ]
tasks:
- name: "Loop"
command: "echo {{ item }}"
with_items: "{{ stuff }}"
register: hello
- debug: "var=hello.results.{{item}}.stdout"
with_sequence: "0-{{stuff|length - 1}}"
I was looking at a similar problem and was confused by getting lots of output when I was expecting a relatively small msg or var from debug:. Turns out most of that output was the 'label' with which Ansible was prefixing each of those small outputs. It being a few years after this question was originally asked I've been using loop rather than with_items; this also has a label: option in loop_control:, so in my case for a similar problem - getting any /etc/passwd entries for users 'alice' or 'bob',
- hosts: all
gather_facts: false
serial: 1 # output easier to read when grouped by host
tasks:
- name: Look for users in /etc/passwd
command: grep {{ item }} /etc/passwd
register: res
ignore_errors: true
loop:
- alice
- bob
- debug:
msg: "{{ item.stdout_lines }}"
when: not item.failed
loop: "{{ res.results }}"
loop_control:
label: "{{ item.item }}"

Resources