How to properly escape special characters in cloud-init runcmd section?

449 views Asked by At

I'm attempting to edit a YAML file in place within the 'runcmd' block. In Bash, my command is working as expected. Here it is:

yq -i '.block1 += {"path": "dir1"} ' /ws1/project.yml.

However, it's not functioning within the 'runcmd' block. When I include this command in the runcmd block, it disrupts my cloud-init completely. I am unable to access the host because the user block where I add my SSH key is not functional.

Can you please tell me how to properly escape special characters to ensure correct execution of cloud-init?

However, a simpler command such as yq -i '.block1 += "path" ' /ws1/project.yml functions properly and does not disrupt the entire cloud-config. I attempted various methods to escape double quotes, curly braces, colon, and spaces before dir1, but none were successful.

2

There are 2 answers

1
falcojr On

Runcmd allows list syntax that shouldn't require escaping:

runcmd:
  - [yq, -i, '.block1 += {"path": "dir1"} ', /ws1/project.yml]
0
billp On

Here is your command with proper escaping (though I like the previous solution if that works, since it doesn't require escaping):

runcmd:
  - 'yq -i ".block1 += {\"path\": \"dir1\"}" /ws1/project.yml'