dmacvicar / terraform-provider-libvirt

Terraform provider to provision infrastructure with Linux's KVM using libvirt
Apache License 2.0
1.59k stars 458 forks source link

Questions about network routes specification #748

Open captainwasabi opened 4 years ago

captainwasabi commented 4 years ago

In the documentation it shows the following:

# (Optional) one or more static routes.
# "cidr" and "gateway" must be specified. The format is:
# routes {
#     cidr = "10.17.0.0/16"
#     gateway = "10.18.0.2"
#   }

if I need two routes, do I just repeat that section for each? or do I just repeat cidr, gateway, cidr, gateway... for each? The corresponding xml tag is <route ...../> so I'm not exactly clear which. I'm asking because I'm writing a jinja2 template for ansible to generate the terraform config file (.tf).

ljluestc commented 1 year ago

To specify multiple static routes in Terraform for the libvirt provider, you would repeat the routes block for each route you want to define. Each routes block contains the cidr and gateway attributes, representing the destination CIDR and gateway IP address for the route, respectively.


resource "libvirt_network" "example" {
  name = "example_network"

  # ... other network configuration ...

  routes {
    cidr    = "10.17.0.0/16"
    gateway = "10.18.0.2"
  }

  routes {
    cidr    = "192.168.1.0/24"
    gateway = "192.168.0.254"
  }
}

Create a Jinja2 template (e.g., network.tf.j2) with the following content:

resource "libvirt_network" "example" {
  name = "example_network"

  # ... other network configuration ...

  {% for route in routes %}
  routes {
    cidr    = "{{ route.cidr }}"
    gateway = "{{ route.gateway }}"
  }
  {% endfor %}
}

n your Ansible playbook, use the template module to render the Jinja2 template and generate the Terraform configuration file:

- name: Generate Terraform configuration file
  hosts: localhost
  gather_facts: false
  tasks:
    - name: Render Terraform template
      template:
        src: path/to/network.tf.j2
        dest: path/to/generated/network.tf
      vars:
        routes:
          - { cidr: "10.17.0.0/16", gateway: "10.18.0.2" }
          - { cidr: "192.168.1.0/24", gateway: "192.168.0.254" }
scabala commented 1 month ago

I think this has been answered/resolved.