In my Packer script I use an echo -e
command to allow me to append newlines to a file, but I noticed that Packer is not handling this as I would expect.
Here are the relevant parts of my Packer source file:
provisioner "shell" {
environment_vars = [
"DEBIAN_FRONTEND=noninteractive",
]
inline = [
"echo -e \"new\nline\" | tee -a /home/ubuntu/new_file.txt",
]
}
Normally if I were to execute echo -e "new\nline"
in a shell it would output
$ echo -e "new\nline"
new
line
but during the build process Packer instead prints out
-e
new
line
How can I make Packer properly execute the 'echo -e' command?
After a lot of Google-fu I was able to find that someone else had raised this issue on the Packer GitHub which lead me to the relevant parts of the documentation.
The shell provisioner documentation actually explains why this is happening:
inline_shebang (string) - The shebang value to use when running commands specified by inline. By default, this is
/bin/sh -e
I tried launching /bin/sh
in a terminal to make sure that echo -e
provides the expected output and I found the reason for my problem:
sh-3.2$ echo -e "new\nline"
-e new
line
Remove the -e
flag from the echo command since /bin/sh
does not need it to interpret newlines
provisioner "shell" {
environment_vars = [
"DEBIAN_FRONTEND=noninteractive",
]
inline = [
"echo \"new\nline\" | tee -a /home/ubuntu/new_file.txt",
]
}
Use the inline_shebang
parameter to tell Packer to use /bin/bash
which will allow the echo -e
command to execute as expected
provisioner "shell" {
environment_vars = [
"DEBIAN_FRONTEND=noninteractive",
]
inline_shebang = "/bin/bash -e"
inline = [
"echo -e \"new\nline\" | tee -a /home/ubuntu/new_file.txt",
]
}