Ansible display the contents of a variable - ansible

If I have these 2 task
- name: Replace ServerIP in config_file on OTHER NODES
set_fact:
variable: "{{hostvars.localhost.new_ips.results}}"
- name: Display variable
debug: var=variable
The result of which are:
TASK: [Display variable] *********************************************************
ok: [vm2] => {
"variable": [
{
"changed": true,
"cmd": "echo \"11.11.4.74\"",
"delta": "0:00:00.002244",
"end": "2014-08-26 02:34:22.880447",
"invocation": {
"module_args": "echo \"11.11.4.74\"",
"module_name": "shell"
},
"item": "74",
"rc": 0,
"start": "2014-08-26 02:34:22.878203",
"stderr": "",
"stdout": "11.11.4.74"
},
{
"changed": true,
"cmd": "echo \"11.11.4.138\"",
"delta": "0:00:00.002156",
"end": "2014-08-26 02:34:22.958337",
"invocation": {
"module_args": "echo \"11.11.4.138\"",
"module_name": "shell"
},
"item": "138",
"rc": 0,
"start": "2014-08-26 02:34:22.956181",
"stderr": "",
"stdout": "11.11.4.138"
}
]
}
ok: [vm1] => {
"variable": [
{
"changed": true,
"cmd": "echo \"11.11.4.74\"",
"delta": "0:00:00.002244",
"end": "2014-08-26 02:34:22.880447",
"invocation": {
"module_args": "echo \"11.11.4.74\"",
"module_name": "shell"
},
"item": "74",
"rc": 0,
"start": "2014-08-26 02:34:22.878203",
"stderr": "",
"stdout": "11.11.4.74"
},
{
"changed": true,
"cmd": "echo \"11.11.4.138\"",
"delta": "0:00:00.002156",
"end": "2014-08-26 02:34:22.958337",
"invocation": {
"module_args": "echo \"11.11.4.138\"",
"module_name": "shell"
},
"item": "138",
"rc": 0,
"start": "2014-08-26 02:34:22.956181",
"stderr": "",
"stdout": "11.11.4.138"
}
]
}
Then how can I access the stdout part of variable only. Please note, I just require the stdout part of this variable i.e 11.11.4.74 and 11.11.4.138 (in a loop preferably)

You can either access it individually
{{ variable[0].stdout }}
and
{{ variable[1].stdout }}
OR use loop
- debug: var=item.stdout
with_items: variable

Related

Ansible parse debug output

