Amazon ec2 Ansible ec2\u远程\u事实

Amazon ec2 Ansible ec2\u远程\u事实,amazon-ec2,ansible,ansible-2.x,Amazon Ec2,Ansible,Ansible 2.x,我正在使用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 现在,我想获

我正在使用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


谢谢,

您应该使用注册变量“ec2\u 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'] }}"

这段被剪掉的代码打乱了缩进,可能会让新手感到困惑。第一部分是任务列表(没有播放标题,如
hosts
etc),第二部分有播放标题,但任务缩进不正确。我建议将此代码块分为两部分–第一部分是某个较大的播放中的任务列表,第二部分是具有更正的任务缩进的播放。请参阅详细输出的“-v”标志,以查看将存储在寄存器中的数据结构--如果只需要知道返回类型的结构,则比添加调试语句要容易一些。