Execute block running continously even after chef run completion


#1

Hey all! I am pretty new to Chef. Am trying to automate installation of DC/OS cluster using Chef. Am running a chef recipe on a Bootstrap node. One of the Execute blocks of the recipe will Make bootstrap node SSH into a remote host and install some packages. It executes successfully and Chef run succeeds. But I can see that that set of commands in execute block is continuously being executed on the remote host. I verified that by “history” command on remote host.

node['dcos']['master_list'].each do |mast|
  execute 'install_docker_master' do
	user 'root'
	cwd '/root'
	returns [0, 1, 255]
	command "ssh -o StrictHostKeyChecking=no #{node['dcos']['ssh_user']}@#{mast} -t -t -i /root/genconf/ssh_key << 'endssh'
	sudo yum -y update
	sudo curl -fsSL https://get.docker.com/ | sh
	sudo systemctl daemon-reload
	sudo systemctl enable docker
	sudo service docker start
	sudo mkdir /etc/systemd/system/docker.service.d
	sudo cp docker.conf /etc/systemd/system/docker.service.d/docker.conf
	sudo systemctl daemon-reload
	sudo systemctl restart docker
	sudo systemctl stop firewalld && sudo systemctl disable firewalld
	sudo yum install -y tar xz unzip curl ipset
	sudo sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config && sudo groupadd nogroup
	exit
	endssh
	
	"
  end
end

#2

The execute resource by itself will always run as it has no way to know if it has to run or now. You have to more or less manually tell it when to run using an not_if or only_if guard clause: http://devdocs.io/chef~12/12-9/resource_common

But actually seeing you execute I would really advise you to have a look at test-kitchen to play around with chef and the docker-cookbook: https://supermarket.chef.io/cookbooks/docker .


#4

Yeah as you said I am using the docker cookbook to install Docker on bootstrap node… The thing is, only Bootstrap node will run the Chef client and other remote hosts does not have Chef installed on them to run cookbooks. Bootstrap node will SSH into remote hosts and install packages.


#5

Ah, ok, sorry. Now I get it. But you will still need a guard clause. Something like this:

node['dcos']['master_list'].each do |mast|
  execute 'install_docker_master' do
    user 'root'
    cwd '/root'
    returns [0, 1, 255]
    command "ssh -o StrictHostKeyChecking=no #{node['dcos']['ssh_user']}@#{mast} -t -t -i /root/genconf/ssh_key &lt;&lt; 'endssh'
    sudo yum -y update
    sudo curl -fsSL https://get.docker.com/ | sh
    sudo systemctl daemon-reload
    sudo systemctl enable docker
    sudo service docker start
    sudo mkdir /etc/systemd/system/docker.service.d
    sudo cp docker.conf /etc/systemd/system/docker.service.d/docker.conf
    sudo systemctl daemon-reload
    sudo systemctl restart docker
    sudo systemctl stop firewalld && sudo systemctl disable firewalld
    sudo yum install -y tar xz unzip curl ipset
    sudo sed -i s/SELINUX=enforcing/SELINUX=disabled/g /etc/selinux/config && sudo groupadd nogroup
    exit
    endssh
    touch #{Chef::Config[:file_cache_path]}/#{mast}.bootstrapped
    "
  end
  not_if { File.exist?("#{Chef::Config[:file_cache_path]}/#{mast}.bootstrapped") }
end

#6

good idea! let me try that! thanks a ton!