This is a Terraform example of remote-exec provisioner with ssh connection using null_resource. To use remote-exec you need to provide remote server connection information. In my below example I am using and creating connection with ssh protocol to linux servers with terraform configuration. If you are using windows system to connect then use winrm type protocol.
In the inline, mention the commands you want to execute on remote system.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
resource "null_resource" "linux" { triggers = { #value = SomeResource_ID always_run = "${timestamp()}" } connection { type = "ssh" host = "192.168.34.83" user = "ubuntu" password = "ubuntu" #https = false #insecure = true #timeout = "20s" #"1m" #target_platform = "windows" } provisioner "remote-exec" { inline = [ "hostname" #, ] } #depends_on = [SomeResource_ID] } |
To use and deploy this configuration, run terraform Init and check plan. They all looks good.
Download this null_resource ssh remote_exec example here or it is also available on github.com.
In the apply phase, I am able to connect to remote server Linux system over ssh and execute the commands provided in inline set.
This is the complete output of the terraform apply configuration.
ubuntu@ansiblegalaxy:~/Documents/terraform/ssh$ terraform init Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/null... - Installing hashicorp/null v3.2.1... - Installed hashicorp/null v3.2.1 (signed by HashiCorp) Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. ubuntu@ansiblegalaxy:~/Documents/terraform/ssh$ terraform plan Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # null_resource.linux will be created + resource "null_resource" "linux" { + id = (known after apply) + triggers = { + "always_run" = (known after apply) } } Plan: 1 to add, 0 to change, 0 to destroy. ──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now. ubuntu@ansiblegalaxy:~/Documents/terraform/ssh$ terraform apply --auto-approve Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # null_resource.linux will be created + resource "null_resource" "linux" { + id = (known after apply) + triggers = { + "always_run" = (known after apply) } } Plan: 1 to add, 0 to change, 0 to destroy. null_resource.linux: Creating... null_resource.linux: Provisioning with 'remote-exec'... null_resource.linux (remote-exec): Connecting to remote host via SSH... null_resource.linux (remote-exec): Host: 192.168.34.83 null_resource.linux (remote-exec): User: ubuntu null_resource.linux (remote-exec): Password: true null_resource.linux (remote-exec): Private key: false null_resource.linux (remote-exec): Certificate: false null_resource.linux (remote-exec): SSH Agent: false null_resource.linux (remote-exec): Checking Host Key: false null_resource.linux (remote-exec): Target Platform: unix null_resource.linux (remote-exec): Connected! null_resource.linux (remote-exec): prod-os-lin1 null_resource.linux: Creation complete after 4s [id=126966801481480593] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. ubuntu@ansiblegalaxy:~/Documents/terraform/ssh$
Useful Articles
Terraform fore_each for loop filter with if condition example
Terraform testing local variables and output csv file without resource Part 1
Terraform testing variable map object values without resource configuration part 2
Terraform foreach module output to show only required results
Hashicorp Terraform map and object inside module and variable example
Terraform one module deploy null or multiple resources based on input
Terraform A reference to a resource type must be followed by at least one attribute access, specifying the resource name
Create an Azure virtual machine scale set and load balancer using Terraform
Azure Terraform fixed Availibility Zones on Virtual Machine Scale Set
Writing and Using Terraform modules
Terraform Using one module variable in another module
Hashicorp Terraform dynamic block with example
Terraform for_each loop on map example
Terraform for_each loop on resource example
Terraform manage similar resources with for_each loop inside modules
Importing existing resources into Terraform - Step by Step
Importing already created module Infrastructure