how to use conditional statements

24 views
Skip to first unread message

Satish Katuru

unread,
Jul 29, 2014, 8:58:32 AM7/29/14
to puppet...@googlegroups.com
Hi

Here is my flow:

Stop service-->copy required files from Master server-->Start service

Once this is done,I am taking a copy of files on Master Server and removing the files from original location.

I wanted to add this condition (if files are not there at that location no need stop and restart the service )


The reason behind this question is for every 30 minutes deployment will be done on agent machine automatically.I just wanted to make sure that if files are there then only I have to stop and start the service.


Thanks,
Satish.

Jason Antman

unread,
Jul 30, 2014, 8:34:40 AM7/30/14
to puppet...@googlegroups.com
Puppet is idempotent. It doesn't work that way. Puppet declares intended state. So you tell puppet "service should be running and files should be there" and Puppet makes it so. If you have a "thing" that should be implemented/executed in a specific way with specific ordering, that's what custom providers (and types for them) are for.

Maybe someone else will chime in with a better solution than "write a custom provider to do something awful", but every time I've run up against this I've just decided that I either needed a provider for it, or the application was broken and needed to be fixed.

-Jason


--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/e1ef43c1-fc2a-4893-9584-730d044709f3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jcbollinger

unread,
Jul 30, 2014, 9:27:35 AM7/30/14
to puppet...@googlegroups.com


On Tuesday, July 29, 2014 7:58:32 AM UTC-5, Satish Katuru wrote:
Hi

Here is my flow:

Stop service-->copy required files from Master server-->Start service

Once this is done,I am taking a copy of files on Master Server and removing the files from original location.


Why?  It would be easier and better to leave the master copy where it is on the master.

 

I wanted to add this condition (if files are not there at that location no need stop and restart the service )


The reason behind this question is for every 30 minutes deployment will be done on agent machine automatically.I just wanted to make sure that if files are there then only I have to stop and start the service.



To begin, if you (may) need to stop and start the service at different points in the run, then you will need to use an Exec for one of those points.  The stop would be a better one to do that way.  The result would be something like this:

exec { 'Stop myservice to deploy new files':
  command => 'service myservice stop',
  path => '/usr/bin:/bin:/usr/sbin:/sbin'
  # ... see below ...
}
->
file { '/var/myservice/deployed_file':
  source => 'puppet:///modules/mymodule/deployed_file'
}
->
service { 'myservice':
  ensure => 'running'
}

Having set up something along those lines, your question boils down to how to avoid executing the Exec's command if file deployment is not going to happen.  Well, the Exec resource type has parameters 'onlyif' and 'unless' that it can use to determine whether the command needs to be run.  The case you describe is a simple one: you can simply test for the existence of your deployed file:

...
  unless => 'test -e /var/myservice/deployed_file'
...

Note that this general strategy doesn't work well if you remove the deployment file from the master.  If you do, then the File resource will fail on every run, and the Service will not be ensured running (though it may be running anyway).


John

Reply all
Reply to author
Forward
0 new messages