Selecting a variable from register

23 views
Skip to first unread message

Kjartan Clausen

unread,
Dec 14, 2015, 10:54:23 AM12/14/15
to Ansible Project
I'm trying to automate creating an AWS VPC with routes, subnets, instances, RDS, etc.
so I created the following yml:

---

-   hosts: local
    connection: local
#    gather_facts: False
    tags: provisioning
    # Which region and AZ's should this VPC be in?
    vars:
      region: us-west-1
      Azone1: us-west-1c
      primary: 1c
      Azone2: us-west-1b
      secondary: 1b
      vpc_name: "Test"
      cidr: "94"

    tasks:
    - name: Create VPC
      ec2_vpc:
        state: present
        cidr_block: 10.{{ cidr }}.0.0/16
        resource_tags:
          Name: "{{ vpc_name }}"
        subnets:
          - cidr: 10.{{ cidr }}.220.0/24
            az: "{{ Azone1 }}"
            resource_tags:
              Name: "{{ vpc_name }}_NAT_{{ primary }}"
          - cidr: 10.{{ cidr }}.221.0/24
            az: "{{ Azone2 }}"
            resource_tags:
              Name: "{{ vpc_name }}_NAT_{{ secondary }}"
        internet_gateway: True
        route_tables:
          - subnets:
              - 10.{{ cidr }}.220.0/24
              - 10.{{ cidr }}.221.0/24
            routes:
              - dest: 0.0.0.0/0
                gw: igw
        region: "{{ region }}"
      register: vpc

    - debug: var=vpc.subnets

# Problem statement
    - debug: var=vpc.subnets.id
      when: vpc.subnets.az == "{{Azone1}}"


In order to create an instance on a given subnet I need to be able to select that subnet, but my selection statement doesn't work.
Output is:
PLAY [local] ******************************************************************

GATHERING FACTS ***************************************************************
ok: [10.10.20.10]

TASK: [Create VPC] ************************************************************
changed: [10.10.20.10]

TASK: [debug var=vpc.subnets] *************************************************
ok: [10.10.20.10] => {
    "var": {
        "vpc.subnets": [
            {
                "az": "us-west-1c",
                "cidr": "10.94.220.0/24",
                "id": "subnet-6555e73c",
                "resource_tags": {
                    "Name": "Test_NAT_1c"
                }
            },
            {
                "az": "us-west-1b",
                "cidr": "10.94.221.0/24",
                "id": "subnet-3983fe5c",
                "resource_tags": {
                    "Name": "Test_NAT_1b"
                }
            }
        ]
    }
}

TASK: [debug var=vpc.subnets.id] **********************************************
fatal: [10.100.200.10] => error while evaluating conditional: vpc.subnets.az == "us-west-1c"

FATAL: all hosts have already failed -- aborting

What do I need to do in order to get the us-west-1c subnet id as output?

Arbab Nazar

unread,
Dec 14, 2015, 12:34:39 PM12/14/15
to Ansible Project
Can you please try this:

    - debug: var="{{ item.id }}"
      when: vpc.subnets.az == "{{Azone1}}"
      with_items: vpc.subnets
for individual you can get like this but I am not sure:
    - debug: var=vpc.subnets[0].id
      when: vpc.subnets.az == "{{Azone1}}"

For complete example, please refer this:
Thanks,
Reply all
Reply to author
Forward
0 new messages