Search code examples
puppetpuppet-enterprise

fail when a file exist in puppet


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


Solution

  • 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.