Ask Your Question
0

Can I skip a block when a file ensure fails, rather than failing the module?

asked 2016-09-27 16:41:15 -0500

Cos gravatar image

updated 2016-09-29 14:26:09 -0500

In a puppet module we have a conditional block, if $somevariable, and in that block is a File resource with ensure => present, and a resource that requires that file and generates content from a template. If we run the puppet agent when the file isn't present, the module fails and puppet fails.

What I'd like to do instead is that if the file resource fails, just don't generate that resource from the template. Skip it and move on. What's a simple straightforward way to do this?

Basically what we have now is something like this:

if $fileneme != '' {
  file { $filename:
    ensure => present,
    mode   => '0440',
  }

  module::path { 'modulename':
    ensure  => present,
    content => template('path/filetemplate.erb'),
    require => File[$filename],
  }
}

I just want to change it such that if the filename is defined (not blank), then generate a file from that template if the named file is actually present, but skip that step and harmlessly move on if that file cannot be ensured.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-09-29 09:46:57 -0500

puser gravatar image

updated 2016-09-29 14:37:54 -0500

I had a similar problem. I solved it by changing the

ensure => present

to

ensure => $is_present

Then I sent the $is_present to present or absent based on if the filename is set or not. It may be round-about but I couldn't think of another scenario without refactoring.

EDIT 2

My Apologies, yes I did read your question wrong. What you have posted here does compile and run.. not sure about best practices though.

Edit 3

You could do something like this in your init.pp file

if $clientcert =~ /your_name/ {
            include myapp::build
    }
    include myapp::config

Where $clientcert is your variable and myapp::build would be an encapsulation of any configuration you want to do that might change.

Last Edit.. Lol

create a facts.shin /modules/<your_module>/facts.d. Execute

/usr/bin/test <path_to_file>
echo "has_file=$?"

If it is 0 it exists and you can proceed, if it is 1 then skip the block. you can access hasfile by doing $hasfile in your manifest. Also if you do a

puppet facts | grep has_file

It will tell you the output prior to the run. Hope that finally answers your question.

edit flag offensive delete link more

Comments

Thanks. You misunderstood my question. I don't want to do this based on whether the variable is set to a value or not. Rather, IF the variable is set to a value, THEN I want to either complete the block if the file is present, or skip the block and move on if the file is not present.

Cos gravatar imageCos ( 2016-09-29 11:08:50 -0500 )edit

BTW, my example code block in my question actually achieves what your suggestion does, already. That is, if the variable is not set to a value, then my config block won't try to ensure the file is present (nor generate the template). So there's no need for a round-about workaround.

Cos gravatar imageCos ( 2016-09-29 11:10:12 -0500 )edit

I don't think that would do anything better than my example. It just checks the value of the variable, and either includes or doesn't include the module based on that; it doesn't seem to do anything based on whether an actual file exists or not.

Cos gravatar imageCos ( 2016-09-29 14:24:22 -0500 )edit

To be very clear, the config sample I gave does NOT achieve what I want. But it DOES work as is. I'm not saying, "how do I do what this sample does?" because for that, I'd just use the example I gave. I'm asking, how do I alter it such that it skips the block cleanly if the file doesn't exist?

Cos gravatar imageCos ( 2016-09-29 14:25:31 -0500 )edit

The only way to do that is if the file does not exist before the run occurs during the fact gathering, with a custom fact to check state. The whole issue is puppet's declarative nature, you should be describing how the system should look, not how it may look.

DarylW gravatar imageDarylW ( 2016-09-30 06:41:39 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-09-27 16:41:15 -0500

Seen: 67 times

Last updated: Sep 29 '16