Steve
Steve

Reputation: 2720

Chef: conditionally notify a resource

I have a case where I'm trying to create a systemd_unit in Chef to create a service. Once the service has been created (assuming its not up-to-date), I want to start it, and then only if the service was just created I want to call a bash script to perform some initialization on it.

I started out with this:

systemd_unit 'mysvc.service' do
  content({
   .
   .
   .
  })
  action [ :create ]
  notifies :run, 'bash[mysvc.init]', :delayed
 end

 bash 'mysvc.init' do
   code <<-CODE
     .
     .
     .
   CODE
   action :nothing
 end

 systemd_unit 'mysvc.service' do
   action [ :enable, :start ]
 end

My expectation here is that if the :create ran on the systemd_unit, the my bash script would execute. Since the bash script is marked as :nothing, it would not run any other time.

Unfortunately, that's not quite the behavior I get. What actually happens is if either the :create or the [ :enable, :start] blocks of the systemd_unit get executed, then my bash script runs. This won't work becuase if I've already initialized the data in my service, attempting to reinitialize will fail.

How can I get my bash script to run only when the service is newly created?

Upvotes: 2

Views: 1263

Answers (1)

coderanger
coderanger

Reputation: 54211

This is not really a thing we support. The updated? flag (which powers notifications internally) is intentionally vague and means something different for each resource, but usually any time the resource takes any action it will count as updated. That compounds with the fact that it looks like we messed up on the implementation of systemd_unit and used the resource name directly instead of a name property :( I'll at least get a ticket in to fix this for the future.

Upvotes: 0

Related Questions