我已经导入了实例列表,并且已经将ec2_instance_fact一起用于实例事实。
现在我需要获取实例类型,私有IP以及实例的所有详细信息,以便我可以删除它并使用加密的数据创建一次新的
下面是一半的代码。
- hosts: localhost
connection: local
gather_facts: false
vars_files:
- instaceId.yaml
tasks:
- name: print module name one by one
debug:
msg: "{{ item }}"
with_items: "{{ Instance }}"
- name: get metadata of Instance
ec2_instance_facts:
instance_ids: "{{ item }}"
with_items: "{{ Instance }}"
register: ec2_metadata
- name: Record Users Access Keys
debug: var= "{{ ec2_metadata.results[{{ item }}].instances[0].instance_id }}"
with_sequence: start=0 end={{ec2_metadata.results|length -1}}
- ec2:
region: "{{ ec2_metadata.results[{{ item }}].instances[0].placement.availability_zone }}"
state: absent
instance_id: "{{ ec2_metadata.results[{{ item }}].instances[0].instance_id }}"
with_sequence: start=0 end={{ ec2_metadata.results|length -1 }}
理想情况下,我认为应该采取
ok: [localhost] => (item=None) => {
"ec2_metadata.results[0].instances[0].instance_id": "i-03dbfd81f19297092"
仅i-03dbfd81f19297092
有人可以建议我继续吗
我遇到了错误
致命:[localhost]:失败! => {“ msg”:“模板字符串时发生模板错误:预期令牌':',得到了'}'。字符串:\” {{ec2_metadata.results [{{item}}]。instances [0] .instance_id}} \“”}
答案 0 :(得分:0)
在行的扩展内扩展变量是错误的语法。
debug: var= "{{ ec2_metadata.results[{{ item }}].instances[0].instance_id }}"
正确
debug: var= "{{ ec2_metadata.results[item].instances[0].instance_id }}"
调试:var =“” {{ec2_metadata.results [{{item}}]。instances [0] .instance_id}}“对我来说很好用
A:下面的播放再现了错误。
- hosts: localhost
vars:
list:
- 'AAA'
- 'BBB'
item: 0
tasks:
- debug:
msg: "{{ list[item] }}"
- debug:
msg: "{{ list[{{ item }}] }}"
失败
致命:[localhost]:失败! => {“ msg”:“模板字符串时发生模板错误:预期标记':',得到了'}'。字符串:{{list [{{item}}]}}”}
答案 1 :(得分:0)
使用ansible的循环模块来迭代所需的属性
- name: Record Users Access Keys
debug:
msg: "{{ item.instances[0].instance_type }} {{ item.instances[0].key_name }}"
loop: "{{ ec2_metadata.results }}"