Ansible 显示错误:“一个或多个未定义的变量:‘项目’未定义";使用“with_items"时 [英] Ansible shows error: "One or more undefined variables: 'item' is undefined" when using 'with_items'
问题描述
我正在尝试计算 elb 中的实例.这是我的 Ansible 手册:
I am trying to count the instances inside an elb. This is my Ansible playbook:
- name: Get elb facts
local_action:
module: ec2_elb_facts
name: "{{elb}}"
region: "{{ansible_ec2_placement_region}}"
environment: creds
register: elb_facts
- debug:
var: elb_facts
verbosity: 2
- debug:
msg: "Instance: {{ item.instances }}"
with_items: "{{ elb_facts.elbs }}"
和我的输出(删除了敏感数据):
and my output (sensitive data removed):
TASK: [debug ] ****************************************************************
ok: [10.0.0.0] => {
"elb_facts": {
"changed": false,
"elbs": [
{
"availability_zones": [
"ap-southeast-2b",
"ap-southeast-2a"
],
"dns_name": "elbname123.ap-southeast-2.elb.amazonaws.com",
"health_check": {
"healthy_threshold": 2,
"interval": 10,
"target": "TCP:0000",
"timeout": 5,
"unhealthy_threshold": 2
},
"instances": [
{
"id": "i-000000000000000",
"state": null
}
],
"name": "accessgateway",
"scheme": "internal",
"security_groups": [
"sg-00000000"
],
"subnet": [
"subnet-0000000",
"subnet-1111111"
],
"vpc_id": "vpc-000000"
}
],
"invocation": {
"module_args": "",
"module_name": "ec2_elb_facts"
}
}
}
TASK: [debug ] ****************************************************************
fatal: [10.0.0.0] => One or more undefined variables: 'item' is undefined
FATAL: all hosts have already failed -- aborting
所以我想做的只是循环并打印 elb_facts、instances 变量中的所有内容.据我所知,它是一个散列,包含一个散列列表.
So what im trying to do is just loop through and print everything inside the elb_facts, instances variable. From what I can tell it's a hash, containing a list of hashes.
我正在使用 http://docs.ansible.com/ansible/playbooks_loops.html#looping-over-subelements 作为参考.我终其一生都无法弄清楚为什么这不起作用.
I am using http://docs.ansible.com/ansible/playbooks_loops.html#looping-over-subelements as a reference. I cannot for the life of mine figure out why this is not working.
推荐答案
with_items
(以及整个 with_
循环系列)是在任务中定义的字典键,不是作为动作的参数.
with_items
(and the whole family of with_
loops) is a dictionary key defined in a task, not as a parameter to the action.
修正缩进:
- debug:
msg: "Instance: {{ item.instances }}"
with_items: "{{ elb_facts.elbs }}"
这篇关于Ansible 显示错误:“一个或多个未定义的变量:‘项目’未定义";使用“with_items"时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!