1

I am trying to use Puppet to modify file content, but if file doesn't exist, I will skip and do nothing. However, i found the dependency checking just work in the first iteration, but after that, it seems not function. Here is my puppet manifests:

class tibco::hawk_gc_tuning {

  $domain_array = split($domain_list, '\n')
  $hawkgc = hiera('hawk_gc_arg','xxxxxx')

  $domain_array.each |$tibcodomain| {

  notify { "Now in : ${tibcodomain} ": }

    exec {"check_presence_${tibcodomain}":
      path        => "/usr/bin:/usr/sbin:/bin",
      command     => 'true',
      onlyif      => "test -e /home/tibco/tra/domain/${tibcodomain}/hawkagent_${tibcodomain}.tra"
    }

    file_line { "change $tibcodomain hawk agent gc arg comment":
      require     => Exec["check_presence_${tibcodomain}"],
      path        => "/home/tibco/tra/domain/${tibcodomain}/hawkagent_${tibcodomain}.tra",
      line        => '# tuning for hawkagent',
      match       => '^# tuning for hawkagent',
    }->
    file_line { "change $tibcodomain hawk agent gc arg":
      path        => "/home/tibco/tra/domain/${tibcodomain}/hawkagent_${tibcodomain}.tra",
      line        => "${hawkgc}",
      match       => '^java.extended.properties=-XX\\\\:MaxPermSize\\\\=.*',
    }
  }
}

Below is the output from my code:

Notice: Now in : A Notice: /Stage[main]/Tibco::Hawk_gc_tuning/Notify[Now in : A ]/message: defined 'message' as 'Now in : A ' Notice: /Stage[main]/Tibco::Hawk_gc_tuning/Exec[check_presence_A]/returns: executed successfully Notice: Now in : B Notice: /Stage[main]/Tibco::Hawk_gc_tuning/Notify[Now in : B ]/message: defined 'message' as 'Now in : B ' Error: /Stage[main]/Tibco::Hawk_gc_tuning/File_line[change B hawk agent gc arg comment]: Could not evaluate: No such file or directory - /home/tibco/tra/domain/B/hawkagent_B.tra Notice: /Stage[main]/Tibco::Hawk_gc_tuning/File_line[change B hawk agent gc arg]: Dependency File_line[change B hawk agent gc arg comment] has failures: true Warning: /Stage[main]/Tibco::Hawk_gc_tuning/File_line[change B hawk agent gc arg]: Skipping because of failed dependencies Notice: Now in : C Notice: /Stage[main]/Tibco::Hawk_gc_tuning/Notify[Now in : C ]/message: defined 'message' as 'Now in : C ' Error: /Stage[main]/Tibco::Hawk_gc_tuning/File_line[change C hawk agent gc arg comment]: Could not evaluate: No such file or directory - /home/tibco/tra/domain/C/hawkagent_C.tra Notice: /Stage[main]/Tibco::Hawk_gc_tuning/File_line[change C hawk agent gc arg]: Dependency File_line[change C hawk agent gc arg comment] has failures: true Warning: /Stage[main]/Tibco::Hawk_gc_tuning/File_line[change C hawk agent gc arg]: Skipping because of failed dependencies Notice: Finished catalog run in 1.06 seconds

As you can see above, both A, B, C are not exists, but only the first one, A, gives correct result and skip the file_line resources. Could someone give me some hints on the problem?

lung
  • 11
  • 1

1 Answers1

1

Your design is based on a common misconception.

Puppet is not a scripting language. Do not try to model queries in the catalog by abusing the exec resource type. The Puppet manifest needs to model one coherent target state, based on the information that is present during compile time. If your target state depends on the presence of a certain file on the agent system, you need to pass this information to the master using a custom fact.

In particular, your exec test cannot help you. Puppet will try to synchronize it by calling the true command when the file is present, yes. This will succeed. If the file is not present, however, Puppet assumes that the exec needs no synchronization at all. Dependet resources can just commence in this case.

Felix Frank
  • 8,125
  • 1
  • 23
  • 30