Hi I have a requirement to write an Ansible code to launch EC2 instances and assign them to available subnets in round robin fashion. There is only 1 VPC which is created manually, however the number of subnets would change depending on the infrastructure getting launched. My hosts file looks like this
[ABC-database]
ABCDB01
[ABC-application]
ABCFE0[1:2]
ABCBE0[1:2]
[cassandra]
CASS0[1:3]
I have also written code to create a subnet file
subnet1: subnet-7284c12b
subnet2: subnet-fd363e98
subnet3: subnet-c892bfbf
What I have to do is pick up one instance at a time, pick up each instances' configurations from all.yml and keep assigning it to each subnet in a cyclic(round robin) fashion.
Currently I have written a shell script to do this. the script counts the number of subnets in the subnet file and returns a new subnet id every time it is called.
I am stuck after this. How should i call this script while launching ec2 instances? the below code throws an error 'next_subnet' is undefined
- name: Launch instances.
local_action:
command: ./get_next_available_subnet.sh
register: next_subnet
module: ec2
region: "{{ region }}"
keypair: "{{ keypair }}"
instance_type: "{{item.instance_type}}"
image: "{{image_id}}"
vpc_subnet_id: "{{ next_subnet.stdout }}"
count: 1
wait: yes
with_items: "{{component_list}}"
Is there a less messy way to achieve this?