Sample Header Ad - 728x90

Stopping parameter expansion in write_file content of cloud-init userdata

0 votes
0 answers
184 views
## Background I'm using Terraform and cloud-init to provision an Ubuntu VM. The Terraform template contains an embedded cloud-init user_data section that contains a write_file directive to write a bash script. The hierarchy looks like this: - Terraform template - cloud-init user_data - write_content - bash script Inside the bash script, I have the following function
mkcd() {
        mkdir -p "${1}"
        cd "${1}"
        }
## Problem and question When the file is written inside the VM, it looks like this:
mkcd() {
        mkdir -p "1"
        cd "1"
        }
The ${1} is replaced with just a 1. The desired behavior is to have the function appear in the written file exactly as it appears in the Terraform template. **How do I get the function to appear in the written file exactly like this?:**
mkcd() {
        mkdir -p "${1}"
        cd "${1}"
        }
## Troubleshooting I have also tried this:
mkcd() {
        mkdir -p "$\{1}"
        cd "$\{1}"
        }
The file is written exactly as shown, with the backslashes retained.
Asked by McKenzieCarr (1 rep)
Mar 12, 2023, 07:48 PM