Packer version: Packer v1.4.1
Target Host platform: Debian 9
My use case is to add Protocol 2
to the last line of /etc/ssh/sshd_config
. I use shell provisioner with inline command as follows:
"provisioners": [
{
"type": "shell",
"inline": [
"echo -e \"\nProtocol 2\" | sudo tee -a /etc/ssh/sshd_config"
]
},
...
]
Where echo -e
should allow backslash escapes like \n
in the echo'd string. However, this produces this in /etc/ssh/sshd_config
...
-e
Protocol 2
Why is this the case and how do I safely solve this?
Hi, thanks for reaching out.
There are two little tweaks you need to your inline call in order to make this work:
first, you need to change the inline_shebang
to use "/bin/bash" instead of "/bin/sh", because sh evaluates the echo flag incorrectly. Second, I think you need a second slash to escape that newline, or else it gets evaluated at the same time as the escaped quotes instead of at runtime.
working config:
{ "type": "shell",
"inline_shebang": "/bin/bash -e",
"inline": [
"echo -e \"\\nProtocol 2\" | sudo tee -a /etc/ssh/sshd_config",
]
}
That said, I generally think the best workaround when you're dealing with string escaping issues is to directly upload the script using the "script" feature rather than the "inline" feature. You could just write
#!/bin/bash -e
echo -e "\nProtocol 2" | sudo tee -a /etc/ssh/sshd_config
to a file myscript.sh
and call it using
"provisioners": [
{ "type": "shell",
"script": "myscript.sh"
}
]
Thank you. I should have RTFM more thoroughly.
Second, I think you need a second slash to escape that newline, or else it gets evaluated at the same time as the escaped quotes instead of at runtime.
This might be worth adding to the documentation?
Maybe; it's not really Packer-specific, just a double string escape, but I'll see if there is a place where it makes sense to add an example to our example scripts. I'd recommend anyone using special characters in their scripts use the "script" option instead of the "inline" one.
I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.