Charlotte Russell
Charlotte Russell

Reputation: 1485

Chef: How to display nodes IP Address

  1. I've noticed that there are 2 commands that can be used to display nodes. However, it only display the hostname and not the IP Address. Please let me know if there's a way to display IP Addresses on each of these nodes.

  2. What are the differences between knife node list and knife client list commands? The outputs are looks alike.

knife node list

C:\chef\cookbooks>knife node list
cheftestnode1
cheftestnode2
cheftestnode3
C:\chef\cookbooks>

knife client list

C:\chef\cookbooks>knife client list
admin-validator
cheftestnode1
cheftestnode2
cheftestnode3
C:\chef\cookbooks>

I would like to see the output in the IP Address format instead of hostname like this. Is this possible?

C:\chef\cookbooks>knife node list
10.1.1.1
10.1.1.2
10.1.1.3
C:\chef\cookbooks>

or like this. Would it be possible?

C:\chef\cookbooks>knife node list
cheftestnode1 - 10.1.1.1
cheftestnode2 - 10.1.1.2
cheftestnode3 - 10.1.1.3
C:\chef\cookbooks>

Upvotes: 1

Views: 7483

Answers (4)

Zaur
Zaur

Reputation: 431

You can use search option with some filter to limit the output and parse the json output with jq:

knife search node "Environment:prod*" -F json | jq -r '.rows[]|"|(.name)|(.automatic.ipaddress)|"'

Upvotes: 0

Sergiu
Sergiu

Reputation: 3195

That could be done quiet easily using a tool which Chef provides which is ohai. So if you type ohai on your command line you would be able to see / grep for the host IP address as well as other attributes.

The answer to your second question would be as follows:

Node = Machine

Client = User that authenticates against chef-server

The Node will run your recipes The Client has the permission level to access your chef server

So it would be something like:

When your host would try to connect to your chef server it will say Hello I am Client X, could I have the run list for node Y?

Upvotes: 1

coderanger
coderanger

Reputation: 54267

The simplest option is to use knife exec:

knife exec -E 'nodes.all.each {|n| puts n["ipaddress"] }'

Upvotes: 6

JackChance
JackChance

Reputation: 520

To answer question #2 if you want to retrieve the IP of a Chef node do the following

knife node show <your node name> -a ipaddress

To see a full list of all the information you can return with the -a flag do the following

knife node show <your node name> -l

If you want to retrieve all the IPs for a set of nodes then construct a list of them and iterate over it in a loop. One example of grabbing every ip address and outputting to console:

knife node list > node_list.txt while read p; do eval "knife node show ${p} -a ipaddress"; done < node_list.txt;

Upvotes: 1

Related Questions