XP_2600
XP_2600

Reputation: 67

Ansible ec2_instance_facts

I have a basic Ansible playbook creating an ec2 instance in aws, its working fine, i want to pick the public ip from the created instance to do furthers task on it, i am using the following section:

  - name: get-facts
    ec2_instance_facts:
      region: eu-west-1
      aws_access_key: xxxxxxxxx
      aws_secret_key: xxxxxxx
      filters:
        "tag:Name": docker-server
    register: ec2
  - debug:   var=ec2.instances

But i want to get just the ip address, according to the documentation public_ip is a string, but i cannot call it when i use ec2.instances.public_ip i get error: "VARIABLE IS NOT DEFINED!" what can i do to get the public ip for my created ec2 instance ? thanks.

Upvotes: 0

Views: 892

Answers (2)

XP_2600
XP_2600

Reputation: 67

Thank you for your support and clarification, its finally worked like this:

- name: get-facts
    ec2_instance_info:
      region: xxxxx
      aws_access_key: xxxxxxxxxxx
      aws_secret_key: xxxxxxxxxxx
      filters:
        "tag:Name": docker-server
    register: ec2
  - debug: var="ec2.instances[0].public_ip_address"

Upvotes: 0

It is ec2.instances.public_ip_address and public_ip

See: https://docs.ansible.com/ansible/2.9/modules/ec2_instance_info_module.html#return-instances/public_ip_address


ec2_instance_facts deprecated, ansible offer to use ec2_instance_info See: https://docs.ansible.com/ansible/2.9/modules/ec2_instance_facts_module.html


UPDATE:

  - name: Print info
    debug: var="ec2.instances[0].public_ip_address"
  - name: Loop instances
    debug:
      var: item.public_ip_address
    with_items: "{{ ec2.instances }}"

Upvotes: 1

Related Questions