gpt4 book ai didi

json - 为多个目标主机创建 ansible list

转载 作者:行者123 更新时间:2023-12-03 16:02:07 25 4
gpt4 key购买 nike

我使用 terraform 创建资源,然后使用 模板文件 用 yaml 语言创建 ansible 库存的方法。我正在 terraform 中创建多个虚拟机,并在单个管道中使用 ansible 配置它们。
我遇到的问题是 yaml 文件使用 - 对于在 ansible playbook 中给出错误的列表
输出.tf

resource "local_file" "AnsibleInventory" {
content = templatefile("inventory.tmpl",
{
ansible_port = "5986"
ansible_connection = "winrm"
ansible_winrm_server_cert_validation = "ignore"
ansible_winrm_transport = "ntlm"
vm-ip = data.azurerm_public_ip.main.*.ip_address,
username = "testadmin",
ansible_password = "abc"
}
)
filename = "inventory.json"
}
库存.tmpl
${jsonencode({
"all": {
"hosts": {
"server": [
for ip in vm-ip : {
"ansible_host": "${ip}",
"ansible_port": 5986,
"ansible_user": "testadmin",
"ansible_winrm_transport": "ntlm",
"ansible_connection": "winrm",
"ansible_winrm_server_cert_validation": "ignore",
"ansible_password": "abc"
}
]
}
}
})}
库存.json
{"all":{"hosts":{"server":{"ansible_connection":"winrm","ansible_host":"343434","ansible_password":"abc","ansible_port":5986,"ansible_user":"testadmin","ansible_winrm_server_cert_validation":"ignore","ansible_winrm_transport":"ntlm"}}}}
库存.yaml
all:
hosts:
server:
- ansible_connection: winrm
ansible_host: 40.88.14.205
ansible_password: abc
ansible_port: 5986
ansible_user: testadmin
ansible_winrm_server_cert_validation: ignore
ansible_winrm_transport: ntlm
我的方法是如何从代码中删除“-”还是我做错了什么。
编辑:
我正在管道中使用 bash 将 json 文件转换为 yaml 文件。
python -c 'import sys, yaml, json; yaml.safe_dump(json.load(sys.stdin), sys.stdout, default_flow_style=False)' < inventory.json > inventory.yaml

最佳答案

正如 @ydaetskcoR's comment 所指出的,您的库存是错误的,因为您需要主机列表是 hosts 的直接子级。属性。

请注意,您最终得到的列表是通过使用方括号 [] 生成的。 ,生成元组,大约 for在您的 Terraform 模板中,而不是使用括号 {} ,生产对象。

The type of brackets around the for expression decide what type of result it produces. The above example uses [ and ], which produces a tuple. If { and } are used instead, the result is an object, and two result expressions must be provided separated by the => symbol.


https://www.terraform.io/docs/configuration/expressions.html#for-expressions

如果您的结构,应该看起来像 all > server1, server2, server3那么你的库存应该是
all:
hosts:
server1:
ansible_host: 10.0.0.2
ansible_port: 5986
server2:
ansible_host: 10.0.0.3
ansible_port: 5986
server3:
ansible_host: 10.0.0.4
ansible_port: 5986
这是用于生成以下内容的 Terraform 模板的方法:
${yamlencode({
"all": {
"hosts": {
for i, ip in vm-ip:
"server${i+1}" => {
"ansible_host": "${ip}",
"ansible_port": 5986
}
}
}
})}
这给出了这个有效的inventory.yaml:
"all":
"hosts":
"server1":
"ansible_host": "10.0.0.2"
"ansible_port": 5986
"server2":
"ansible_host": "10.0.0.3"
"ansible_port": 5986
"server3":
"ansible_host": "10.0.0.4"
"ansible_port": 5986

另一方面,如果你想要一个像 all > server > server1, server2, server3 这样的结构
那么你应该做 server all的 child 并使用 children entry在您的库存中:
all:
children:
server:
hosts:
server1:
ansible_host: 10.0.0.2
ansible_port: 5986
server2:
ansible_host: 10.0.0.3
ansible_port: 5986
server3:
ansible_host: 10.0.0.4
ansible_port: 5986
对于这个,相应的 Terraform 模板将是
${yamlencode({
"all": {
"children": {
"server": {
"hosts": {
for i, ip in vm-ip:
"server${i+1}" => {
"ansible_host": "${ip}",
"ansible_port": 5986
}
}
}
}
}
})}
其中产生这个inventory.yaml:
"all":
"children":
"server":
"hosts":
"server1":
"ansible_host": "10.0.0.2"
"ansible_port": 5986
"server2":
"ansible_host": "10.0.0.3"
"ansible_port": 5986
"server3":
"ansible_host": "10.0.0.4"
"ansible_port": 5986

注:对于上面的所有示例,我使用的是下面的 terraform 文件 test.tf:
resource "local_file" "AnsibleInventory" {
content = templatefile("inventory.tpl", {
vm-ip = ["10.0.0.2","10.0.0.3","10.0.0.4"],
})
filename = "inventory.yaml"
}

关于json - 为多个目标主机创建 ansible list ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62429760/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com