在Ansible Playbook

时间:2016-04-07 09:45:42

标签: ansible ansible-playbook

我有一个示例剧本在其中,我使用with_nested从两个ansible变量匹配主机。这两个变量采用json格式。记录正确匹配。但是由于ansible 2.x的变化,它在寄存器变量中显示匹配和不匹配的记录。

---

 - 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

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

所以有两个匹配的记录。有没有办法计算这些匹配的记录?

东西

     - debug: msg="Task if matching record count more than one"
       when: all_hosts.changed | length > 0

1 个答案:

答案 0 :(得分:1)

您真的需要计数还是想检查是否至少有一条匹配的记录?怎么样:

 - debug: msg="Task if matching record count more than one"
   when: all_hosts.changed
相关问题