Search code examples
stringsshchef-infrachef-recipestring-interpolation

Passing attributes to chef via command line


This is driving me nuts, any help is massively appreciated.

Currently using a recipe to run an ssh command whereby the command takes in args and then uses that.

The escaping of the string string quotes is quite literally sending me insane; please help me SO, you're my only help. :D

This is the literal string that I need for my ssh:

ssh -i /home/ec2-user/.ssh/Test-Key.pem -o StrictHostKeyChecking=no  ec2-user@ipAddress echo '{\"attr\":\"value\"}' | sudo chef-client -o solr-restart -j /dev/stdin

it's wrapped in a command within the recipe like so:

command "ssh -i /home/ec2-user/.ssh/Test-Key.pem -o StrictHostKeyChecking=no  ec2-user@ipAddress echo '{\"attr\":\"value\"}' | sudo chef-client -o solr-restart -j /dev/stdin"

no matter how I try and manipulate the string I cannot get the output to be correct, it either removes the escaped characters in the json, or adds in additional ones.

I've tried to echo '#{madness}'

where madness = madness = '{\"portAttribute\":\"'+"#{portNumber}"+'\"}'

but still no luck, thanks for any help.


Solution

  • IMHO your string interpolation looks fine but as you want to run the following command on remote machine:

    echo '{\"portAttribute\":\"#{portNumber}\"}' | sudo chef-client -o solr-restart -j /dev/stdin
    

    Command should tweaked a bit more and be passed in recipe as:

    command "ssh -i /home/ec2-user/.ssh/Test-Key.pem -o StrictHostKeyChecking=no  ec2-user@ipAddress 'echo \'{\\\"portAttribute\\\":\\\"#{portNumber}\\\"}\' | sudo chef-client -o solr-restart -j /dev/stdin' "