How to get 'value only' for node IP address or FQDN?

If I enter the following I can get the node’s IP address or FQDN:

knife node show server1 -a fqdn

Which returns:


OR, for IP Address:

knife node show server1 -a ipaddress

Which returns:

server1: ipaddress:

What I want to do is just return the value only:

…so I can store in a variable and pass in later.

How can I return just the value of the fqdn or ipaddress?

BTW: I need to do this outside of Chef, like in a PowerShell script, if possible.


knife node show server1 -a ipaddress | grep ipaddress | awk -F: ‘{print $2}’ ? :slight_smile:

Hehe…was on the same wavelength. Did something similar in PowerShell but not quite as elegant:

[string](knife node show server1 -a ipaddress | Select-String 'ipaddress:' -simplematch) | % {$_.Trim(" ipaddress: ")}

Was actually hoping I could do something like:

(knife node show server1 -a ipaddress).value

But oh well! :slight_smile:

May I suggest to remove grep since AWK doesn’t need it?
knife node show server1 -a ipaddress | awk '$1=="ipaddress:" {print $2}'

Or you can skip all of that:

knife exec -E 'puts nodes.load("server1")["ipaddress"]'

Scripting is your friend.

1 Like

Many thanks, coderanger!
Where can I find out what sort of things I could script. For example, how can I know that there is such a thing like nodes.load?

I think I get it.
knife exec -E ‘puts nodes.methods’ does give me a lot of things I can do.