title: AWS AMI Cleanup w/Ansible iterate through results array
body: <p>I have a previous task that creates weekly backups, labeling them with the server name followed by a date/time tag. The goal of this job is to go in behind it and clean up the old AMI backups, leaving only the last 3. The <code>ec2_ami_find</code> task works fine, but it could also return empty results for some servers and I'd like the deregister task to handle that.</p>
  
  <p>The error I'm getting is pretty generic:</p>
  
  <blockquote>
    <p>fatal: [127.0.0.1]: FAILED! => {
        "failed": true,
        "msg": "The conditional check 'item.ec2_ami_find.exists' failed. The error was: error while evaluating conditional
    (item.ec2_ami_find.exists): 'dict object' has no attribute
    'ec2_ami_find'\n\nThe error appears to have been in
    '/root/ansible/ec2-backups-purge/roles/first_acct/tasks/main.yml': line 25,
    column 3, but may\nbe elsewhere in the file depending on the exact
    syntax problem.\n\nThe offending line appears to be:\n\n\n- name:
    Deregister old backups\n  ^ here\n"</p>
  </blockquote>
  
  <p>The playbook task reads as follows:</p>
  
  <pre><code>---
  - 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 }}"
  </code></pre>
  
  <p>Snippet of one of the results returns:</p>
  
  <pre><code>"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"
      },
  </code></pre>