title: Replacing Ansible with Terraform scripts
body: <p>I was recently given a project to essentially rewrite all the ansible code the team is using to create infrastructure in terraform. I always thought ansible was just for configuring servers but it seems that its also able to create cloud resources much like terraform. The trouble is that it seems to be able to do tasks which I dont think are possible in terraform and I'm not sure how to approach it.</p>
  <p>For example one of the scripts creates a vnet in Azure which is something I can easily translate into terraform. However its also doing things like calculating CIDR blocks.
  Specifically it looks like its getting a list of all possible VNETS and then getting a list of my Azure subscriptions and the associated existing VNETS. Then its filtering those out of the initial list
  Can terraform do something similar or would I need to invoke some kind of script or call ansible from Terraform?</p>
  <pre><code>- name: Calulate cidr for new vnet 
    block:
  
  - name: Calulate number of available vnets in vnet_base
    set_fact:
      gen_vnet_count: &quot;{{ vnet_base | ipsubnet(subnet_size | basename) }}&quot;
  
  - name: Generate all available subnets
    set_fact: 
      gen_vnet_list: &quot;{{ gen_vnet_list  | default([]) + [vnet_base | ipsubnet(subnet_size | basename, item)] }}&quot;
    loop: &quot;{{ range(0, gen_vnet_count | int) | list }}&quot;
  
  - name: List all available subscriptions
    command: &gt;
      az account list --query &quot;[*].id&quot;
    register: subscription_list
    retries: 2
    delay: 30
    until: subscription_list is succeeded
  
  - name: Get existing ccs vnet's from all subscriptions
    azure_rm_virtualnetwork_info:
      subscription_id: &quot;{{ item }}&quot;
      client_id: &quot;{{ vault_azure_client_id }}&quot;
      tenant: &quot;{{ vault_azure_tenant_id }}&quot;
      secret: &quot;{{ vault_azure_client_secret }}&quot;
      tags:
        - &quot;deploy:ccsvnet&quot;
    loop: &quot;{{ subscription_list.stdout }}&quot;
    register: existing_vnets
    retries: 2
    delay: 30
    until: existing_vnets is succeeded 
  
  - name: Filter out existing vnet CIDR ranges
    set_fact:
      existing_vnet_list: &quot;{{ existing_vnets | json_query('results[*].virtualnetworks[].address_prefixes[0]') }}&quot;
      
  - name: Set selected_cidr
    set_fact:
      selected_cidr: &quot;{{ (gen_vnet_list | difference(existing_vnet_list)) | first }}&quot;
  </code></pre>