Ansible 查询 AWS AMI [英] Ansible querying AWS AMIs
问题描述
我正在尝试从 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 }}"
这篇关于Ansible 查询 AWS AMI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!