Search code examples
ipchef-infraknife

chef new web node recipe


I'm a chef newbie and working on a "new web node" recipe that creates a server from scratch and does many things to it until it reaches a required state. I got this part right and so far so good, however I need to do another task as well and here's were I get confused in using chef to do it. Whenever I create the new web node, I also need to edit a php .conf file on another server by adding the new web node's ip address to a variable string on that php conf file.

This is how I currently do things:

 # knife rackspace server create --server-name chef-node1 --node-name chef-node1 --flavor performance1-2 --image 042395fc-728c-4763-86f9-9b0cacb00701

Once server is created I add the recipe to it and run chef client on the new node

# knife node run_list add chef-node1 recipe[new-web-node::default]
# knife ssh -a ipaddress 'name:chef-node1' 'chef-client'

My question is how would I go about editing that php .conf file on the other server as well without creating a new recipe that I have to manually run on that other server? How can it be done all in one go?


Solution

  • Use search to search for "the other" node based on a certain role, recipe or attribute. Then put the IP address of that other node in the template creating your php config file.

    Imagine that the your web node has the role web, then this code on your "other" node would give you the IP address of your first web node:

    web = search(:node, "role:web")
    ip = web.first[:ipaddress]
    

    This can be then parsed to a template.