About | FAQ | Help
Ask Your Question
0

Could you please some one let me know, how to check a particular path(directory) exists or not. Based on condition i need to assign value for a variable

asked 2018-05-04 04:24:48 -0500

Iswarya_C gravatar image

Example like . if (' folder exists'){ $var1 ='Exists' } else { $var2 ='Not exists' }

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2018-05-04 12:18:30 -0500

binford2k gravatar image

updated 2018-05-07 15:34:43 -0500

Hi. While it is certainly possible to write a custom fact to expose this information to your manifests, that's generally not how you think about writing configuration. Instead of reacting to this state, you would instead describe the state you want.

In other words, your class to manage a particular component should manage all the parts of that component and nothing else. That way, your logic becomes much simpler. Instead of the "if folder exists, then do ..." logic described above, you simply include that class on the nodes which should have it, and not on the nodes which should not.

Thinking like this is often due to misunderstanding Puppet's dependency model. Often this type of code happens when installing a package creates a directory. People want to make sure that the directory exists before writing a config file inside. The way to accomplish this is to make dependencies on the package resource itself, such as

package { 'foo':
    ensure => present,
}
file { '/etc/foo/config.ini':
    ensure => file,
    source => 'puppet:///modules/foo/config.ini',
    require => Package['foo'],  # This ensures that '/etc/foo' exists
}
edit flag offensive delete link more

Comments

https://serverfault.com/questions/859112/exists-dir-on-puppet/859127#859127

c4f4t0r gravatar imagec4f4t0r ( 2018-05-07 03:22:02 -0500 )edit

Sorry, but that answer is not correct. A custom function will run *on the master*, not the agent. That means that this solution will manage a file on the agent if a certain directory exists on the master.

binford2k gravatar imagebinford2k ( 2018-05-07 15:26:38 -0500 )edit

I know the master only compile the catalog and the execution of the function is on agent, try for yourself.

c4f4t0r gravatar imagec4f4t0r ( 2018-05-08 03:11:28 -0500 )edit

IIRC, the function runs on the master during the catalog compliatoin process. If you are running 'masterless', i.e. puppet apply on a node, then your node is acting in the 'master' role.

DarylW gravatar imageDarylW ( 2018-05-08 11:29:53 -0500 )edit

Sure, but that means that your codebase is artificially limited to forever only working masterless--and then it's a silent error that just does weird things if you ever try running it in a standard Puppet infrastructure.

binford2k gravatar imagebinford2k ( 2018-05-08 12:33:26 -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

Stats

Asked: 2018-05-04 04:24:48 -0500

Seen: 56 times

Last updated: May 07