jeudi 16 avril 2015

Selecting specific data from an Ansible register

I'd really appreciate some help on correctly selecting data from a register in ansible. The following code almost works... :)


I create an AWS VPC here, I register the information in 'vpc'. I then refer to vpc.



- name: Create VPC and Subnets
ec2_vpc:
state: present
cidr_block: '{{ ip-range}}'
resource_tags: { "Name": "{{ vpc_name }}" }
region: '{{ region }}'
subnets:
- cidr: '{{ pub-subneta }}'
az: '{{ region }}a'
resource_tags: { "Name": "Public Subnet 1" }

- cidr: '{{ pub-subnetb }}'
az: '{{ region }}b'
resource_tags: { "Name": "Public Subnet 2" }

- cidr: '{{ priv-subnet1 }}'
az: '{{ region }}a'
resource_tags: { "Name": "Private Subnet 1" }

- cidr: '{{ priv-subnet2 }}'
az: '{{ region }}b'
resource_tags: { "Name": "Private Subnet 2" }
internet_gateway: True
register: vpc


That works well. It creates the VPC with the 4 subnets. I then want to launch a NAT instance in a specific subnet which I tried to do by specifiying the subnet as follows:



- name: Create NAT instance
ec2:
state: present
key_name: '{{ ssh_key_name }}'
instance_type: '{{ nat_instance_type }}'
image: '{{ nat_ami }}'
region: '{{ region }}'
wait: yes
instance_tags:
Name: "natsrv01"
Description: "NAT Server"
assign_public_ip: yes
source_dest_check: false
vpc_subnet_id: '{{ vpc.subnets[0].id }}'


This is where it doesn't work as intended. I'd assumed that the register would contain invformation in the order it was defined/created but that's not the case.


Using debug I can see that the 4 subnets are in a random order in the register. e.g. on one attempt "Public Subnet 2" was identified by 'vpc.subnets[0].id' and on another attempt "private Subnet 2" was the first in the list.


Can someone suggest how I can reliably & repeatedly select "Public Subnet 1" from the register please?


An example of the data in the register:



"subnets": [
{
"az": "eu-west-1b",
"cidr": REDACTED,
"id": "subnet-REDACTED",
"resource_tags": {
"Name": "Private Subnet 2"
}
},
{
"az": "eu-west-1a",
"cidr": "REDACTED",
"id": "subnet-REDACTED",
"resource_tags": {
"Name": "Public Subnet 1"
}
},
{
"az": "eu-west-1b",
"cidr": "REDACTED",
"id": "subnet-REDACTED",
"resource_tags": {
"Name": "Public Subnet 2"
}
},
{
"az": "eu-west-1a",
"cidr": "REDACTED",
"id": "subnet-REDACTED",
"resource_tags": {
"Name": "Private Subnet 1"
}
},




Aucun commentaire:

Enregistrer un commentaire