我正在使用ec2_remote_facts根据标签查找实例的ec2事实。
- name: Search ec2
ec2_remote_facts:
filters:
"tag:Name": "{{hostname}}"
aws_access_key: "{{aws_access_key}}"
aws_secret_key: "{{aws_secret_key}}"
region: "{{aws_region}}"
register: ec2_info
现在我想获取该特定主机的实例ID的实例ID,并将其存储在变量中以在我的playbook中使用它。
有人可以帮助您查找或提取实例ID。
谢谢,
答案 0 :(得分:4)
您应该使用注册变量' ec2_info':
- debug: var=ec2_info
- debug: var=item
with_items: ec2_info.instance_ids
# register new variable
- set_fact:
instance_ids: ec2_info.instance_ids
- add_host: hostname={{ item.public_ip }} groupname=ec2hosts
with_items: ec2_info.instances
- name: wait for instances to listen on port:22
wait_for:
state=started
host={{ item.public_dns_name }}
port=22
with_items: ec2_info.instances
# Connect to the node and gather facts,
# including the instance-id. These facts
# are added to inventory hostvars for the
# duration of the playbook's execution
- hosts: ec2hosts
gather_facts: True
user: ec2-user
sudo: True
tasks:
# fetch instance data from the metadata servers in ec2
- ec2_facts:
# show all known facts for this host
- debug: var=hostvars[inventory_hostname]
# just show the instance-id
- debug: msg="{{ hostvars[inventory_hostname]['ansible_ec2_instance-id'] }}"