Ansible查询AWS AMI [英] Ansible querying AWS AMIs

查看:128
本文介绍了Ansible查询AWS AMI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Ansible查询AWS EC2 AMI,但是在遍历结果时始终会遇到错误:

I'm trying to query AWS EC2 AMIs from Ansible but keep running into an error when looping through the results:

- hosts: localhost

  tasks:
   - name: Get AMI
     ec2_ami_facts:
        owner: amazon
        filters:
           architecture: x86_64
           root-device-type: ebs
     register: amis

   - name: return filtered data
     debug:
        msg: "{{ item }}"
     loop: " {{ amis \
        | json_query( 'Images[?Description!=`null`] \
        | [?starts_with(Description,`Amazon Linux`)]' ) \
        }} "

想法是返回图像文档,然后返回具有更多过滤功能的图像ID(最终目标是获取给定描述的最新ami id).但是在当前示例以及其他任何尝试下,我都会收到此错误:

The idea is to return the image documents, and later just the image IDs with more filtering (end goal is to get the most recent ami id for a given description). But with the current example, and anything else I try I get this error:

TASK [return filtered data] ****************************************************
fatal: [localhost]: FAILED! => {"msg": "Invalid data passed to 'loop',
 it requires a list, got this instead:   . Hint: If you passed a
 list/dict of just one element, try adding wantlist=True to your lookup
 invocation or use q/query instead of lookup."}

我可以完整地查看"amis",它看起来不错,但是我尝试进行的任何过滤都会失败.正确的方法是什么?

I can look at the 'amis' in its entirety and it looks good, but any filtering I try fails. What is the correct method?

推荐答案

此方法有效,感谢freenode上#ansible的人们.

This works, thanks for the folks at #ansible on freenode.

- hosts: localhost

  tasks:
   - name: Get AMI
     ec2_ami_facts:
        owner: amazon
        filters:
           architecture: x86_64
           root-device-type: ebs
     register: amis

   - name: return latest AMI
     set_fact:
        my_ami: "{{ amis.images \
            | selectattr('description', 'defined') \
            | selectattr('description', 'match', '^Amazon Linux.*GP2$') \
            | selectattr('description', 'match', '[^(Candidate)]') \
            | sort(attribute='creation_date') \
            | last }} "

   - debug:
        msg: "ami = {{ my_ami | to_nice_yaml }}"

也请参见此处: https://bitbucket.org/its-application-delivery/ansible-aws/src/master/ansible/task_find_ami.yml?fileviewer=file-view-default

这篇关于Ansible查询AWS AMI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