title: How to find a free VPC CIDR block with Ansible?
body: <p>I've got an existing set of VPC's in AWS, each with their own CIDR blocks:</p>
  
  <p>vpc1: 20.0.0.0/16
  vpc2: 20.10.0.0/16</p>
  
  <p>Now, I'm trying to write a playbook, which pulls in the existing list of VPC's and then finds an available CIDR (e.g. in the above example, the next available one would be 20.20.0.0/16, though I'm not too concerned about keeping it sequential).</p>
  
  <p>I know that doing this works with two lists:</p>
  
  <pre><code>- name: Loop over all possible CIDRs
      debug: msg="Found a free CIDR {{ item }}"
      with_items: all_potential_cidrs
      when: "\"{{ item }}\" not in {{ currently_used_cidrs }}"
  </code></pre>
  
  <p>However, the way I'm getting the list of existing CIDRs is:</p>
  
  <pre><code>- name: Get list of VPCs and their CIDR blocks
    command: aws ec2 describe-vpcs --output json
    register: cli_output
  
  - name: Register variables
    set_fact:
    current_vpcs: "{{ cli_output.stdout | from_json }}"
  </code></pre>
  
  <p>The command gives the following data back (in JSON format):</p>
  
  <pre><code>{
      "Vpcs": [
          {
              "VpcId": "vpc-4aad0c23",
              "InstanceTenancy": "default",
              "Tags": [
                  {
                      "Value": "vpc1",
                      "Key": "Name"
                  }
              ],
              "State": "available",
              "DhcpOptionsId": "dopt-ff6b238f",
              "CidrBlock": "20.0.0.0/16",
              "IsDefault": false
          },
          {
              "VpcId": "vpc-d4101abc",
              "InstanceTenancy": "default",
              "Tags": [
                  {
                      "Value": "vpc2",
                      "Key": "Name"
                  }
              ],
              "State": "available",
              "DhcpOptionsId": "dopt-eaaab38c",
              "CidrBlock": "20.10.0.0/16",
              "IsDefault": false
          }
      ]
  }
  </code></pre>
  
  <p>Which allows getting all CIDR blocks as follows:</p>
  
  <pre><code>- name: Print filtered VPCs and their subnets
    debug: msg="VPC ID {{ item.VpcId }}, VPC CIDR block {{ item.CidrBlock }}"
    with_items: current_vpcs.Vpcs
  </code></pre>
  
  <p>However, as "CidrBlock" is a property of a list item, I'm unable to use it in the "when" statement, which requires a List:</p>
  
  <pre><code>when: "{{ item }}" not in {{ list_of_cidrs }}"
  </code></pre>
  
  <p>How can I take the "CidrBlock" property of each of the "Vpcs" items and turn it into it's own list, in order to pass it to the "when: ... not in ..." statement?</p>