amazon-web-services - Ansible 查询 AWS AMI
问题描述
我正在尝试从 Ansible 查询 AWS EC2 AMI,但在遍历结果时不断遇到错误:
- 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)。但是对于当前的示例,以及我尝试的其他任何方法,我都会收到此错误:
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”,它看起来不错,但是我尝试的任何过滤都失败了。什么是正确的方法?
解决方案
这很有效,感谢 freenode 上#ansible 的人们。
- 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 }}"
推荐阅读
- firebase - 在 Firebase Analytics 中识别用户 ID 之外的用户的友好方式?
- java - 如何解决OncePerRequestFilter抽象类Autowired获取空指针异常?
- python - 通过回溯理解排列
- pyspark - 错误:类型错误 when() 在 PySpark 中缺少 1 个必需的位置参数“值”
- bash - 用 Shell 变量替换 yml 文件中的值
- java - 为什么计算复杂度为 O(n^4)?
- json - 为什么将 JSONB 中的显式“null”提取为文本会产生 SQL“null”?
- azure - 如何使用 Dispatch 查找两个 LUIS 应用程序的意图?
- android - How to build a miniprogram (aka, tinyapp) framework in an app?
- c++ - 在android虚拟机上运行时的错误颜色