ryekayo
ryekayo

Reputation: 2421

Store ansible output to variable

I want to use Ansible that would get information about an AWS Ec2 instance. I am really looking for it's Instance-ID. I am going to use this to loop through a template. But I can't seem to get the instance-ID. Here is what I have so far:

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

I know this is incorrect as when I run this i get:

"instanceId.instances.id": "VARIABLE IS NOT DEFINED!"

Can someone tell me of a way to return the instanceId?

Upvotes: 0

Views: 585

Answers (1)

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68269

If you do something for the first time, do it gradually... To understand what's inside. Like:

- 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

Upvotes: 1

Related Questions