Granolaboy
Granolaboy

Reputation: 333

Puppet strange behaviour of execs

I use a function to install several libraries to my VM right now. However for some reason it always executes the chown exec even when the create exec is not used (because the library is already installed).

Is my understanding of subscribe wrong? I thought the second exec will only be executed when the one subscribed to is.

define add (
  $lib_version = undef,
  $lib_version_prefix = undef,
  $lib_name = undef,
  $user_path = "/usr/bin:/usr/sbin:/bin",
){
  file {"/var/www/lib/$lib_name":
    mode    => $php::folder_mode,
    owner   => $php::folder_owner,
    group   => $php::folder_group,
    ensure  => directory
  }
  exec {"create-$lib_name":
    cwd     => "/var/www/lib/$lib_name",
    command => "xxx",
    creates => "/var/www/lib/$lib_name/$lib_version",
  }
  exec {"chown-$lib_name":
    cwd => "/var/www/lib",
    command => "chown xxx",
    path => $user_path,
    subscribe => Exec["create-$lib_name"],
  }
}

Thanks in advance

Upvotes: 0

Views: 69

Answers (3)

Tombart
Tombart

Reputation: 32388

Puppet does not execute statements in linear order (as is the code written) unless you define some constraints.

You need to enforce your ordering, this could be done with chaining arrows ~> or require statements:

  exec {"chown-$lib_name":
    cwd       => "/var/www/lib",
    command   => "chown xxx",
    path      => $user_path,
    require   => Exec["create-$lib_name"],
  }

subscribe means that the resource will be notified when dependent one changes. It is frequently used e.g. for restarting services, but not very useful in this case.

Upvotes: 0

daxlerod
daxlerod

Reputation: 1166

Using the file type here is the better solution, but if you want an exec to only run when subscribed or notifying resources are changed, set refreshonly => true on your exec.

Upvotes: 0

ptierno
ptierno

Reputation: 10074

No need to use an exec for the chown operation. Use the file resource instead.

 exec { "create-${lib_name}":
   cwd     => "/var/www/lib/${lib_name}",
   command => 'xxx',
   creates => "/var/www/lib/${lib_name}/${lib_version}"
 }

 file { "/var/www/lib/path/to/directory":
   ensure    => directory,
   owner     => 'root',
   group     => 'root',
   recurse   => true,
   subscribe => Exec["create-${lib_name}"
 }

Upvotes: 1

Related Questions