我对剧本有疑问,想知道问题是否是我使用的字典中的某些关键名称具有特殊字符,即其中的破折号。Ansible 不清楚这是否允许。
任务:
- debug:
var: block_storage_route
- debug:
var: block_storage_route[0].table-id
vars:
block_storage_route:
- "table-id": 0
Run Code Online (Sandbox Code Playgroud)
输出:
TASK [debug] ******************************************************
ok: [example.com] => {
"block_storage_route": [
{
"table-id": 0
}
]
}
TASK [debug] ******************************************************
ok: [example.com] => {
"block_storage_route[0].table-id": "VARIABLE IS NOT DEFINED!"
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,尽管键存在于变量中,但我收到“变量未定义”错误。
这是为什么?table-iddebugblock_storage_route
在编写 terraform 模块时,我经常遇到这样的错误:
Error: Invalid index
on ../../../modules/host/main.tf line 7, in resource "aws_network_interface" "host":
7: subnet_id = data.aws_subnet_ids.current[each.key].ids[0]
|----------------
| data.aws_subnet_ids.current is object with 2 attributes
| each.key is "lab"
Run Code Online (Sandbox Code Playgroud)
发生这种情况的原因有多种。通常是因为我认为某个对象将包含的内容是错误的。
为了帮助调试,至少查看对象包含的内容会很有用。“具有 2 个属性的对象”相当模糊。我想知道它有什么属性,所以我可以添加必要的转换以最终得到我需要的字符串。
那么有办法吗?您能否以某种方式运行“terraform plan”,以便在计划出现错误时实际显示这些对象的内容?
在 ansible 变量中给出这个列表:
---
hosts:
- address: host1.local
cluster:
name: RED
- address: host2.local
cluster:
name: RED
- address: host3.local
cluster:
name: GREEN
- address: host4.local
cluster:
name: BLUE
Run Code Online (Sandbox Code Playgroud)
我现在想以某种方式计算每个集群中的主机数量。所以我想结束:
hosts_per_cluster:
RED: 2
BLUE: 1
GREEN: 1
Run Code Online (Sandbox Code Playgroud)
人们会怎样做呢?
我的第一次尝试是这样的:
- name: Get number of hosts per cluster
set_fact:
hosts_per_cluster[item.cluster.name]={{ hosts_per_cluster[item.cluster.name] | default(0) | int +1 }}
loop: "{{ hosts }}"
Run Code Online (Sandbox Code Playgroud)
然而这并没有奏效......