2017-09-13 9 views
1

私は、週単位のバックアップを作成し、サーバー名の後に日付/時刻タグを付けてラベル付けする前のタスクを持っています。この仕事の目的は、その背後に入り、古いAMIバックアップをクリーンアップし、最後の3つのみを残すことです。ec2_ami_findタスクは正常に機能しますが、一部のサーバーでは空の結果が返される可能性があります。それを処理する。AWS AMIクリーンナップ無しで結果の配列を繰り返す

私は取得していますエラーはかなり一般的なもので:

致命的な:[127.0.0.1]:失敗しました! === { "failed":true、 "条件付きチェック 'item.ec2_ami_find.exists'が失敗しました。エラーは次のとおりです:条件付きの評価中にエラーが発生しました (item.ec2_ami_find.exists): 'dictオブジェクト'にはno属性 'ec2_ami_find' \ n \ nエラーは '/root/ansible/ec2-backups-purge/roles/first_acct/tasks/main.yml'にあります:行25、 列3ですが、 。\ N \ N \ N-名: は、古いバックアップを登録解除\ nは^ここでは\ n」は

NBE別の場所でファイルの正確な 構文の問題に応じて、\ n個の\ n問題のある行があるように思われますプレイブックタスクは次のようになります。

--- 
- name: Find old backups 
    tags: always 
    ec2_ami_find: 
    owner: self 
    aws_access_key: "{{ access_key }}" 
    aws_secret_key: "{{ secret_key }}" 
    region: "{{ aws_region }}" 
    ami_tags: 
     Name: "{{ item }}-weekly-*" 
    sort: name 
    sort_order: descending 
    sort_start: 3 
    with_items: 
    - server-01 
    - server-02 
    - server-win-01 
    - downloads 
    register: stale_amis 

- name: Deregister old backups 
    tags: always 
    ec2_ami: 
    aws_access_key: "{{ access_key }}" 
    aws_secret_key: "{{ secret_key }}" 
    region: "{{ aws_region }}" 
    image_id: "{{ item.ami_id }}" 
    delete_snapshot: True 
    state: absent 
    with_items: 
    - "{{ stale_amis.results }}" 

結果を返すの1のスニペット:ec2_ami_findは自身のresultsフィールドに結果を置くため

with_items: 
    - "{{ stale_amis.results }}" 

"results": [ 
    { 
     "ami_id": "ami-zzzzzzz", 
     "architecture": "x86_64", 
     "block_device_mapping": { 
      "/dev/xvda": { 
       "delete_on_termination": true, 
       "encrypted": false, 
       "size": 200, 
       "snapshot_id": "snap-xxxxxxxxxxxxx", 
       "volume_type": "gp2" 
      } 
     }, 
     "creationDate": "2017-08-01T15:26:11.000Z", 
     "description": "Weekly backup via Ansible", 
     "hypervisor": "xen", 
     "is_public": false, 
     "location": "111111111111/server-01.example.com-20170801152611Z", 
     "name": "server-01.example.com-20170801152611Z", 
     "owner_id": "111111111111", 
     "platform": null, 
     "root_device_name": "/dev/xvda", 
     "root_device_type": "ebs", 
     "state": "available", 
     "tags": { 
      "Name": "server-01-weekly-20170801152611Z", 
      "Type": "weekly" 
     }, 
     "virtualization_type": "hvm" 
    }, 

答えて

1

私はあなたの試みを疑います。したがって、最初のサーバーの最初のAMIはstale_amisを必要なリストに置き換えてループオーバーするようアドバイスします。たとえば、json_queryフィルタを使用できます。

- ec2_ami: 
    aws_access_key: "{{ access_key }}" 
    aws_secret_key: "{{ secret_key }}" 
    region: "{{ aws_region }}" 
    image_id: "{{ item }}" 
    delete_snapshot: True 
    state: absent 
    with_items: "{{ stale_amis | json_query('results[].results[].ami_id') }}" 
+0

これは完全に機能しました。ありがとうございました! 他の人には、 'python2-jmespath'パッケージをインストールする必要があります –

関連する問題