所以我的剧本中有以下部分:
- name: Loop hash
set_fact:
"{{ item.key }}":
instance_id: "{{ item.value.ansible_ec2_instance_id }}"
instance_az: "{{ item.value.ansible_ec2_placement_availability_zone }}"
with_dict: "{{ hostvars }}"
when: (item.value.ansible_ec2_instance_id is defined) and
(item.value.ansible_ec2_placement_availability_zone is defined)
当我检查输出时,它没有解析item.key
'item.key': {'instance_id': u'i-abc12345678', 'instance_az': u'ap-southeast-2b'}
关于最新进展的任何想法?
编辑:
错误建议的解决方案:
ERROR: Syntax Error while loading YAML script, /apps/co-playbooks/common/tasks/elb_check_instances.yml
Note: The error may actually appear before this position: line 40, column 18
{ "{{ item.key }}":
instance_id: "{{ item.value.ansible_ec2_instance_id }}"
^
We could be wrong, but this one looks like it might be an issue with
missing quotes. Always quote template expression brackets when they
start a value. For instance:
尝试了不同的语法:
- name: Loop hash
set_fact:
{ "{{ item.key }}":
{ instance_id: "{{ item.value.ansible_ec2_instance_id }}",
instance_az: "{{ item.value.ansible_ec2_placement_availability_zone }}" }}
with_dict: "{{ hostvars }}"
when: (item.value.ansible_ec2_instance_id is defined) and
(item.value.ansible_ec2_placement_availability_zone is defined)
仍无法解析item.key
'{# item.key #}': {'instance_id': u'i-abc1234567', 'instance_az': u'ap-southeast-2b'}
答案 0 :(得分:0)
在set_fact任务之外添加一对{ }
。
- name: Loop hash
set_fact:
{ "{{ item.key }}":
instance_id: "{{ item.value.ansible_ec2_instance_id }}"
instance_az: "{{ item.value.ansible_ec2_placement_availability_zone }}" }
with_dict: "{{ hostvars }}"
when: (item.value.ansible_ec2_instance_id is defined) and
(item.value.ansible_ec2_placement_availability_zone is defined)