DeirdreRodgers
DeirdreRodgers

Reputation: 451

Replacing Ansible with Terraform scripts

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.

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?

- name: Calulate cidr for new vnet 
  block:

- name: Calulate number of available vnets in vnet_base
  set_fact:
    gen_vnet_count: "{{ vnet_base | ipsubnet(subnet_size | basename) }}"

- name: Generate all available subnets
  set_fact: 
    gen_vnet_list: "{{ gen_vnet_list  | default([]) + [vnet_base | ipsubnet(subnet_size | basename, item)] }}"
  loop: "{{ range(0, gen_vnet_count | int) | list }}"

- name: List all available subscriptions
  command: >
    az account list --query "[*].id"
  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: "{{ item }}"
    client_id: "{{ vault_azure_client_id }}"
    tenant: "{{ vault_azure_tenant_id }}"
    secret: "{{ vault_azure_client_secret }}"
    tags:
      - "deploy:ccsvnet"
  loop: "{{ subscription_list.stdout }}"
  register: existing_vnets
  retries: 2
  delay: 30
  until: existing_vnets is succeeded 

- name: Filter out existing vnet CIDR ranges
  set_fact:
    existing_vnet_list: "{{ existing_vnets | json_query('results[*].virtualnetworks[].address_prefixes[0]') }}"
    
- name: Set selected_cidr
  set_fact:
    selected_cidr: "{{ (gen_vnet_list | difference(existing_vnet_list)) | first }}"

Upvotes: 0

Views: 528

Answers (1)

RamaraoAdapa
RamaraoAdapa

Reputation: 3129

Thank you for your comment @clockworknet.

You can use Terraform to calculate CIDR blocks.

Below are the available functions to calculate CIDR blocks :

cidrhost : cidrhost calculates a full host IP address for a given host number within a given IP network address prefix

cidrnetmask : cidrnetmask converts an IPv4 address prefix given in CIDR notation into a subnet mask address

cidrsubnet : cidrsubnet calculates a subnet address within given IP network address prefix

cidrsubnets : cidrsubnets calculates a sequence of consecutive IP address ranges within a particular CIDR prefix

I tested with cidrsubnet function in my environment for calculation of CIDR blocks for subnet

enter image description here

Reference : cidrhost - Functions - Configuration Language - Terraform by HashiCorp

Upvotes: 1

Related Questions