Terraform is a cloud native, open-source infrastructure provisioning tooling similar to Ansible. IOS XE Terraform utilizes RESTCONF + YANG to configure devices using a single binary file. Terraform is declarative, meaning that it defines the desired state. It has commercial support from HashiCorp.
Terraform providers communicate with the desired resource. Typically, there are one or more resources in a .tf file.
Terraform is installed using the “apt” package management system. Running the Debian Package command, or dpkg, with the -L flag (list) shows which packages are installed on the system, including the Terraform tool.
- We'll use the JSON in a Terraform file to configure a new VLAN on a Catalyst 9300.
- Navigate to the terraform directory
cd ~/terraform
within the Linux VM - Review the terraform.tf file to apply a new VLAN to the switch using the command
cat terraform.tf
, which will look similar to the following:
terraform {
required_providers {
iosxe = {
source = "CiscoDevnNet/iosxe"
}
}
}
provider "iosxe" {
host = "https://10.1.1.5"
insecure = true
device_username = "admin"
device_password = "Cisco123"
}
resource "iosxe_rest" "vlan_example_put" {
method = "PUT"
path = "/data/Cisco-IOS-XE-native:native/vlan/vlan-list=511"
payload = jsonencode(
{
"Cisco-IOS-XE-vlan:vlan-list": {
"id": "511",
"name": "VLAN511-flag8838384747"
}
}
)
}
resource "iosxe_rest" "vlan_example_get" {
method = "GET"
path = "/data/Cisco-IOS-XE-native:native/vlan"
}
Now that the .tf file has been reviewed and is ready for use, the Terraform tool itself can be initialized and then used to apply this configuration
-
Initialize Terraform with
terraform init
-
Apply the configuration with the terraform apply command
terraform apply -auto-approve
-
Note that the terraform provider has been executed if the message appears "Apply complete! Resources: 2 added, 0 changed, 0 destoyed."
Next, let's create a new Terraform file in a new directory using the CLI2YANG feature described below.
In Cisco IOS XE Cupertino 17.7.1 and later releases, you can automatically translate IOS commands into relevant NETCONF-YANG XML or RESTCONF-JSON request messages. You can analyze the generated configuration messages and familiarize with the Xpaths used in these messages. The generated configuration in the structured format can be used to provision other devices in the network; however, this configuration cannot be modified.
Review the CLI running configuration
- Review the running configuration in the good ole fashioned CLI using
show run
Generate the JSON of the current running config using show run | format restconf-json
Next, we can review the section of the output to find access-list
Let's create a new access list on our device using Terraform. Run the following commands in the Linux VM:
- Create a new directory
mkdir acl
- Navigate into the new directory
cd acl
- Copy the terraform.tf into a text editor such as Notepad
- Open the file in VI using
vi acl.tf
- Add a new resource to the file by copying the first