Hem
Hem

Reputation: 729

Puppet: how many times an exec block with creates run?

I have the below two exec resources and would like the exec resource run the script to run whenever the file /var/lib/my-file is not present. I'm wondering what would happen if the file never gets created. Would the exec resource check if file exists run forever in a loop until it gets created?

exec { 'run the script':
  command     => "python my-script.py",
  path        => '/bin:/usr/bin:/usr/local/bin',
  timeout     => 900,
  subscribe   => File["my-settings.yaml"],
  refreshonly => true,
} 

exec { 'check if file exists':
  command => 'true',
  path    => '/bin:/usr/bin:/usr/local/bin',
  creates => '/var/lib/my-file',
  notify  => Exec['run the script']
}

Upvotes: 1

Views: 282

Answers (1)

Matthew Schuchard
Matthew Schuchard

Reputation: 28739

A resource is only applied once per catalog application, which occurs once per catalog compilation per node. You can verify this for yourself by trying it out.

If the Python script fails to create the file, the resource would simply be applied again during the next catalog application. Otherwise, idempotence prevails and the resource is not applied because the file already exists.

Additionally, you should simplify your resources into:

exec { 'run the script':
  command     => 'python my-script.py',
  path        => '/bin:/usr/bin:/usr/local/bin',
  timeout     => 900,
  creates     => '/var/lib/my-file',
  subscribe   => File["my-settings.yaml"],
  refreshonly => true,
}

This is functionally the same as what you have in your question and is more efficient and easier to read.

Upvotes: 2

Related Questions