Capturing private ip of an ansible created ec2 instance

38 views Asked by At

I have a playbook to provision an EC2 instance using ansible. The task I paste here minus authentication information:

amazon.aws.ec2_instance:
  region: "{{ region }}"
  state: running
  wait: true
  count: 1
  image_id: "{{ image_id }}"
  instance_type: t2.mini
  name: new-ec2-instance
register: ec2

I need to access the private ip of the new instance. I have inspected the JSON contained in the ec2 variable and I know the private ip of an instance can be obtained at:

ec2.instances[0].network_interfaces[0].private_ip_address

However, the problem I am running into is that the "instances" list contains more then just the newly created ec2. It seems to contain all the instances that I have created. Therefore I cannot seem to reliably use the 0 index.

Can anyone say how to reliably get the private IP of the newly created instnace?

0

There are 0 answers