Chef: Delete node with knife and add it again later

13,653

Solution 1

Our use case involves creating virtual machines and chef bootstrapping them (using the hostname as the chef node name). Nodes are often deleted and created over and over with the same name. When we destroy the virtual machine we run the two commands to clean up in Chef.

knife node delete --yes NODENAME
knife client delete --yes NODENAME

Keep in mind that in our use case we are not interested in keeping any information about what the node was doing (i.e., its run list or other attributes).

If you don't want to delete the server, you can run the above two commands to clean up the node from the chef server and then run the following commands on the machine to remove chef locally. Once done you can chef bootstrap the machine again.

#depending on how you installed chef
yum -y remove chef 
OR 
rpm -e `rpm -q chef`  # rpm -q chef returns the version of chef installed

rm -rf /var/chef
rm -rf /etc/chef
rm -rf /opt/chef

Solution 2

I think, after delete the node from your chef server the credentials of the machines which you delete was gone from the server. Again if you want to add the same node again then you must delete the client.pem (/etc/chef/client.pem) file in that node which was created by the previous bootstrap.

Solution 3

The attributes are gone after deleting the node. Thus: no.

Solution 4

By this command knife node delete 'NODENAME' you delete a node from a chef organization. But remember the node information (mostly a yml file) including the various cookbooks are stored in an SCM. So, you need to delete the entry or comment out the node that you want to be removed and check-in the code. So that the next time you upload the cookbooks to the chef-server the node will not be seen.

When you want to add it back, add it to your cookbook & Check-in the code. This is for the SCM. Then upload the cookbook to the chef-server. Now - when you do a chef-client, it will fail in the hand shake.

Delete the /etc/chef/client.pem (make sure the validation.pem is already there) on the node. And re-run the chef-client

Share:
13,653
j7nn7k
Author by

j7nn7k

Hi, I'm Jannik, Entrepreneur and coder. Combining both in my current position as founder & CTO of Particulate. I studied at the University of Koblenz, Germany (MSc of Business and Computer Science). I'm interesed in technology, traveling, football and music

Updated on July 23, 2022

Comments

  • j7nn7k
    j7nn7k over 1 year

    Say do a knife node delete 'NODENAME' to delete the node from chef server while leaving the corresponding VM running like it is.

    Is it possible, if I need to make changes to that server in the future, to add the VM again as a node and run chef-client on it (or any other chef command for that matter)?

  • Mark O'Connor
    Mark O'Connor over 10 years
    Actually... If the cookbooks you're using can tolerate the loss of VM's state (normally captured by a node's attributes) then a fresh bootstrap will work just fine.