How to create dependency between two tasks within a chef cookbook?

I wrote the a Chef cookbook which configures a server in the company.

One of the tasks there is to install an “apt_package” called “pssh” and another task is intended to be run after the pssh package installation takes place.

In reality, the second task is running before the package installation and then the chef-client run fails saying that the relevant file is missing – it is missing because the pssh package installation creates it.

I’m trying to configure the second task to run only after the package installation task is finished.

here’s the relevant code:

apt_package 'pssh' do
  action :install
  subscribes :run, 'file[/usr/bin/pssh]', :before
end

file '/usr/bin/pssh' do
  owner 'root'
  group 'root'
  mode 0755
  content ::File.open("/usr/bin/parallel-ssh").read
  action :create
end

I’ve tried using both “notifies” and “subscribes” but to no avail, the second task always runs first and causes the chef-client run to fail.

How do I establish dependency between the tasks?

Answer

The issue is that the way you have things written, the file read happens at compile time. Check out https://coderanger.net/two-pass for details on to fix it, but briefly you need to use the lazy{} helper around the file read.

Attribution
Source : Link , Question Author : Itai Ganot , Answer Author : coderanger

Leave a Comment