2017-08-14 288 views
0

我想使用Ansible来获取有关AWS Ec2实例的信息。我真的在寻找它的Instance-ID。我将使用它来循环模板。但我似乎无法获得实例ID。以下是我迄今为止:将变量输出存储到变量

--- 
- name: Including Variables 
    include_vars: 
    file: Linux.yml 

- name: Gathering EC2 Facts 
    ec2_remote_facts: 
    aws_access_key: "{{ access_key }}" 
    aws_secret_key: "{{ secret_key }}" 
    region: us-east-1 
    filters: 
     "tag:Name": "{{ ansible_hostname }}" 
    register: instanceId 
- debug: var=instanceId.instances.id 

我知道这是不正确的,当我运行此我得到:

“instanceId.instances.id”:“变量未定义”

有人可以告诉我一种方法来返回instanceId吗?

回答

1

如果您第一次做某件事,请逐步做......理解里面的内容。像:

- debug: var=instanceId 
# to see raw result and find out that `instances` is there 

- debug: var=instanceId.instances 
# to see what `instanses` is, and to see it is a list 

- debug: var=instanceId.instances[0] 
# to see the first element of the list and see it's properties 

- debug: var=instanceId.instances[0].id 
# to see instance ID 
+0

明白了。感谢你 - 我一直这样做,直到我得到了instancesId.instances,但并不完全确定如何拉动实例ID在这一点上,但这工作得很好:-) – ryekayo