How to run sudo commands in terraform?
The answer was to use the following syntax in my first sudo command:
"echo yourPW | sudo -S someCommand"
This bypasses the sudo password prompt and enters the password directly into the command. I already had my sudo password as a variable "${var.pw}"
so running my sudo commands was the simple matter of changing my first command to:
provisioner "remote-exec" {
inline = [
"echo ${var.pw} | sudo -S apt-get update",
"sudo apt-get install -y curl"
]
}
What about:
echo ${var.pw} | sudo -S -k apt-get update
-k
means to ignore cached credentials to force sudo to always ask. This is for consistent behavior.
https://superuser.com/q/67765