Amazon ec2 将ansible输出存储到变量

Amazon ec2 将ansible输出存储到变量,amazon-ec2,ansible,Amazon Ec2,Ansible,我想使用Ansible来获取有关AWS Ec2实例的信息。我真的在寻找它的实例ID。我将使用它来循环遍历一个模板。但我似乎无法获取实例ID。以下是我目前所拥有的: --- - name: Including Variables include_vars: file: Linux.yml - name: Gathering EC2 Facts ec2_remote_facts: aws_access_key: "{{ access_key }}" aws_secr

我想使用Ansible来获取有关AWS Ec2实例的信息。我真的在寻找它的实例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的方法吗?

如果您是第一次做某事,请慢慢做。。。去了解里面的东西。比如:

- 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
知道了。感谢您的帮助--我一直在这样做,直到我得到instancesId.instances,但当时不确定如何提取实例ID,但效果非常好:-)