修复缩进:- debug: msg: "Instance: {{ item.instances }}" with_items: "{{ elb_facts.elbs }}"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 undefinedFATAL: all hosts have already failed -- abortingSo 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.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 (and the whole family of with_ loops) is a dictionary key defined in a task, not as a parameter to the action.Fix the indentation:- debug: msg: "Instance: {{ item.instances }}" with_items: "{{ elb_facts.elbs }}" 这篇关于Ansible显示错误:“一个或多个未定义变量:'item'未定义"当使用'with_items'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-23 12:28