I have this ansible output got from 3 different hosts, using ansible debug module:
TASK [debug] ***************************************************************************************************************************************************************************************
ok: [10.240.22.44] => {
"msg": {
"changed": true,
"msg": "All items completed",
"results": [
{
"_ansible_ignore_errors": true,
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "/opt/confluent/bin/nodefirmware smm1",
"delta": "0:00:00.128325",
"end": "2020-02-05 11:22:19.435049",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "/opt/confluent/bin/nodefirmware smm1",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": "10.240.18.20",
"rc": 0,
"start": "2020-02-05 11:22:19.306724",
"stderr": "",
"stderr_lines": [],
"stdout": "smm1: SMM: 1.10 (TESM14F)\nsmm1: PSOC: 0.7",
"stdout_lines": [
"smm1: SMM: 1.10 (TESM14F)",
"smm1: PSOC: 0.7"
]
},
{
"_ansible_ignore_errors": true,
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "/opt/confluent/bin/nodefirmware smm1",
"delta": "0:00:00.096292",
"end": "2020-02-05 11:22:22.847949",
"failed": false,
"invocation": {
"module_args": {
"_raw_params": "/opt/confluent/bin/nodefirmware smm1",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"stdin": null,
"warn": true
}
},
"item": "10.240.19.21",
"rc": 0,
"start": "2020-02-05 11:22:22.751657",
"stderr": "",
"stderr_lines": [],
"stdout": "smm1: SMM: 1.10 (TESM14F)\nsmm1: PSOC: 0.7",
"stdout_lines": [
"smm1: SMM: 1.10 (TESM14F)",
"smm1: PSOC: 0.7"
]
}
]
}
}
I'm trying to parse this output and display in the end, for each hosts, strictly the:
"stdout_lines": [
"smm1: SMM: 1.10 (TESM14F)",
"smm1: PSOC: 0.7"
The playbook that shows the above output is this:
- debug:
msg: "{{ smm_output }}"
when: "('primary' in default_hostname or 'Primary' in default_hostname)"
tags: ['ic', 'smm']
I tried with "{{ smm_output.stdout_lines }}" but says there's not such dict object
Any clues?
You are registering the output of a looping task. Quoting the documentation:
When you register a variable in a task with a loop, the registered variable contains a value for each item in the loop. The data structure placed in the variable during the loop will contain a results attribute, that is a list of all responses from the module. For a more in-depth example of how this works, see the Loops section on using register with a loop.
To debug each stdout_lines of your respective results in smm_output you can use the following task:
- name: debug result
debug:
var: item.stdout_lines
loop: "{{ smm_output.results }}"
Ref: https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html#registering-variables

How to run mutiple include tasks asynchronously in ansible?

I use 'include' and 'with_items' to looping over a block of tasks:
---
- name: main file
gather_facts: false
hosts: localhost
vars:
list1:
- { name: 'testuser1', groups: 'wheel', time: 10 }
- { name: 'testuser2', groups: 'root', time: 3 }
tasks:
- name: multiple tasks
include: multiple.yml item={{item}}
with_items: "{{ list1 }}"
and the multiple.yml is:
---
- name: time before
shell: date +"%H:%M:%S"
- name: run
shell: sleep {{ item.time }}
- name: time after
shell: date +"%H:%M:%S"
The task works well. However, the multiple.yml for each loop are executed serialisely:
TASK [time before] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:00.013440", "end": "2018-11-25 15:56:04.595098", "rc": 0, "start": "2018-11-25 15:56:04.581658", "stderr": "", "stderr_lines": [], "stdout": "15:56:04", "stdout_lines": ["15:56:04"]}
TASK [run] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "sleep 10", "delta": "0:00:10.013043", "end": "2018-11-25 15:56:14.859720", "rc": 0, "start": "2018-11-25 15:56:04.846677", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [time after] *******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:00.012656", "end": "2018-11-25 15:56:15.153993", "rc": 0, "start": "2018-11-25 15:56:15.141337", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:15"]}
TASK [time before] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:01.014217", "end": "2018-11-25 15:56:16.448480", "rc": 0, "start": "2018-11-25 15:56:15.434263", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:15"]}
TASK [run] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "sleep 3", "delta": "0:00:03.012509", "end": "2018-11-25 15:56:19.711891", "rc": 0, "start": "2018-11-25 15:56:16.699382", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [time after] *******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:01.013766", "end": "2018-11-25 15:56:20.973979", "rc": 0, "start": "2018-11-25 15:56:19.960213", "stderr": "", "stderr_lines": [], "stdout": "15:56:19", "stdout_lines": ["15:56:19"]}
I want to execute the multiple.yml of each loop asynchronously, so I tried to use async and poll in main file:
---
- name: main file
gather_facts: false
hosts: localhost
vars:
list1:
- { name: 'testuser1', groups: 'wheel', time: 10 }
- { name: 'testuser2', groups: 'root', time: 3 }
tasks:
- name: multiple tasks
include: multiple.yml item={{item}}
async: 20
poll: 0
with_items: "{{ list1 }}"
The expected result is:
TASK [time before] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:00.013440", "end": "2018-11-25 15:56:04.595098", "rc": 0, "start": "2018-11-25 15:56:04.581658", "stderr": "", "stderr_lines": [], "stdout": "15:56:04", "stdout_lines": ["15:56:04"]}
TASK [run] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "sleep 10", "delta": "0:00:10.013043", "end": "2018-11-25 15:56:14.859720", "rc": 0, "start": "2018-11-25 15:56:04.846677", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [time after] *******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:00.012656", "end": "2018-11-25 15:56:15.153993", "rc": 0, "start": "2018-11-25 15:56:15.141337", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:15"]}
TASK [time before] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:01.014217", "end": "2018-11-25 15:56:16.448480", "rc": 0, "start": "2018-11-25 15:56:15.434263", "stderr": "", "stderr_lines": [], "stdout": "15:56:15", "stdout_lines": ["15:56:04"]}
TASK [run] ******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "sleep 3", "delta": "0:00:03.012509", "end": "2018-11-25 15:56:19.711891", "rc": 0, "start": "2018-11-25 15:56:16.699382", "stderr": "", "stderr_lines": [], "stdout": "", "stdout_lines": []}
TASK [time after] *******************************************************************************************
changed: [localhost] => {"changed": true, "cmd": "date +\"%H:%M:%S\"", "delta": "0:00:01.013766", "end": "2018-11-25 15:56:20.973979", "rc": 0, "start": "2018-11-25 15:56:19.960213", "stderr": "", "stderr_lines": [], "stdout": "15:56:19", "stdout_lines": ["15:56:08"]}
But the result of running new main.yml is the same as before, and the multiple.yml of the two loops is not executed asynchronously. I don't kown what the problem is. Is there any advice?
That is a known issue and is marked as an "improvement," rather than a bug. Feel free to drop a comment or +1 or whatever into the issue, but I would not expect it to be resolved in the near future since that issue has been open since 2017.
Separately, it may interest you to know that the "inline variable" syntax has been deprecated in favor of using vars:, but even in that case you don't need to because with_items: automatically exposes item and thus it is implicitly in vars: for you. The deprecation is in the "Note" in the manual

