[英]Ansible querying AWS AMIs
我正在嘗試從 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 }}"
另見此處: https : //bitbucket.org/its-application-delivery/ansible-aws/src/master/ansible/task_find_ami.yml?fileviewer=file-view-default
使用以下動態獲取最新的 AMI。
--- - name: Find latest AMI ec2_ami_facts: owners: 099720109477 region: "{{ AWS_REGION }}" filters: name: "ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*" register: findami - name: Sort the latest AMI set_fact: latest_ami: > {{ findami.images | sort(attribute='creation_date') | last }} - name: Launch Instance with latest AMI ec2: instance_type: "{{ INSTANCE_TYPE }}" image: "{{ latest_ami.image_id }}" key_name: "{{ KEY_NAME }}" region: "{{ AWS_REGION }}" group_id: "{{ sg.group_id }}" wait: yes count: "{{ INSTANCES_COUNT }}" vpc_subnet_id: "{{ subnet.subnet.id }}" assign_public_ip: no
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.