Get the count of matching condition of register variable in Ansible Playbook
<p>I have a sample playbook In which, I am matching host from two ansible variable using <code>with_nested</code>. 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.</p>

<pre><code>---

 - 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
</code></pre>

<p>Output of <code>all_hosts</code></p>

<pre><code>"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
        }
    ]
</code></pre>

<p>So there is two matching records are there. Is there any ways to get count of these matching records? </p>

<p>Something </p>

<pre><code>     - debug: msg="Task if matching record count more than one"
       when: all_hosts.changed | length &gt; 0
</code></pre>