2016-03-02 74 views
3

我正在使用ec2_remote_facts查找基于标签的实例的ec2事实。Ansible ec2_remote_facts

- 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。

感谢,

回答

3

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

此代码剪断已经搞砸缩进和可能混淆的新人。第一部分是一个任务列表(没有播放头像'hosts'等),第二部分有播放头,但任务缩进不正确。我建议将这个代码块分成两部分 - 第一部分是来自某些较大型游戏的任务列表,第二部分是带有更正任务缩进的游戏。 –

+0

另请参阅详细输出的“-v”标志以查看将存储在寄存器中的数据结构。 - 如果您只需要知道返回类型的结构,那么比添加调试语句更容易一些。 – Erich