注册ec2创建变量的问题

时间:2016-09-28 00:34:31

标签: python ansible ansible-playbook

我有一个创建ec2实例的剧本,我希望将输出注册为var。从那里我将使用已注册的var来创建和附加ebs卷。但是,我遇到了以下错误。

 - name: Create EC2 instance for zone A
   ec2:
    key_name: "{{ keypair }}"
    group: "{{ security_groups }}"
    image: "{{ ami }}"
    instance_type: "{{ instance_type }}"
    wait: true
    region: "{{ ec2_region }}"
    vpc_subnet_id: "{{ subneta }}"
    assign_public_ip: "{{ public_choice }}"
    zone: "{{ zonea }}"
    count: 1
    instance_tags:
      Name: "db{{ item }}a.{{ env }}"
      envtype: "{{ envtype }}"
   register: ec2
   with_sequence: "start=1 end={{ num }}"


 - debug:
    msg: "{{ ec2 }}"
 - debug:
    msg: "{{ ec2.instance_ids }}"

有了这个,我得到以下输出

dumbledore@ansible1a:/etc/ansible/roles/db_ec2/tasks > ansible-playbook db_ec2.yml -e 'env=qa num=1 ebs=true'
 ______
< PLAY >
 ------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

 ______________________
< TASK [db_ec2 : fail] >
 ----------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

 _______________________
< TASK [db_ec2 : debug] >
 -----------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [localhost] => {
    "msg": "If you need to change any default variables for this playbook edit vars/qa.yml and vars/ebs.yml for ebs configs"
}
 _________________________
< TASK [db_ec2 : include] >
 -------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

included: /etc/ansible/roles/db_ec2/tasks/./db_create.yml for localhost
 ________________________________________________
< TASK [db_ec2 : Create EC2 instance for zone A] >
 ------------------------------------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

changed: [localhost] => (item=1)
 _______________________
< TASK [db_ec2 : debug] >
 -----------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

ok: [localhost] => {
    "msg": {
        "changed": true,
        "msg": "All items completed",
        "results": [
            {
                "_ansible_no_log": false,
                "changed": true,
                "instance_ids": [
                    "i-1046c108"
                ],
                "instances": [
                    {
                        "ami_launch_index": "0",
                        "architecture": "x86_64",
                        "block_device_mapping": {
                            "/dev/sda1": {
                                "delete_on_termination": true,
                                "status": "attached",
                                "volume_id": "vol-55812edd"
                            }
                        },
                        "dns_name": "",
                        "ebs_optimized": false,
                        "groups": {
                            "sg-749f3c0d": "qa-ssh",
                            "sg-8f983bf6": "qa-db"
                        },
                        "hypervisor": "xen",
                        "id": "i-1046c108",
                        "image_id": "ami-55e31a35",
                        "instance_type": "m4.xlarge",
                        "kernel": null,
                        "key_name": "ccpkey",
                        "launch_time": "2016-09-28T00:28:00.000Z",
                        "placement": "us-west-2a",
                        "private_dns_name": "ip-10-50-36-201.us-west-2.compute.internal",
                        "private_ip": "10.50.36.201",
                        "public_dns_name": "",
                        "public_ip": null,
                        "ramdisk": null,
                        "region": "us-west-2",
                        "root_device_name": "/dev/sda1",
                        "root_device_type": "ebs",
                        "state": "running",
                        "state_code": 16,
                        "tags": {
                            "Name": "db1a.qa",
                            "envtype": "qa-db"
                        },
                        "tenancy": "default",
                        "virtualization_type": "hvm"
                    }
                ],
                "invocation": {
                    "module_args": {
                        "assign_public_ip": false,
                        "aws_access_key": null,
                        "aws_secret_key": null,
                        "count": 1,
                        "count_tag": null,
                        "ebs_optimized": false,
                        "ec2_url": null,
                        "exact_count": null,
                        "group": [
                            "qa-db",
                            "qa-ssh"
                        ],
                        "group_id": null,
                        "id": null,
                        "image": "ami-55e31a35",
                        "instance_ids": null,
                        "instance_profile_name": null,
                        "instance_tags": {
                            "Name": "db1a.qa",
                            "envtype": "qa-db"
                        },
                        "instance_type": "m4.xlarge",
                        "kernel": null,
                        "key_name": "ccpkey",
                        "monitoring": false,
                        "network_interfaces": null,
                        "placement_group": null,
                        "private_ip": null,
                        "profile": null,
                        "ramdisk": null,
                        "region": "us-west-2",
                        "security_token": null,
                        "source_dest_check": true,
                        "spot_price": null,
                        "spot_type": "one-time",
                        "spot_wait_timeout": 600,
                        "state": "present",
                        "tenancy": "default",
                        "termination_protection": false,
                        "user_data": null,
                        "validate_certs": true,
                        "volumes": null,
                        "vpc_subnet_id": "subnet-5d625c39",
                        "wait": true,
                        "wait_timeout": 300,
                        "zone": "us-west-2a"
                    },
                    "module_name": "ec2"
                },
                "item": "1",
                "tagged_instances": []
            }
        ]
    }
}
 _______________________
< TASK [db_ec2 : debug] >
 -----------------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

fatal: [localhost]: FAILED! => {"failed": true, "msg": "'dict object' has no attribute 'instance_ids'"}
 ____________
< PLAY RECAP >
 ------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

localhost                  : ok=4    changed=1    unreachable=0    failed=1

所以我可以看到应该存在的变量在完整的ec2变量的输出中。但是当尝试使用ec2.instance_ids的密钥特定变量时,它会失败......

编辑:

这似乎是with_sequence特有的。如果我删除with序列并将其更改为类似的内容。

 - name: Create EC2 instance for zone A
   ec2:
    key_name: "{{ keypair }}"
    group: "{{ security_groups }}"
    image: "{{ ami }}"
    instance_type: "{{ instance_type }}"
    region: "{{ ec2_region }}"
    vpc_subnet_id: "{{ subneta }}"
    assign_public_ip: "{{ public_choice }}"
    zone: "{{ zonea }}"
    count: "{{ num }}"
    wait: true
    instance_tags:
      envtype: "{{ envtype }}"
   register: ec2

 - debug:
    msg: "{{ item.id }}"
   with_items: "{{ ec2.instances }}"

我不再收到任何错误,它会毫无问题地打印实例ID。

1 个答案:

答案 0 :(得分:1)

不确定with_sequence会如何影响这里,但我在这里看到至少一个基本问题。

当您在名为“ec2”的变量中注册ec2任务的输出时,可以使用以下内容引用instance_ids -

select t1.student_id, t1.student_name, t1.class, t2.Last_activity_date, t3.Last_action_date, t3.Action_desc

(select student_id, student_name, class from student) as t1  join

(select student_id, max(activity_date) as Last_activity_date group by student_id) as t2 on t2.student_id =t1.student_id join

select student_id, max(actiondate) as Last_action_date, Action_desc  group by student_id) as t3 on t3.student_id =t1.student_id
order by 1

Ansible任务正在抱怨,因为在“ec2”中没有名为“instance_ids”的顶级键,而是存在于“ec2”中的“results”键中。

希望这是有道理的,除非我错误地解释你的问题。