【问题标题】:Ansible EC2 SnapshotsAnsible EC2 快照
【发布时间】:2017-12-09 04:54:54
【问题描述】:

我正在尝试使用 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 }}"

那么谁能帮我解决这个问题?

【问题讨论】:

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


    【解决方案1】:

    AFAIK 快照适用于卷,而不适用于实例。
    为什么不直接获取所有 volume_id 并对其进行快照?

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

    【讨论】:

    • 以上播放返回failed: [localhost] (item=vol-0f0026ee65c5452e4) => {"failed": true, "item": "vol-0f0026ee65c5452e4", "msg": "InvalidVolume.NotFound: The volume 'vol-0f0026ee65c5452e4' does not exist."}错误
    • 但在 AWS 中,vol-0f0026ee65c5452e4 卷存在
    • 如果我使用{{ ec2_facts['instances'][0]['block_device_mapping'][0]['device_name'] }}",它适用于第一个实例,因为我使用的是[0] AMI 索引
    【解决方案2】:

    我可以使用以下方式对所有正在运行的 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[]') }}"
    

    希望以上内容对未来的访客有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-05
      相关资源
      最近更新 更多