Neel
Neel

Reputation: 113

How to spin up multiple aws instances and assign given range of IP addresses using ansible?

The objective is to spin up multiple instances which can be achieved using count but I have been give specific range of private IP addresses, and want to assign them to the instances.

Below is my present playbook,

---
  - name: Provision an EC2 Instance
    hosts: local
    connection: local
    gather_facts: False
    tags: provisioning
    # Necessary Variables for creating/provisioning the EC2 Instance
    vars:
      instance_type: t2.micro
      security_group: default # Change the security group name here
      image: ami-a9d276c9 # Change the AMI, from which you want to launch the server
      region: us-west-2 # Change the Region
      keypair: ansible # Change the keypair name
      ip_addresses:
        - 172.31.1.117/32
        - 172.31.1.118/32
      count: 2

    tasks:

      - name: Launch the new EC2 Instance
        local_action: ec2
                      group={{ security_group }}
                      instance_type={{ instance_type}}
                      image={{ image }}
                      wait=true
                      region={{ region }}
                      keypair={{ keypair }}
                      count={{count}}
                      vpc_subnet_id=subnet-xxxxxxx
#                      private_ip={{private_ip}}
        with_items: ip_addresses
        register: ec2

      - name: Wait for SSH to come up
        local_action: wait_for
                      host={{ item.public_ip }}
                      port=22
                      state=started
        with_items: ec2.instances

      - name: Add tag to Instance(s)
        local_action: ec2_tag resource={{ item.id }} region={{ region }} state=present
        with_items: ec2.instances
        args:
          tags:
            Name: ansible

      - name: Update system
        apt: update_cache=yes

      - name: Install Git
        apt:
          name: git
          state: present

      - name: Install Python2.7
        apt:
          name: python=2.7
          state: present

      - name: Install Java
        apt:
          name: openjdk-8-jdk
          state: present

Which is although bringing up the instances but not assigning the IP addresses intended to be assigned. and I'm getting following warning

PLAY [Provision an EC2 Instance] ***********************************************

TASK [Launch the new EC2 Instance] *********************************************
changed: [localhost -> localhost] => (item=172.31.1.117/32)
changed: [localhost -> localhost] => (item=172.31.1.118/32)
[DEPRECATION WARNING]: Skipping task due to undefined attribute, in the future this will be a fatal error.. This feature will be removed in a future release. Deprecation warnings can be 
disabled by setting deprecation_warnings=False in ansible.cfg.

Please suggest me the best possible way to achieve this.

Upvotes: 1

Views: 1029

Answers (1)

helloV
helloV

Reputation: 52423

  • You are giving count=2, so 2 instances will be launched
  • Your IP addresses are wrong, you are giving a CIDR instead of IP
  • You are not using the IP address anywhere in your code when launching the instances

How to fix?

  ip_addresses:
    - 172.31.1.117
    - 172.31.1.118
  • Don't specify count in ec2 module
  • Loop through the list of ipaddresses (there are 2 of them)
  • Make sure you use the IP by referencing {item}

Like this:

private_ip={{item}}

Upvotes: 1

Related Questions