Ansible显示错误:"一个或多个未定义的变量:' item'未定义"使用' with_items'

时间:2017-02-15 00:51:11

标签: ansible

我正在尝试计算elb中的实例。这是我的Ansible剧本:

- 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 }}"

和我的输出(删除敏感数据):

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变量中的所有内容。据我所知,它是一个散列,包含一个哈希列表。

我使用http://docs.ansible.com/ansible/playbooks_loops.html#looping-over-subelements作为参考。我不能为我的生活弄清楚为什么这不起作用。

1 个答案:

答案 0 :(得分:7)

with_(以及- debug: msg: "Instance: {{ item.instances }}" with_items: "{{ elb_facts.elbs }}" 循环的整个系列)是在任务中定义的字典键,而不是操作的参数。

修复缩进:

squares