How to Store and display the output of shell command run in loop using with_lines

Below is the playbook i have created. I am not getting the way to store and display the output od the shell command and pass the same output to other play having different hosts.
---
- hosts: localhost
tasks:
- name: "check the connectivity with username for all the LLC servers from hosts.new file"
shell: ssh dp794d#{{ item }} "date"
register: result
with_lines: cat "/home/capio/ansible/pmossWipm/day2/logs/ipAddress.txt"
- debug: var=result
Now how can i display the result of register variable "result". I tried using the debug, but it didn't worked.
Below is the output of var=result
TASK [debug] *******************************************************************
ok: [localhost] => {
"result": {
"changed": true,
"msg": "All items completed",
"results": [
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "ssh dp794d#130.6.50.131 \"date\"",
"delta": "0:00:00.237866",
"end": "2017-05-09 08:59:13.918581",
"invocation": {
"module_args": {
"_raw_params": "ssh dp794d#130.6.50.131 \"date\"",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"item": "130.6.50.131",
"rc": 0,
"start": "2017-05-09 08:59:13.680715",
"stderr": " _________________________________________________________________\n\n This system is restricted to ABC authorized users for business\n purposes. Unauthorized access is a violation of the law. This\n service may be monitored for administrative and security reasons.\n By proceeding, you consent to this monitoring.\n _________________________________________________________________\n\n ,
"stdout": "Tue May 9 08:59:13 EDT 2017",
"stdout_lines": [
"Tue May 9 08:59:13 EDT 2017"
],
"warnings": []
},
{
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"cmd": "ssh dp794d#130.6.50.132 \"date\"",
"delta": "0:00:00.245660",
"end": "2017-05-09 08:59:14.430728",
"invocation": {
"module_args": {
"_raw_params": "ssh dp794d#130.6.50.132 \"date\"",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"item": "130.6.50.132",
"rc": 0,
"start": "2017-05-09 08:59:14.185068",
"stderr": " _________________________________________________________________\n\n This system is restricted to ABC authorized users for business\n purposes. Unauthorized access is a violation of the law. This\n service may be monitored for administrative and security reasons.\n By proceeding, you consent to this monitoring.\n _________________________________________________________________\n\n [,
"stdout": "Tue May 9 08:59:14 EDT 2017",
"stdout_lines": [
"Tue May 9 08:59:14 EDT 2017"
],
"warnings": []
}
]
}
}
You can store result to file as follows:
- copy:
content: '{{ result.results | map(attribute="stdout") | list | join("\n") }}'
dest: /tmp/out.txt

How to filter unmatched value from ansible variable

I have recently upgraded ansible version from 1.7 to 2.0. After upgrade with_nested loop unable to register only matching records in ansible variable. The same is working in ansible 1.7.2
Here is example playbook:
---
- hosts: 127.0.0.1
connection: local
tasks:
- shell: "echo {{ item[0] }}"
with_nested:
- [{"host": "host1","description": "This is host1 server"}, {"host": "host2","description": "This is host2 server"}]
- [{"host_name": "host1"},{"host_name": "host2"},{"host_name": "host3"},{"host_name":"host4"}]
register: all_hosts
when: item[0].host == item[1].host_name
- debug: var=all_hosts
Output:
PLAY ***************************************************************************
TASK [setup] *******************************************************************
ok: [127.0.0.1]
TASK [command] *****************************************************************
changed: [127.0.0.1] => (item=[{u'host': u'host1', u'description': u'This is host1 server'}, {u'host_name': u'host1'}])
skipping: [127.0.0.1] => (item=[{u'host': u'host1', u'description': u'This is host1 server'}, {u'host_name': u'host2'}])
skipping: [127.0.0.1] => (item=[{u'host': u'host1', u'description': u'This is host1 server'}, {u'host_name': u'host3'}])
skipping: [127.0.0.1] => (item=[{u'host': u'host1', u'description': u'This is host1 server'}, {u'host_name': u'host4'}])
skipping: [127.0.0.1] => (item=[{u'host': u'host2', u'description': u'This is host2 server'}, {u'host_name': u'host1'}])
changed: [127.0.0.1] => (item=[{u'host': u'host2', u'description': u'This is host2 server'}, {u'host_name': u'host2'}])
skipping: [127.0.0.1] => (item=[{u'host': u'host2', u'description': u'This is host2 server'}, {u'host_name': u'host3'}])
skipping: [127.0.0.1] => (item=[{u'host': u'host2', u'description': u'This is host2 server'}, {u'host_name': u'host4'}])
TASK [debug] *******************************************************************
ok: [127.0.0.1] => {
"all_hosts": {
"changed": true,
"msg": "All items completed",
"results": [
{
"_ansible_no_log": false,
"changed": true,
"cmd": "echo {u'host': u'host1', u'description': u'This is host1 server'}",
"delta": "0:00:00.005613",
"end": "2016-04-05 04:09:05.269437",
"invocation": {
"module_args": {
"_raw_params": "echo {u'host': u'host1', u'description': u'This is host1 server'}",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"item": [
{
"description": "This is host1 server",
"host": "host1"
},
{
"host_name": "host1"
}
],
"rc": 0,
"start": "2016-04-05 04:09:05.263824",
"stderr": "",
"stdout": "{uhost: uhost1, udescription: uThis is host1 server}",
"stdout_lines": [
"{uhost: uhost1, udescription: uThis is host1 server}"
],
"warnings": []
},
{
"_ansible_no_log": false,
"changed": false,
"item": [
{
"description": "This is host1 server",
"host": "host1"
},
{
"host_name": "host2"
}
],
"skip_reason": "Conditional check failed",
"skipped": true
},
{
"_ansible_no_log": false,
"changed": false,
"item": [
{
"description": "This is host1 server",
"host": "host1"
},
{
"host_name": "host3"
}
],
"skip_reason": "Conditional check failed",
"skipped": true
},
{
"_ansible_no_log": false,
"changed": false,
"item": [
{
"description": "This is host1 server",
"host": "host1"
},
{
"host_name": "host4"
}
],
"skip_reason": "Conditional check failed",
"skipped": true
},
{
"_ansible_no_log": false,
"changed": false,
"item": [
{
"description": "This is host2 server",
"host": "host2"
},
{
"host_name": "host1"
}
],
"skip_reason": "Conditional check failed",
"skipped": true
},
{
"_ansible_no_log": false,
"changed": true,
"cmd": "echo {u'host': u'host2', u'description': u'This is host2 server'}",
"delta": "0:00:00.005463",
"end": "2016-04-05 04:09:05.425793",
"invocation": {
"module_args": {
"_raw_params": "echo {u'host': u'host2', u'description': u'This is host2 server'}",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"item": [
{
"description": "This is host2 server",
"host": "host2"
},
{
"host_name": "host2"
}
],
"rc": 0,
"start": "2016-04-05 04:09:05.420330",
"stderr": "",
"stdout": "{uhost: uhost2, udescription: uThis is host2 server}",
"stdout_lines": [
"{uhost: uhost2, udescription: uThis is host2 server}"
],
"warnings": []
},
{
"_ansible_no_log": false,
"changed": false,
"item": [
{
"description": "This is host2 server",
"host": "host2"
},
{
"host_name": "host3"
}
],
"skip_reason": "Conditional check failed",
"skipped": true
},
{
"_ansible_no_log": false,
"changed": false,
"item": [
{
"description": "This is host2 server",
"host": "host2"
},
{
"host_name": "host4"
}
],
"skip_reason": "Conditional check failed",
"skipped": true
}
]
}
}
PLAY RECAP *********************************************************************
127.0.0.1 : ok=3 changed=1 unreachable=0 failed=0
In my all_hosts its also giving me unmatched record with skip message "skip_reason": "Conditional check failed". I don't want to include unmatched records in all_hosts variable.
Expected Output:
TASK [debug] *******************************************************************
ok: [127.0.0.1] => {
"all_hosts": {
"changed": true,
"msg": "All items completed",
"results": [
{
"_ansible_no_log": false,
"changed": true,
"cmd": "echo {u'host': u'host1', u'description': u'This is host1 server'}",
"delta": "0:00:00.005613",
"end": "2016-04-05 04:09:05.269437",
"invocation": {
"module_args": {
"_raw_params": "echo {u'host': u'host1', u'description': u'This is host1 server'}",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"item": [
{
"description": "This is host1 server",
"host": "host1"
},
{
"host_name": "host1"
}
],
"rc": 0,
"start": "2016-04-05 04:09:05.263824",
"stderr": "",
"stdout": "{uhost: uhost1, udescription: uThis is host1 server}",
"stdout_lines": [
"{uhost: uhost1, udescription: uThis is host1 server}"
],
"warnings": []
},
{
"_ansible_no_log": false,
"changed": true,
"cmd": "echo {u'host': u'host2', u'description': u'This is host2 server'}",
"delta": "0:00:00.005463",
"end": "2016-04-05 04:09:05.425793",
"invocation": {
"module_args": {
"_raw_params": "echo {u'host': u'host2', u'description': u'This is host2 server'}",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"item": [
{
"description": "This is host2 server",
"host": "host2"
},
{
"host_name": "host2"
}
],
"rc": 0,
"start": "2016-04-05 04:09:05.420330",
"stderr": "",
"stdout": "{uhost: uhost2, udescription: uThis is host2 server}",
"stdout_lines": [
"{uhost: uhost2, udescription: uThis is host2 server}"
],
"warnings": []
}
]
}
}
PLAY RECAP *********************************************************************
127.0.0.1 : ok=3 changed=1 unreachable=0 failed=0
I am able to filter value using item|changed like below
- debug: var={{ item }}
with_items: all_hosts.results
when: item|changed
But is there any way to exclude these value from variable (all_hosts) itself? As its unnecessary increasing the iteration.
Any suggestion?
Unfortunately, that is the intended behavior. Lot of people have complained about this but avoiding the second loop is not possible.
Registered Variables
Note
If a task fails or is skipped, the variable still is registered
with a failure or skipped status, the only way to avoid registering a
variable is using tags.

Get the count of matching condition of register variable in Ansible Playbook

I have a sample playbook In which, I am matching host from two ansible variable using with_nested. These two variable in json format. Records are matching correctly. But due to changes in ansible 2.x its showing both matching and unmatching records in register variable.
---
- hosts: 127.0.0.1
connection: local
tasks:
- shell: "echo {{ item[0] }}"
with_nested:
- [{"host": "host1","description": "This is host1 server"}, {"host": "host2","description": "This is host2 server"}]
- [{"host_name": "host1"},{"host_name": "host2"},{"host_name": "host3"},{"host_name":"host4"}]
register: all_hosts
when: item[0].host == item[1].host_name
- debug: var=all_hosts.results
Output of all_hosts
"all_hosts.results": [
{
"_ansible_no_log": false,
"changed": true,
"cmd": "echo {u'host': u'host1', u'description': u'This is host1 server'}",
"delta": "0:00:00.005456",
"end": "2016-04-07 02:34:37.151824",
"invocation": {
"module_args": {
"_raw_params": "echo {u'host': u'host1', u'description': u'This is host1 server'}",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"item": [
{
"description": "This is host1 server",
"host": "host1"
},
{
"host_name": "host1"
}
],
"rc": 0,
"start": "2016-04-07 02:34:37.146368",
"stderr": "",
"stdout": "{uhost: uhost1, udescription: uThis is host1 server}",
"stdout_lines": [
"{uhost: uhost1, udescription: uThis is host1 server}"
],
"warnings": []
},
{
"_ansible_no_log": false,
"changed": false,
"item": [
{
"description": "This is host1 server",
"host": "host1"
},
{
"host_name": "host2"
}
],
"skip_reason": "Conditional check failed",
"skipped": true
},
{
"_ansible_no_log": false,
"changed": false,
"item": [
{
"description": "This is host1 server",
"host": "host1"
},
{
"host_name": "host3"
}
],
"skip_reason": "Conditional check failed",
"skipped": true
},
{
"_ansible_no_log": false,
"changed": false,
"item": [
{
"description": "This is host1 server",
"host": "host1"
},
{
"host_name": "host4"
}
],
"skip_reason": "Conditional check failed",
"skipped": true
},
{
"_ansible_no_log": false,
"changed": false,
"item": [
{
"description": "This is host2 server",
"host": "host2"
},
{
"host_name": "host1"
}
],
"skip_reason": "Conditional check failed",
"skipped": true
},
{
"_ansible_no_log": false,
"changed": true,
"cmd": "echo {u'host': u'host2', u'description': u'This is host2 server'}",
"delta": "0:00:00.005470",
"end": "2016-04-07 02:34:37.310095",
"invocation": {
"module_args": {
"_raw_params": "echo {u'host': u'host2', u'description': u'This is host2 server'}",
"_uses_shell": true,
"chdir": null,
"creates": null,
"executable": null,
"removes": null,
"warn": true
},
"module_name": "command"
},
"item": [
{
"description": "This is host2 server",
"host": "host2"
},
{
"host_name": "host2"
}
],
"rc": 0,
"start": "2016-04-07 02:34:37.304625",
"stderr": "",
"stdout": "{uhost: uhost2, udescription: uThis is host2 server}",
"stdout_lines": [
"{uhost: uhost2, udescription: uThis is host2 server}"
],
"warnings": []
},
{
"_ansible_no_log": false,
"changed": false,
"item": [
{
"description": "This is host2 server",
"host": "host2"
},
{
"host_name": "host3"
}
],
"skip_reason": "Conditional check failed",
"skipped": true
},
{
"_ansible_no_log": false,
"changed": false,
"item": [
{
"description": "This is host2 server",
"host": "host2"
},
{
"host_name": "host4"
}
],
"skip_reason": "Conditional check failed",
"skipped": true
}
]
So there is two matching records are there. Is there any ways to get count of these matching records?
Something
- debug: msg="Task if matching record count more than one"
when: all_hosts.changed | length > 0
Do you really need the count or want to check if there is at least one matching record? How about:
- debug: msg="Task if matching record count more than one"
when: all_hosts.changed

Resources