Ansible ec2_remote_facts

Ansible ec2_remote_facts

我正在使用 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 并将其存储在一个变量中以在我的剧本中使用它。

有人可以帮助查找或提取实例 ID。

谢谢,

你应该使用你注册的变量'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'] }}"