ec2_vpc :: Different output format when using with_items

Hi All,

I have noticed a “strange” behaviour in one of my playbooks that use the ec2_vpc module. After changing how to input data the output format changed.

I’m not sure if this is expected or not but couldn’t find any documentation around it. Perhaps someone encountered the same scenario before ?

Let me explain.

I had a playbook to create a VPC that I was using variables for CIDR Block, Internet Gateway (yes/no) and two Tags (Name and Environment). After running the playbook I was able to retrieve the vpc_id from {{ ec2_vpc_out.vpc_id }}.

Request — without using with_items:

35 # create the VPC
36 - name: VPC | Create the required VPC
37 ec2_vpc:
38 aws_access_key: “{{ aws_access_key }}”
39 aws_secret_key: “{{ aws_secret_key }}”
40 region: “{{ aws_region }}”
41 state: present
42 cidr_block: “{{ aws_vpc_cidr_block }}”
43 resource_tags:
44 Name: “{{ aws_vpc_name }}”
45 Environment: “{{ aws_vpc_subnet_envname }}”
46 internet_gateway: “{{ aws_vpc_internet_gateway }}”
47 subnets:
48 “{{ my_subnets }}”
49 route_tables:
50 “{{ aws_route_table_info }}”
51 register: ec2_vpc_out

Response — without using with_items**:**

ok: [localhost] => {
“msg”: {
“changed”: false,
“igw_id”: “igw-dddddddd”,
“subnets”: [
{
“az”: “ap-southeast-2a”,
“cidr”: “10.125.0.0/24”,
“id”: “subnet-bbbbbbbb”,
“resource_tags”: {
“Environment”: “Test",
“Name”: “Subnet-Test-A",
“TierName”: “Test",
“TierType”: “Test"
}
},

(…)

],
“vpc”: {
“cidr_block”: “10.125.0.0/16”,
“dhcp_options_id”: “dopt-99999999”,
“id”: “vpc-55555555”,
“region”: “ap-southeast-2”,
“state”: “available”
},
“vpc_id”: “vpc-55555555”
}
}

I changed this playbook to read those variables for CIDR Block, Internet Gateway (yes/no) and two Tags (Name and Environment) from a dictionary and after this the output format changed.

Dictionary:

19 aws_vpc_info:
20 - cidr_block: “{{ aws_vpc_cidr_block }}”
21 vpc_internet_gateway: yes
22 resource_tags:
23 Name: “{{ aws_iac_fullstackname }}-{{ aws_nc_vpc }}”
24 Environment: “{{ aws_vpc_subnet_envname }}”
25 “CIDR_BLOCK”: “{{ aws_vpc_cidr_block }}”

Request — with_items:

35 # create the VPC
36 - name: VPC | Create the required VPC
37 ec2_vpc:
38 aws_access_key: “{{ aws_access_key }}”
39 aws_secret_key: “{{ aws_secret_key }}”
40 region: “{{ aws_region }}”
41 state: present
42 cidr_block: “{{ item.cidr_block }}”
43 resource_tags: “{{ item.resource_tags }}”
44 internet_gateway: “{{ item.vpc_internet_gateway }}”
45 subnets:
46 “{{ my_subnets }}”
47 route_tables:
48 “{{ aws_route_table_info }}”
49 with_items: aws_vpc_info
50 register: ec2_vpc_out

Response — with_items**:**

ok: [localhost] => {
“msg”: {
“changed”: false,
“msg”: “All items completed”,
“results”: [
{
“_ansible_no_log”: false,
“changed”: false,
“igw_id”: “igw-dddddddd”,
“invocation”: {
“module_args”: {
“aws_access_key”: “AAAAAAAAAAAAAAAAAAAA”,
“aws_secret_key”: “VALUE_SPECIFIED_IN_NO_LOG_PARAMETER”,
“cidr_block”: “10.125.0.0/16”,
“dns_hostnames”: true,
“dns_support”: true,
“ec2_url”: null,
“instance_tenancy”: “default”,
“internet_gateway”: true,
“profile”: null,
“region”: “ap-southeast-2”,
“resource_tags”: {
“Environment”: “Test",
“Name”: “Test-VPC”,
“CIDR_BLOCK”: “10.125.0.0/16”

(…)

],
“vpc”: {
“cidr_block”: “10.125.0.0/16”,
“dhcp_options_id”: “dopt-99999999”,
“id”: “vpc-55555555”,
“region”: “ap-southeast-2”,
“state”: “available”
},
“vpc_id”: “vpc-55555555”
}
]
}
}

After the change to the playbook I had to change the return vpc_id information from {{ ec2_vpc_out.vpc_id }} to {{ ec2_vpc_out.results.0.subnets }}.

As you can see, the output are radically different between each other. Nothing else was changed and I can run both playbooks and despite the output both works as expected.

Does anyone know if this is a bug or if it’s expected ? If it is expected, can someone point me some documentation so I can handle this changes ?

Thank you,
—Gustavo Aguiar