Ansible EC2快照

时间:2017-12-09 04:54:54

标签: amazon-web-services amazon-ec2 ansible

我尝试使用Ansible自动执行EC2快照。我写了下面的剧集来收集事实:

- hosts: localhost
  connection: local
  tasks:
    - name: Gathering the facts...
        ec2_remote_facts:
        aws_secret_key: bcv.jdbvdjbvdfjbv
        aws_access_key: dfnvdfnbvdfjbvndfj
        region: us-west-2
      register: ec2_facts

- name: printing the results
  debug: var=ec2_facts

以上播放正常并返回以下输出:

ok: [localhost] => {
    "ec2_facts": {
    "changed": false, 
    "instances": [
        {
            "ami_launch_index": "0", 
            "architecture": "x86_64", 
            "block_device_mapping": [
                {
                    "attach_time": "2017-12-08T08:52:21.000Z", 
                    "delete_on_termination": true, 
                    "device_name": "/dev/sda1", 
                    "status": "attached", 
                    "volume_id": "vol-0f0026ee65c5452e4"
                }
            ], 
            "client_token": "", 
            "ebs_optimized": false, 
            "groups": [
                {
                    "id": "sg-e5d10780", 
                    "name": "default"
                }
            ], 
            "hypervisor": "xen", 
            "id": "i-0409acd413bb9db0f", 
            "image_id": "ami-03445933", 
            "instance_profile": null, 
            "interfaces": [
                {
                    "id": "eni-1ea57b3d", 
                    "mac_address": "02:04:f4:3a:92:34"
                }
            ], 
            "kernel": null, 
            "key_name": "ansible-test", 
            "launch_time": "2017-12-08T08:52:20.000Z", 
            "monitoring_state": "disabled", 
            "persistent": false, 
            "placement": {
                "tenancy": "default", 
                "zone": "us-west-2b"
            }, 
            "private_dns_name": "ip-172-31-41-48.us-west-2.compute.internal", 
            "private_ip_address": "172.31.41.48", 
            "public_dns_name": "ec2-x-x-x-x.us-west-2.compute.amazonaws.com", 
            "public_ip_address": "x.x.x.x", 
            "ramdisk": null, 
            "region": "us-west-2", 
            "requester_id": null, 
            "root_device_type": "ebs", 
            "source_destination_check": "true", 
            "spot_instance_request_id": null, 
            "state": "running", 
            "tags": {}, 
            "virtualization_type": "hvm", 
            "vpc_id": "vpc-4a77992f"
        }, 
        {
            "ami_launch_index": "0", 
            "architecture": "x86_64", 
            "block_device_mapping": [
                {
                    "attach_time": "2017-12-08T11:43:45.000Z", 
                    "delete_on_termination": false, 
                    "device_name": "/dev/sda1", 
                    "status": "attached", 
                    "volume_id": "vol-036a851b5b96ac359"
                }
            ], 
            "client_token": "", 
            "ebs_optimized": false, 
            "groups": [
                {
                    "id": "sg-c63677a3", 
                    "name": "test"
                }
            ], 
            "hypervisor": "xen", 
            "id": "i-05b9ecfe5fa30be49", 
            "image_id": "ami-02c71d7a", 
            "instance_profile": null, 
            "interfaces": [
                {
                    "id": "eni-e2d40dc1", 
                    "mac_address": "02:7c:b1:08:45:e8"
                }
            ], 
            "kernel": null, 
            "key_name": "ansible-test", 
            "launch_time": "2017-12-08T11:43:44.000Z", 
            "monitoring_state": "disabled", 
            "persistent": false, 
            "placement": {
                "tenancy": "default", 
                "zone": "us-west-2b"
            }, 
            "private_dns_name": "ip-172-31-35-159.us-west-2.compute.internal", 
            "private_ip_address": "172.31.35.159", 
            "public_dns_name": "ec2-x-x-x-x.us-west-2.compute.amazonaws.com", 
            "public_ip_address": "x.x.x.x", 
            "ramdisk": null, 
            "region": "us-west-2", 
            "requester_id": null, 
            "root_device_type": "ebs", 
            "source_destination_check": "true", 
            "spot_instance_request_id": null, 
            "state": "running", 
            "tags": {}, 
            "virtualization_type": "hvm", 
            "vpc_id": "vpc-4a77992f"
        }
    ]
}
}

我的AWS账户中有两个正在运行的测试实例,上面的输出显示了正确的信息。现在使用上述事实,我想快照附加到各个实例的EBS卷。但即使有多个实例,ami_launch_index值也始终返回0。所以我相信我无法动态选择实例和附加的EBS卷来对其进行快照。以下是我根据上述事实编写的快照实例的剧本:

- name: Snapshoting the instance...
  ec2_snapshot:
    aws_secret_key: bcv.jdbvdjbvdfjbv
    aws_access_key: dfnvdfnbvdfjbvndfj
    region: us-west-2
    instance_id: "{{ item.id }}"
    device_name: "{{ item['device_name'] }}"
    description: "Snapshot test by"
  with_items: "{{ ec2_facts.instances }}"

那么有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

AFAIK快照适用于卷,不适用于实例 为什么不采取所有index.html并快照它们?

volume_id

答案 1 :(得分:0)

我可以使用以下游戏为所有正在运行的AWS实例创建快照:

- hosts: localhost
  connection: local
  tasks:
    - name: Gathering the facts...
      ec2_remote_facts:
        aws_secret_key:
        aws_access_key:
        region: us-west-2
      register: ec2_facts

    - name: Snapshoting the instance...
      ec2_snapshot:
        aws_secret_key:
        aws_access_key:
        region: us-west-2
        volume_id: "{{ item.volume_id }}"
      with_items: "{{ ec2_facts | json_query('instances[].block_device_mapping[]') }}"

希望以上有助于未来的访客。