Reputation: 183
I am stuck in situation where I need ignore error and continue with execution of Puppet Recipe.
Is there a property like "ignore_failure" in Chef, so that I can overcome from below situation. If not please suggest work around for same.
Step 1: find for .txt file in /tmp directory and remove from /tmp directory.
Step 2: After execution of step 1 [exit code either 0 or 1], I have to create file which print with current time.
Issue :: If .txt files not present in /tmp directory, I'm getting error and step 2 unable to perform.
Piece of manifest on Puppet Master:
Exec ['delete_str_tmp'] -> File [ '/info.txt' ]
exec { 'delete_str_tmp':
path => [ '/bin/', '/sbin/' , '/usr/bin/', '/usr/sbin/' ],
command => "find /tmp/ -name '*.txt' -type f | xargs -n 1 rm",
}
file { '/info.txt' :
ensure => 'present',
content => inline_template("Created by Puppet at <%= Time.now %>\n")
}
When I ran 'puppet agent --test' on Puppet Agent, it gave the following error :
> Notice:
> /Stage[main]/Main/Node[app1-server]/Exec[delete_str_tmp]/returns: rm:
> missing operand Notice:
> /Stage[main]/Main/Node[app1-server]/Exec[delete_str_tmp]/returns: Try
> 'rm --help' for more information. Error: find /tmp/ -name '.txt' -type
> f | xargs -n 1 rm && true returned 123 instead of one of [0] Error:
> /Stage[main]/Main/Node[app1-server]/Exec[delete_str_tmp]/returns:
> change from notrun to 0 failed: find /tmp/ -name '.txt' -type f |
> xargs -n 1 rm && true returned 123 instead of one of [0] Notice:
> /Stage[main]/Main/Node[app1-server]/File[/info.txt]: **Dependency
> Exec[delete_str_tmp] has failures: true Warning:**
> /Stage[main]/Main/Node[app1-server]/File[/info.txt]: **Skipping
> because of failed dependencies**
Upvotes: 1
Views: 3877
Reputation: 180418
For your particular case, use a Tidy
resource instead of an Exec
to perform the cleanup:
tidy { 'delete_str_tmp':
path => '/tmp',
matches => '*.txt'
}
(By default, tidy does not remove directories.)
More generally, specifying a resource relationship to Puppet, as you do by means of a chain operator, expresses that the dependent resource can only be properly synced when the independent resource is in sync. Ordering follows from that, but so also does the fact that no attempt will be made to sync the dependent resource if the Puppet fails to sync the independent one.
For Exec
s in particular, it follows that you must ensure that Puppet will correctly map exit status to success or failure. You can do this either by choosing your command carefully or by using the returns
parameter. For a case such as this, I think the former is more appropriate, as it doesn't depend on you predicting all the possible exit statuses. You could do it like this:
exec { 'delete_str_tmp':
path => [ '/bin/', '/sbin/' , '/usr/bin/', '/usr/sbin/' ],
command => "find /tmp/ -name '*.txt' -type f | xargs -n 1 rm || :",
}
The :
is a standard shell built-in that does nothing and returns a success code. Adding the "|| :" to the end of the command is therefore a standard shell trick to ensure that the overall command always succeeds.
Upvotes: 2