1

I am trying to write a puppet script which will install a module by un-tar. I want puppet to fail if it is already un tar. I tried to do below code but it always fails even if directory is absent.

I am checking if /opt/sk is present then fail otherwise proceed on installation.

define splunk::fail($target)
{
    $no = 'true'
    case $no {
         default : { notice($no) }#fail('sk is already installed.')}
    }
}

define splunk::forwarder( $filename , $target )
{
    file{"$target/sk":
        ensure => present
    }

    splunk::fail{"NO":
        target => '/opt/',
        require => File[$target],
    }

    file{"$target/A.tgz":
        source => $filename ,
        replace => false ,
    }

    exec{"NO1":
        command => "tar xzvf A.tgz" ,
        cwd => $target ,
        require => File["$target/A.tgz"] ,
    }

    exec{"Clean":
        command => "rm -rf A.tgz" ,
        cwd => target ,
        require => Exec["NO1"],
    }
}

splunk::forwarder {"non":
    filename => 'puppet:///modules/splunk/files/NO.tgz' ,
    target => '/opt/',
}

Thanks

anonymous
  • 1,920
  • 2
  • 20
  • 30

2 Answers2

2

Define custom_fact and use it combined with fail resource.

In your ruby directory e.g /usr/lib/ruby/vendor_ruby/facter define file tmp_exist.rb with content:

# tmp_exist.rb

Facter.add('tmp_exist') do
  setcode do
     File.exist? '/root/tmp'
  end
end

Next use it in puppet manifest. E.g I combined it with str2bool function from stdlib:

class test {

  if !str2bool($::tmp_exist) {
    fail('TMP NOT EXIST')
  }

  if !str2bool($::foo_exist) {
    fail('FOO NOT EXIST')
  }
}

include test

In /root create only tmp file.

In result you will have:

Error: FOO NOT EXIST at /etc/puppet/deploy/tests/test.pp:8 on node dbmaster

UPDATED: I updated my answer. Chris Pitman was right, my previous solution works only on puppet master or with puppet apply. I have also found an article describing how to define custom function file_exists in puppet. That also might be helpful.

kkamil
  • 2,593
  • 11
  • 21
0

You should use "creates" attribute of exec, for example:

exec { 'install':
  command => "tar zxf ${package}",
  cwd     => $some_location,
  path    => $path,
  creates => "${some_location}/my_package",
}

Puppet will only execute 'install' if "${some_location}/my_package" doesn't exist.

Mateusz M.
  • 382
  • 7
  • 17