Ask Your Question

How to run "case" portion of the code only after requirement is fullfilled.

asked 2017-08-11 07:46:21 -0600

fuzzy186 gravatar image

Hi, I need to modify code, so that "case" part of the code runs only if "exec" finds "/EFS/customerdata|/EFS/saasdata" in fstab,

How can I do that ?

exec { 'Ensure EFS is not already used':   
   command => "/bin/egrep -q '/EFS/customer_data|/EFS/saas_data' /etc/fstab",  }
$already_mounted = Exec['Ensure EFS is not already used']
if $already_mounted =~ /(\/EFS\/customer_data|\/EFS\/saas_data)/ {

case $site_type {

'gluster':  {
              gluster::mount { '/ss_data':
              gluster::mount { '/cust_data':
              $cdir_require = Gluster::Mount['/cust_data']
              $sdir_require = Gluster::Mount['/ss_data']

'nfs-aws':  {
              $mstr_dirs = [ '/cust_data', '/ss_data' ]
              file { $mstr_dirs:
              mount { '/cust_data':
              mount { '/ss_data':
              $cdir_require = Mount['/cust_data']
              $sdir_require = Mount['/ss_data']

default:    {
              fail("Unknown site_mount_type. Received ${site_type}")
            }  }


edit retag flag offensive close merge delete

3 Answers

Sort by ยป oldest newest most voted

answered 2017-08-11 11:06:24 -0600

DarylW gravatar image

updated 2017-08-11 12:26:43 -0600

You can't conditionally pass information to the puppet catalog based on things run on the agent during the run.

The basics behind a puppet run are as follows:

  1. Client connects to master

  2. Master sends 'plugins' to client (custom facter facts, custom resources and libs, etc)

  3. client does a facter run (using the synced facts) and sends the results to the master

  4. Master uses the information from the facts, the puppet modules in the environment, (exported resources from puppetdb), etc to compile the catalog of resources which tells the client what state it should be in

  5. Master sends the catalog to the client

  6. Client gathers 'resources' on the system, based on the catalog, so it can compare what 'should be'(the catalog) with what 'is' (the results on the system. you can see an example of resourcegathering by running puppet resource service to see all the services puppet knows about on the system)

  7. The client then attempts to make all of the resources as they 'should be', defined in the catalog.

  8. Client sends the run report back to the master

  9. Master stores the report for the run (locally or in puppetdb)

The only way to get information from the system into the puppet run is to either run custom facts to feed that information into the catalog compilation process, or to create custom resources which take state on the system into consideration.

edit flag offensive delete link more



Thank you. I will remember about that in the future.

fuzzy186 gravatar imagefuzzy186 ( 2017-08-13 05:17:29 -0600 )edit

answered 2017-08-12 19:00:03 -0600

bess gravatar image

You cannot wrap an exec resource around if or case conditionals. What you can do is use the onlyif, unless, or creates parameter to determine if the exec should run.

I prefer to use the refreshonly parameter in my exec calls to better ensure they are executed when I desire them to but onlyif, unless, and creates work as well.

Like others have posted, for complex exec resources, it's best to write your own custom resource type:

These will be evaluated agent side like an exec call and it's a better implementation over exec calls.

edit flag offensive delete link more

answered 2017-08-11 11:49:16 -0600

Emerson Prado gravatar image

You can write a custom fact which checks this condition. Custom facts are Ruby code executed in the nodes. So your case block can be enclosed in an if block which only checks the new fact.

edit flag offensive delete link more

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


Asked: 2017-08-11 07:46:21 -0600

Seen: 66 times

Last updated: Aug 12 '17