Karthi1234
Karthi1234

Reputation: 1017

puppet need to call file resource from exec resource only

Is there anyway to call puppet file resource from exec resource only when unless condition met? Means by default file resource shouldn't executed and it can be triggered thru exec resource only.

Updated details:

Here is my manifest file

file { '/tmp/test_script.sh':
        path => '/tmp/test_script.sh',
        mode => 755,
        owner => 'root',
        group => 'root',
        ensure => file,
        source => "puppet:///modules/custom_files//tmp/test_script.sh",
}

exec {'run_script':
        unless => '/bin/rpm -qa | grep package-name',
        require => File['test_script.sh'],
        command => '/tmp/test_script.sh',
}

Here I want file { '/tmp/test_script.sh': resource has to executed only when condition unless => '/bin/rpm -qa | grep package-name', on exec resource doesn't meet. Otherwise this file resource shouldn't be executed.

Upvotes: 1

Views: 1947

Answers (1)

John Bollinger
John Bollinger

Reputation: 180201

What you present has no chance of working anything like how you intend. Because the Exec requires the File to (conditionally) be applied first, its own unless parameter would not be evaluated in time to affect that, even if there were a way it could do.

Generally speaking, details of machines' current state on which Puppet is to base decisions about the contents of that machine's catalog must be communicated to the catalog compiler via facts. Puppet / Facter has no built-in fact that conveys the information you need, but it is pretty easy to add an external fact or a custom fact that the agent will evaluate for you.

Details vary slightly according to the software versions involved, but if you are using even remotely recent Puppet and Facter then my recommendation would be an external fact. For example, you might drop this script in the appropriate module's facts.d/ directory:

#!/bin/bash

echo mypackage=$(/bin/rpm -q mypackage || echo none)

In your manifest, you could then do this:

if $mypackage == 'none' {
  file { '/tmp/test_script.sh':
    path   => '/tmp/test_script.sh',
    mode   => 755,
    owner  => 'root',
    group  => 'root',
    ensure => file,
    source => "puppet:///modules/custom_files//tmp/test_script.sh",
  }

  exec {'run_script':
    command => '/tmp/test_script.sh',
    require => File['test_script.sh'],
  }
}

Note that facts are evaluated before any resources are applied, so if you have other manifests that might manage the RPM in question, then you'll want to coordinate with that.

Upvotes: 2

Related Questions