我是Ansible的新手,我正努力从寄存器变量ec2中获取字典值。这是我的戏
---
- name: Provision 2 bastions in different AZ
hosts: local
connection: local
gather_facts: True
vars_files:
- /home/marco/project_provision/bastion_vars.yml
tasks:
- name: Launch the new EC2 Instance
with_items: "{{ subnets }}"
local_action: ec2
group_id={{ security_group }}
instance_type={{ instance_type}}
image={{ image }}
wait=true
region={{ region }}
keypair={{ keypair }}
count={{count}}
vpc_subnet_id={{ item.vpc_pvt_subnet }}
zone={{ item.zone }}
register: ec2
- name: debug
debug: var=ec2
- name: Add the newly created EC2 instance(s) to the local host group
local_action: lineinfile
dest="/home/marco/safeboda_provision/hosts"
regexp={{ item.public_ip }}
insertafter="[bastion]" line={{ item.public_ip }}
with_dict: "{{ ec2.instances }}"
- name: Wait for SSH to come up
local_action: wait_for
host={{ item.public_ip }}
port=22
state=started
with_items: "{{ ec2.instances }}"
实例创建正确,但是当我尝试对其进行标记或将其动态添加到清单中时,我得到的结果就是
TASK [Add the newly created EC2 instance(s) to the local host group] ****************************************************
fatal: [localhost]: FAILED! => {"msg": "'dict object' has no attribute 'instances'"}
to retry, use: --limit @/home/marco/project_provision/create_ec2_bastions.retry
这是上一条命令的调试输出
TASK [debug] ************************************************************************************************************
ok: [localhost] => {
"ec2": {
"changed": true,
"msg": "All items completed",
"results": [
{
"_ansible_delegated_vars": {
"ansible_delegated_host": "localhost",
"ansible_host": "localhost"
},
"_ansible_ignore_errors": null,
"_ansible_item_label": {
"vpc_pvt_subnet": "subnet-083d88e5974383b34",
"zone": "us-east-1a"
},
"_ansible_item_result": true,
"_ansible_no_log": false,
"_ansible_parsed": true,
"changed": true,
"failed": false,
"instance_ids": [
"i-04346a168c08e5571"
],
"instances": [
{
"ami_launch_index": "0",
"architecture": "x86_64",
"block_device_mapping": {
"/dev/xvda": {
"delete_on_termination": true,
"status": "attached",
"volume_id": "vol-0e61d3b4c7c2ecfc9"
}
},
"dns_name": "ec2-54-221-14-254.compute-1.amazonaws.com",
"ebs_optimized": false,
"groups": {
"sg-0951a3c52f397a1b2": "SafebodaUS-BastionSecurityGroup-C60AGX2R0NRY"
},
"hypervisor": "xen",
"id": "i-04346a168c08e5571",
"image_id": "ami-0b8401ca902dbfbb9",
有人可以阐明我如何遍历任务返回的值吗?