# \[Solved\] Knife with a variable hostname

**URL:** <https://discourse.chef.io/t/solved-knife-with-a-variable-hostname/11272>\
**Category:** Chef Infra (archive)\
**Created:** [August 1, 2017, 8:48pm UTC](https://discourse.chef.io/t/solved-knife-with-a-variable-hostname/11272 "2017-08-01T20:48:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ssss](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@ssss](https://discourse.chef.io/u/ssss)\
**Post date:** [August 1, 2017, 8:48pm UTC](https://discourse.chef.io/t/solved-knife-with-a-variable-hostname/11272/1 "2017-08-01T20:48:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![nbrewer](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/nbrewer/32/1901_2.png) [@nbrewer](https://discourse.chef.io/u/nbrewer)\
**Post date:** [August 1, 2017, 9:20pm UTC](https://discourse.chef.io/t/solved-knife-with-a-variable-hostname/11272/2 "2017-08-01T21:20:09Z")

</div>

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)
```

---

<div class="post-metadata">

**Author:** ![ssss](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@ssss](https://discourse.chef.io/u/ssss)\
**Post date:** [August 1, 2017, 9:46pm UTC](https://discourse.chef.io/t/solved-knife-with-a-variable-hostname/11272/3 "2017-08-01T21:46:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ssss](https://avatars.discourse-cdn.com/v4/letter/s/9dc877/32.png) [@ssss](https://discourse.chef.io/u/ssss)\
**Post date:** [August 1, 2017, 9:52pm UTC](https://discourse.chef.io/t/solved-knife-with-a-variable-hostname/11272/4 "2017-08-01T21:52:12Z")

</div>

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