Open captainwasabi opened 4 years 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" }
I think this has been answered/resolved.
In the documentation it shows the following:
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).