Use results from facts

Hi,

I am getting facts from the ec2_vpc_net_facts module which I’m trying to grab the vpc-id from the output and use it as a variable to create a security group. This seems that it should be easy enough and I’ve read that ansible creates variables automatically but the example that I found doesn’t not seem to work here. I thought I’d post here to see if someone else has done this sort of thing.

Thanks so much,
Ladean

---- main.yml

  • name: get vpc_id
    ec2_vpc_net_facts:
    region: “{{ region }}”
    filters:
    “tag:Name”: Svpc

  • debug:
    var=ansible_ec2_vpc_vpc_id

  • name: Create VPC Security Groups
    ec2_group:
    state: present
    name: “{{ env }}sshIn”
    description: allow ssh in from any
    region: “{{ region }}”
    vpc_id: “{{ ansible_ec2_vpc_vpc_id }}”
    rules:

  • proto: tcp
    cidr_ip: x.x.x.x/x
    to_port: 22

---- results

TASK [awsvpc : get vpc_id] *****************************************************
ok: [localhost]

TASK [awsvpc : debug] **********************************************************
ok: [localhost] => {
“ansible_ec2_vpc.vpcs_id”: “VARIABLE IS NOT DEFINED!”
}

TASK [awsvpc : Create VPC Security Groups] *************************************
fatal: [localhost]: FAILED! => {“failed”: true, “msg”: “‘ansible_ec2_vpc_vpc_id’ is undefined”}

After some fighting I found the way to capture the vpc id by registering the output. What I was missing all along in the variable string was the [0].

  • name: get vpc_id
    ec2_vpc_net_facts:
    region: “{{ region }}”
    filters:
    “tag:Name”: Svpc
    register: vpcdata

  • debug:
    var=vpcdata.vpcs[0].id

  • name: Create VPC Security Groups
    ec2_group:
    state: present
    name: “{{ env }}sshIn”
    description: allow ssh in from any
    region: “{{ region }}”
    vpc_id: “{{ vpcdata.vpcs[0].id }}”
    rules:

  • proto: tcp
    cidr_ip: x.x.x.x/x
    from_port: 22
    to_port: 22

Thanks