Is there a more elegant way to remotely run chef-client?

Solution 1:

You could use knife ssh to run chef-client on all boxes that contain a certain role or recipe:

knife ssh "role:web" "sudo chef-client" -x ubuntu --sudo 

Or if you're in EC2:

knife ssh "role:web" "sudo chef-client" -x ubuntu -a ec2.public_hostname 

Solution 2:

That'd pretty much how you get things started to begin with, but it only needs to be done once. The initial run of chef-client typically enables and starts the chef-client daemon as an init.d service.

If you really wanted to do it more elegantly, you could ditch knife-ssh and run ssh directly:

ssh ubuntu@ipadddress -i mycredentials.pem sudo chef-client

that will probably be faster, as knife-ssh does a search against the Chef server to fetch nodes matching the search term (in this case name:dynode), which you don't strictly need to do if you already know the IP address.


Solution 3:

You could use ansible to deploy and run chef-client.

$ ansible -i hosts all -a 'chef-client'

ansible is easily installed with pip:

pip install ansible

Your inventory file (in the example, named "hosts") might look like this:

[all] host1.example.com ansible_user=root host2.example.com ansible_user=root host3.example.com ansibel_user=root

(notice "all" is the name of the grouping in the configuration file for our example - this is arbitrary and can be anything. Your inventory file can also include other groupings as well, eg [web_wervers], [database_servers], [chef_servers], etc.)

So,again, putting it all together:

> ansible -i hosts all -a 'chef-client'

or maybe:

> ansible -i hosts all -a 'systemctl status'

Tags:

Chef