ansible 动态库存主机值不接受主机列表

ansible 动态库存主机值不接受主机列表

我正在尝试为我的设置创建动态库存。我到处都看到 IP 和主机名的示例列表。但当我提供主机值作为列表时,它会出现错误并说它必须是字典。

以下是我提供给剧本的清单,

{
    "all": {
        "hosts": ["192.168.1.2", "192.168.1.3", "192.168.1.4"],
        "vars": {
             "ansible_ssh_user": "root"
        },
        "_meta": {
            "hostvars": {}
        }
    },
    "webserver": {
        "hosts": ["192.168.1.3", "192.168.1.4"],
        "vars": {
             "ansible_ssh_user": "root"
        },
        "_meta": {
            "hostvars": {}
        }
    },
    "dbserver": {
        "hosts": ["192.168.1.2"],
        "vars": {
             "ansible_ssh_user": "root"
        },
        "_meta": {
            "hostvars": {}
        }
    }
}

它给出了这个警告和错误

[WARNING]:  * Failed to parse /Users/rahulbhatu/playbooks/waf-
playbooks/inventory with yaml plugin: Invalid "hosts" entry for "all" group,
requires a dictionary, found "<type 'list'>" instead.

[WARNING]:  * Failed to parse /Users/rahulbhatu/playbooks/waf-
playbooks/inventory with ini plugin: Invalid host pattern 'all:' supplied,
ending in ':' is not allowed, this character is reserved to provide a port.

[WARNING]: Unable to parse /Users/rahulbhatu/playbooks/waf-playbooks/inventory
as an inventory source

[WARNING]: No inventory was parsed, only implicit localhost is available```
fatal: [{]: UNREACHABLE! => {"changed": false, "msg": "Failed to connect to the host via ssh: ssh: Could not resolve hostname {: nodename nor servname provided, or not known", "unreachable": true}

但是,当我以这种方式将它作为字典传递并且将值设为 null 时,它就可以工作了,有人可以帮助我解决我所缺少的问题吗?

    "all": {
        "hosts": {
             "192.168.1.2": null,
             "192.168.1.3" : null,
             "192.168.1.4" : null
        },
        "vars": {
             "ansible_ssh_user": "root"
        },
        "_meta": {
            "hostvars": {}
        }
    },
    "webserver": {
        "hosts": {
            "192.168.1.3": null,
            "192.168.1.4": null
        },
        "vars": {
             "ansible_ssh_user": "root"
        },
        "_meta": {
            "hostvars": {}
        }
    },
    "dbserver": {
        "hosts": {
            "192.168.1.2": null
        },
        "vars": {
             "ansible_ssh_user": "root"
        },
        "_meta": {
            "hostvars": {}
        }
    }
}

答案1

除了上述错误之外,如果您使用.inihosts 文件,也会遇到同样的错误。将文件扩展名更改.yml为与实际文件格式匹配。我的问题就是这么简单,就这样解决了!

相关内容