I am new in Terraform. I am trying to setup some scripts for managing Cisco Catalyst Center (new name of Cisco DNA, but I am using the DNA Provider anyway :) ). I used an existing repo, that I've modified. I have structure that looks like this:
├── main.tf
├── variables.tf
├── modules
│ └── site_settings
│ └── site.tf
│ └── variables.tf
├── Site-1
│ └── site-1.tfvars
In ./main.tf I have:
provider "dnacenter" {
username = var.dnac_username
password = var.dnac_password
base_url = var.dnac_url
debug = "true"
ssl_verify = "false"
}
module "site_settings" {
# Using m0_general_settings module to configure general settings
source = "../modules/site_settings"
dnac_username = var.dnac_username
dnac_password = var.dnac_password
dnac_url = var.dnac_url
global_name = var.global_name
region_name = var.region_name
country_name = var.country_name
city_name = var.city_name
site_name = var.site_name
buildings = var.buildings
building_name = var.building_name
}
The ./modules/site_settings/site.tf has:
resource "dnacenter_area" "country" {
provider = dnacenter
parameters {
site {
area {
name = var.country_name
parent_name = "${var.global_name}/${var.region_name}"
}
}
type = "area"
}
}
resource "dnacenter_area" "city" {
depends_on = [ dnacenter_area.country ]
provider = dnacenter
parameters {
site {
area {
name = var.city_name
parent_name = "${var.global_name}/${var.region_name}/${var.country_name}"
}
}
type = "area"
}
}
resource "dnacenter_area" "site" {
depends_on = [ dnacenter_area.city ]
provider = dnacenter
parameters {
site {
area {
name = var.site_name
parent_name = "${var.parent_name}/${var.global_name}/${var.country_name}/${var.city_name}"
}
}
type = "area"
}
}
** resource "dnacenter_building" "building" {
depends_on = [ dnacenter_area.site ]
for_each = { for building in var.buildings : building.name => building }
provider = dnacenter
parameters {
site {
building {
name = "${each.value.building_name}"
parent_name = "${var.global_name}/${var.region_name}/${var.country_name}/${var.city_name}/${each.value.parent_site_name}"
latitude = "${each.value.latitude}"
longitude = "${each.value.longitude}"
}
}
type = "building"
}
}**
These are the varialbe values in ./site-1.tfvars :
dnac_username = "Cisco"
dnac_password = "Cisco123"
dnac_url = "https://my-dna-lab.test.net"
global_name = "Global"
region_name = "EMEA"
country_name = "Spain"
city_name = "Madrid"
site_name = "Site-1"
buildings = [
{
building_name = "B01"
parent_site_name = "Site-1"
latitude = "xx.xxxx"
longitude = "xx.xxxx"
},
{
building_name = "B02"
parent_site_name = "Site-1"
latitude = "xx.xxxx"
longitude = "xx.xxxx"
},
]
The variables are defined in both ./variables.tf and ./modules/site_settings/variables.tf ... not sure why :) :
variable "dnac_username" {
sensitive = true
}
variable "dnac_password" {
sensitive = true
}
variable "dnac_url" {
type = string
}
variable "global_name" {
type = string
}
variable "region_name" {
type = string
}
variable "country_name" {
type = string
}
variable "city_name" {
type = string
}
variable "site_name" {
type = string
}
variable "building_name" {
type = string
}
variable "parent_site_name" {
type = string
}
I have the variables.tf in the root and in the subfolder, not quite sure why .... but regardless if I keep them in only one of those I am still asked for the variables, even though I specify I want to use the ones from the site-1.tfvars file:
~/Doc/G/ccc on main ?1 terraform plan -out Site-1/site-1.tfplan **-var-file=Site-1/site-1.tfvars**
**var.building_name
Enter a value: **
I would expect not to be asked for the variables defined in the tfvars file, I guess something is wrong with how variables are set/formatted in that tfvars file, or with my loop for the building. Can you please help? :) Thanks!
You should define ./variables.tf
and ./modules/site_settings/variables.tf
as a list, look like this:
variable "buildings" {
description = "List of buildings"
type = list(object({
building_name = string
parent_site_name = string
latitude = string
longitude = string
}))
}
Because of this :
parameters {
site {
building {
name = "${each.value.building_name}"
parent_name = "${var.global_name}/${var.region_name}/${var.country_name}/${var.city_name}/${each.value.parent_site_name}"
latitude = "${each.value.latitude}"
longitude = "${each.value.longitude}"
}
}
type = "building"
}
And you provided a list:
buildings = [
{
building_name = "B01"
parent_site_name = "Site-1"
latitude = "xx.xxxx"
longitude = "xx.xxxx"
},
{
building_name = "B02"
parent_site_name = "Site-1"
latitude = "xx.xxxx"
longitude = "xx.xxxx"
},
]