Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: allocating an elastic ip to newly created instance

I am creating a new instance with ansible and want to associate an elastic ip to it. What value should i write in instance_id? instance_id: "{{ newinstance.instances[0].id }}" ??? But this value seems to be wrong, because i have an output after checking:

TASK: [Allocating elastic IP to instance]     *************************************
fatal: [localhost] => One or more undefined variables: 'dict object' has no attribute 'instances'

---
- name: Setup an EC2 instance
  hosts: localhost
  connection: local
  tasks:

    - name: Create an EC2 machine
      ec2:
        aws_access_key: my_access_key
        aws_secret_key: my_secret_key
        key_name: my_key
        instance_type: t1.micro
        region: us-east-1
        image: some_ami
        wait: yes
        vpc_subnet_id: my_subnet
        assign_public_ip: yes
      register: newinstance

    - name: Allocating elastic IP to instance
      ec2_eip:
        aws_access_key: my_access_key
        aws_secret_key: my_secret_key
        in_vpc: yes
        reuse_existing_ip_allowed: yes
        state: present
        region: us-east-1
        instance_id: "{{ newinstance.instances[0].id }}"
      register: instance_eip
    - debug: var=instance_eip.public_ip

    - name: Wait for SSH to start
      wait_for:
        host: "{{ newinstance.instances[0].private_ip }}"
        port: 22
        timeout: 300
        sudo: false
      delegate_to: "127.0.0.1"

    - name: Add the machine to the inventory
      add_host:
        hostname: "{{ newinstance.instances[0].private_ip }}"
        groupname: new

What should i put instead "{{ newinstance.instances[0].id }}"? The same question is about "{{ newinstance.instances[0].private_ip }}".

like image 988
Serko Avatar asked Oct 31 '25 05:10

Serko


1 Answers

You are basically trying to parse data from the JSON output of Ansible task which is given to your variable. instance_ids is an array and child of newinstance JSON. Similarly private_ip is a direct child of newinstance

---
- name: Setup an EC2 instance
  hosts: localhost
  connection: local
  tasks:

 - name: Create an EC2 machine
   ec2:
    aws_access_key: my_access_key
    aws_secret_key: my_secret_key
    key_name: my_key
    instance_type: t1.micro
    region: us-east-1
    image: some_ami
    wait: yes
    vpc_subnet_id: my_subnet
    assign_public_ip: yes
  register: newinstance

- name: Allocating elastic IP to instance
  ec2_eip:
    aws_access_key: my_access_key
    aws_secret_key: my_secret_key
    in_vpc: yes
    reuse_existing_ip_allowed: yes
    state: present
    region: us-east-1
    instance_id: "{{ newinstance.instance_ids[0] }}"
  register: instance_eip
- debug: var=instance_eip.public_ip

- name: Wait for SSH to start
  wait_for:
    host: "{{ newinstance.private_ip }}"
    port: 22
    timeout: 300
    sudo: false
  delegate_to: "127.0.0.1"

- name: Add the machine to the inventory
  add_host:
    hostname: "{{ newinstance.private_ip }}"
    groupname: new
like image 86
Rahul Mehrotra Avatar answered Nov 02 '25 19:11

Rahul Mehrotra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!