provisioner "remote-exec" { inline = [ ". /root/.oldroot/nfs/bash_aliases\ninstallimage -a -c /root/installimage.conf -x /tmp/post-install.sh" ] } instead of \n, what can I do to have multilines?

Terraform remote-exec provisioner multiline commands alternatives

Focused the query on Terraform and the specific context of using multiline commands in the remote-exec provisioner, which will yield more relevant results for the user's question.

To execute multi-line commands with Terraform's remote-exec provisioner instead of using the \n newline character, you can utilize a heredoc syntax. This method not only improves the readability of your code but also neatly packages multiple commands in a single block. Here's how you can do it:

Using Heredoc Syntax

Heredoc allows you to define multi-line strings directly in your Terraform configuration. You can rewrite your existing remote-exec provisioner block as follows:

provisioner "remote-exec" {
    inline = <<-EOF
        . /root/.oldroot/nfs/bash_aliases
        installimage -a -c /root/installimage.conf -x /tmp/post-install.sh
    EOF
}

Breakdown of the Syntax

  • <<-EOF: This indicates the start of a heredoc, where EOF is a custom delimiter that you can choose; it can be any string. Using <<- allows for the indentation of the content.
  • Multi-line Commands: Place each desired command on its own line, which improves readability and organization.
  • Ending the Heredoc: The heredoc ends with EOF on a new line and no leading spaces.

Advantages of Using Heredoc

  1. Readability: Utilizing heredoc makes it easier to read and maintain the script, particularly when you have multiple commands.
  2. Avoiding Syntax Errors: Heredoc minimizes the chance of syntax errors that can occur when manually inserting newline characters with \n.
  3. Organized Structure: This approach helps to keep your Terraform configurations tidy and well-structured, enhancing overall clarity.

Conclusion

By adopting the heredoc syntax for your Terraform remote-exec provisioner, you can effectively streamline your command execution process. This method allows for cleaner code that is easier to manage, particularly as your infrastructure provision scripts grow more complex.

For further details regarding provisioning in Terraform, you can refer to the official Terraform Documentation.

Sources

10
1
Terraform Remote-Exec: A Concise Guide
Scalr

Master Terraform's remote-exec provisioner: clear setup steps, SSH tips, examples, and best-practice alternatives for post-provision ...

2
Provisioner: remote-exec | Terraform
Developer

The remote-exec provisioner requires a connection and supports both ssh and winrm . Important: Use provisioners as a last resort. There are better alternatives ...

3
Running multiple commands in local-exec : r/Terraform
Reddit

I have a terraform that is local exec to execute a command locally. I want to add a second command, which will immediately follow the command before it.

4
How To Handle Multiple Lines within a Command Block in ...
Stack Overflow

There is a way to do it with heredoc that allows you to run multiple commands in one command using the local-exec provisioner.

5
Terraform Provisioners - Why You Should Avoid Them
Spacelift

The remote-exec provisioners are similar to local-exec provisioners – where the commands are executed on the target EC2 instance instead of Terraform host.

6
remote-exec: "inline" with multiple commands succeeds ...
GitHub

Search for not_a_valid_command in the output. Despite this, provisioning succeeds, and subsequent command still runs.

7
How to run a long bash command in multiline using heredoc
Discuss

I am trying to use the local-exec provisioner and I have a long command that I would like to make multiline for readability sakes but I am having trouble doing ...

8
Should You Use Terraform Provisioners? Best Practices ...
Scalr

The remote-exec provisioner executes commands or scripts on a remote resource after it's created, requiring a connection block to define how to ...

9
How do I use the remote-exec provisioner with Terraform?
Devops

The typical way to use remote-exec with an EC2 instance is to pass one of the instance's own IP addresses as the hostname.

10
Provisioner using multi-line powershell command with ...
GitHub

I'm provisioning a PowerShell script named file.ps1 with a null_resource and using the remote-exec provisioner in order to run the script with the parameters.