我的主机和相应的主机变量作为 JSON 输入给出。如何在 Playbook 中使用它们

My Hosts and corresponding hostvars is given as an JSON input. How to use them in the Playbook

我的场景类似于下面的示例。

[atlanta]
host1 http_port=80   maxRequestsPerChild=808
host2 http_port=303  maxRequestsPerChild=909

我有不同的主机,每个主机都有特定的 maxRequestsPerChild 参数值。

如果这是一个静态主机文件,我可以获得每个主机中 maxRequestsPerChild 的值。

但我需要将主机和 maxRequestsPerChild 作为 JSON 动态传递。 如何构造 JSON 并在我的剧本中访问它?

Ansible 不直接支持 JSON 文件形式的清单。 但是它完全支持 库存脚本 returning JSON.

这是官方文档的 link,也描述了预期的输出格式:https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html

您可能想要实施更新的清单格式,它更快并且明确允许每个主机变量,如 https://docs.ansible.com/ansible/latest/dev_guide/developing_inventory.html#tuning-the-external-inventory-script

部分所述

在这种情况下,您的脚本将 return 输出以下内容:

    {
        "_meta": {
            "hostvars": {
                "host1": {
                    "http_port": 80,
                    "maxRequestsPerChild": 808
                },
                "host2": {
                    "http_port": 303,
                    "maxRequestsPerChild": 909
                }
            }
        },
        "atlanta": {
            "hosts": [ "host1", "host2" ]
        }
    }