loafoe/terraform-provider-ssh

Missing folders lead to failing retries

Closed this issue · 2 comments

Hi,

thank you for your work.

Request

It would be great if a file copy would automatically create any missing directories.

the issue

e.g.

user@example.com: /opt# ls
one two three

and a main.tf like

terraform {
  required_providers {
    ssh = {
      source = "loafoe/ssh"
      version = "2.6.0"
    }
  }
}

resource "ssh_resource" "init" {
  host     = "example.com"
  user     = "user"
  agent   = true

  file {
    destination = "/opt/four/file" # the folder does not exist yet
    source      = "/my/local/file"
  }
}

Upon applying ssh_resource gets stuck.

Workaround

  • You can manually create the missing folder.
  • You can copy over a script to create the missing directories.

Proposal

Either create the required directories automatically or add an option to enable automatic folder creation.


What are your thoughts on this?

loafoe commented

hi @gsedlacz thanks for the request. There is a pre_commands argument which allows you to execute a set of commands before the file blocks are processed. To solve your issue you try something like:

resource "ssh_resource" "init" {
  host     = "example.com"
  user     = "user"
  agent   = true
  
  pre_commands = ["mkdir -p  /opt/four"]

  file {
    destination = "/opt/four/file" # the folder does not exist yet
    source      = "/my/local/file"
  }
}

Basically you use pre_commands to ensure all conditions are in place for the file blocks to succeed. Would this work for you?

Nice workaround and thank you for your effort