[Solved] Knife with a variable hostname

I’m trying to come up with a bash script to automate some knife commands. The following knife command works just fine:

knife ssh -a ipaddress ‘name:mailserver’ “run some command here” -x myaccount -i .ssh/mykey.pem -C3

However, I need to write a script that substitutes a variable for the hostname. So I woudl do something like:

read hostnameVar # read in a hostname variable from the command line

knife ssh -a ipaddress ‘name:$hostnameVar’ “run some command here” -x myaccount -i .ssh/mykey.pem -C3

So that last line should run some command against the host variable $hostnameVar. I’ve not gotten it work. Here are the variations I’ve tried:

knife ssh -a ipaddress ‘name:$hostnameVar’ “run some command here” -x myaccount -i .ssh/mykey.pem -C3
knife ssh -a ipaddress “name:$hostnameVar” “run some command here” -x myaccount -i .ssh/mykey.pem -C3
knife ssh -a ipaddress name:$hostnameVar “run some command here” -x myaccount -i .ssh/mykey.pem -C3
knife ssh -a ipaddress name:`$hostnameVar’ “run some command here” -x myaccount -i .ssh/mykey.pem -C3

I believe I’ve tried every combination of quotes, single quotes, and back tick with no luck. Is there a way to run knife against a variable that represents a hostname?

TIA

Hmm, I’m a little confused as to why you’re using both -a and name: here…but assuming that you just want to use bash to script around supplying a node name and then running a specific command, something like this would work:

#!/bin/bash

echo -n "What is the node name?"
read node_name

(cd /path/to/chef/repo/.chef && knife ssh name:$node_name "command" -x account -i authenticator.pem)

Thanks for the response. However I’m still not getting the expected result. Here is the output I’m getting based on your response (the following is typed, not copied).

echo $z
myhostname

knife ssh name:$z “hostname” -x myaccount -i ~/.ssh/mykeys.pem

The result I get is:
FATAL: 1 node found but does not have the required attribute to establish the connection. Try setting another attribute to open the connection using --attribute

Nevermind. My command was wrong. I’m good. Thanks for your help!!