No edit summary |
|||
| (4 intermediate revisions by the same user not shown) | |||
| Line 14: | Line 14: | ||
Show possible changes before applying:<syntaxhighlight lang="terraform"> | Show possible changes before applying:<syntaxhighlight lang="terraform"> | ||
terraform plan | terraform plan | ||
terraform plan -var-file build-specific-variables.tfvars #select a variables file to use for this plan. Will overwrite any of the same variables used in the other variables files | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 21: | Line 22: | ||
Inspect state of configuration<syntaxhighlight lang="terraform"> | Inspect state of configuration<syntaxhighlight lang="terraform"> | ||
terraform show | terraform show #inspects current state file | ||
terraform show previousplanfile.ftplan #can also be used to inspect previously created plan file | |||
</syntaxhighlight>Output plan to show in a format that can be used in a text file to display correctly<syntaxhighlight lang="powershell"> | |||
terraform plan -no-color > planfile.txt | |||
</syntaxhighlight>Use tfvars file when executing plan or apply actions:<syntaxhighlight lang="powershell"> | |||
terraform plan -var-file="variables_file.tfvars" | |||
</syntaxhighlight> | </syntaxhighlight> | ||
| Line 87: | Line 93: | ||
=== Clone from template === | === Clone from template === | ||
When building templates make sure you add the "firmware" option to the "vsphere_virtual_machine" resource. This needs to be set to "efi" if the template is built with an efi firmware. By default the clone does not apply the same firmware as the template. | When building templates make sure you add the "firmware" option to the "vsphere_virtual_machine" resource. This needs to be set to "efi" if the template is built with an efi firmware. By default the clone does not apply the same firmware as the template. | ||
==Terraform For Azure== | |||
When using terraform for an azure environment you will need to setup the environment differently. | |||
First, terraform will need a service principal to access the azure tenant to carry out the changes. | |||
To create the required configuration you will need to use the azure CLI:<syntaxhighlight lang="powershell"> | |||
az login | |||
</syntaxhighlight>it will show you the subscriptions your account has access to after login. Set the subscription you want to create the service principal in, make sure to save the output so you can use the appid and password to connect later on:<syntaxhighlight lang="powershell"> | |||
az account set --subscription "<subscription-id>" | |||
</syntaxhighlight>Create the service principal:<syntaxhighlight lang="powershell"> | |||
az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/<SUBSCRIPTION_ID>" | |||
</syntaxhighlight>you can then setup your environment with the details for the service principal. Alternatively you can save the details in the variables file for use later, however this is less secure:<syntaxhighlight lang="powershell" line="1"> | |||
$Env:ARM_CLIENT_ID = "<APPID_VALUE>" | |||
$Env:ARM_CLIENT_SECRET = "<PASSWORD_VALUE>" | |||
$Env:ARM_SUBSCRIPTION_ID = "<SUBSCRIPTION_ID>" | |||
$Env:ARM_TENANT_ID = "<TENANT_VALUE>" | |||
</syntaxhighlight>once this is complete you can then build your main.tf configuration. | |||
Latest revision as of 10:44, 4 October 2024
Basic Commands
Initialise configuration: (use folder as a location to store terraform configuration files )
terraform init
Format configuration files
terraform fmt
Validate configuration
terraform validate
Show possible changes before applying:
terraform plan
terraform plan -var-file build-specific-variables.tfvars #select a variables file to use for this plan. Will overwrite any of the same variables used in the other variables files
Apply changes:
terraform apply
Inspect state of configuration
terraform show #inspects current state file
terraform show previousplanfile.ftplan #can also be used to inspect previously created plan file
Output plan to show in a format that can be used in a text file to display correctly
terraform plan -no-color > planfile.txt
Use tfvars file when executing plan or apply actions:
terraform plan -var-file="variables_file.tfvars"
Terraform Configuration files
The main config files are the main.tf and variables.tf files
main.tf
The main.tf contains the main configuration about the build you are creating. The first bit of info in the file is the terraform provider. This is a block of code describing the service that terraform will be building on (AWS, Azure, vSphere etc). It will also contain the info to login to the service.
for the vsphere provider:
provider "vsphere" {
user = "${var.vsphere_user}"
password = "${var.vsphere_password}"
vsphere_server = "${var.vsphere_server}"
allow_unverified_ssl = true
}
data block
In the main file you will place data blocks to describe the infrastructure. this will be used to get the info when building the resources.
You will see the two arguments in the data block, the first describes what type of data it is (this is an object in the terraform provider) and the second is the name which can be used to reference the data elsewhere in the config
In the compute cluster data block you can see it is looking at the earlier data block by using the data.vsphere_datacenter.datacenter description
data "vsphere_datacenter" "datacenter" {
name = "<Datacenter name>"
}
data "vsphere_compute_cluster" "cluster" {
name = "<Cluster name>"
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_datastore" "datastore" {
name = "${var.vsphere_datastore}"
datacenter_id = data.vsphere_datacenter.datacenter.id
}
data "vsphere_network" "network" {
name = "${var.vsphere_network}"
datacenter_id = data.vsphere_datacenter.datacenter.id
}
variables.tf
This file will contain the variables for the main.tf file. This way it is easier to do different builds using the same main file.
example of the variable code block for the vsphere provider variables:
variable "vsphere_user" {
description = "Username for vSphere"
default = "<username>"
}
variable "vsphere_password" {
description = "Password for vSphere"
default = "<password>"
}
variable "vsphere_server" {
description = "vSphere server address"
default = "<FQDN of vCenter server>"
}
Clone from template
When building templates make sure you add the "firmware" option to the "vsphere_virtual_machine" resource. This needs to be set to "efi" if the template is built with an efi firmware. By default the clone does not apply the same firmware as the template.
Terraform For Azure
When using terraform for an azure environment you will need to setup the environment differently.
First, terraform will need a service principal to access the azure tenant to carry out the changes.
To create the required configuration you will need to use the azure CLI:
az login
it will show you the subscriptions your account has access to after login. Set the subscription you want to create the service principal in, make sure to save the output so you can use the appid and password to connect later on:
az account set --subscription "<subscription-id>"
Create the service principal:
az ad sp create-for-rbac --role="Contributor" --scopes="/subscriptions/<SUBSCRIPTION_ID>"
you can then setup your environment with the details for the service principal. Alternatively you can save the details in the variables file for use later, however this is less secure:
$Env:ARM_CLIENT_ID = "<APPID_VALUE>"
$Env:ARM_CLIENT_SECRET = "<PASSWORD_VALUE>"
$Env:ARM_SUBSCRIPTION_ID = "<SUBSCRIPTION_ID>"
$Env:ARM_TENANT_ID = "<TENANT_VALUE>"
once this is complete you can then build your main.tf configuration.