Patrick Cervicek
2011-Jan-28 14:12 UTC
[Puppet Users] Create a file only under certain conditions
Is there a way to create a file only under certain conditions? I would like to check if a certain device exists and the config file is still missing. If the file ist missing, create the file and update the initramdisk. (This device exists only on our notebooks, otherwise it is a desktop and nothing should be done) This is our first try: class hibernate { notice("hibernate") exec { "echo": alias => "echo", # onlyif => "test -b /dev/mapper/swap_crypt -a ! -f /etc/initramfs-tools/conf.d/resume" , # onlyif => "true" , onlyif => "false" , } # should not be created as Exec["echo"] is "false"! # ... but "resume" is still created file { "resume": name => "/etc/initramfs-tools/conf.d/resume", source => "puppet:///modules/hibernate/resume", mode => 644, require => Exec["echo"], } # should not be execute as Exec["echo"] is "false"! # ... but is executed because "resume" had been created exec { "update-initramfs": name => "update-initramfs -u", refreshonly => true, subscribe => File["resume"], } } Even Exec["echo"] is not beeing executed, the file "resume" will be created and "update-initramfs" is executed. The workaround is to avoid the file-type an simple use an exec with an "echo .. > file" in it. class hibernate { exec { "echo RESUME=/dev/mapper/swap_crypt > /etc/initramfs-tools/conf.d/resume": alias => "echo", onlyif => "test -b /dev/mapper/swap_crypt -a ! -f /etc/initramfs-tools/conf.d/resume" , } # if "onlyif" is successful, then Exec["update-initramfs"] is triggerd exec { "update-initramfs": name => "update-initramfs -u", refreshonly => true, subscribe => Exec["echo"], } } Is there any way to use something like "refreshonly" also for File? Patrick -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
Daniel Pittman
2011-Jan-28 16:50 UTC
Re: [Puppet Users] Create a file only under certain conditions
On Jan 28, 2011 6:12 AM, "Patrick Cervicek" < patrick@googlealtert.spamtrap.fht-esslingen.de> wrote:> > Is there a way to create a file only under certain conditions?Totally, and we did it routinely. The process is perhaps not quite as simple as you might expect though: You need to write a custom fact to run on the client, and which reports the presence or absence of the device to the server. Your manifest can then use that fact to determine what other actions to take. Custom facts in modules are pretty trivial to write. :) Regards, Daniel -- Puppet Labs Developer –http://puppetlabs.com Daniel Pittman <daniel@puppetlabs.com> Contact me via gtalk, email, or phone: +1 (877) 575-9775 Sent from a mobile device. Please forgive me if this is briefer than usual. -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
jcbollinger
2011-Jan-28 20:50 UTC
[Puppet Users] Re: Create a file only under certain conditions
On Jan 28, 8:12 am, Patrick Cervicek <patr...@googlealtert.spamtrap.fht-esslingen.de> wrote:> Is there a way to create a file only under certain conditions?What Daniel said. For the record, though:> class hibernate { > > notice("hibernate") > > exec { "echo": > alias => "echo", > # onlyif => "test -b /dev/mapper/swap_crypt -a ! -f /etc/initramfs-tools/conf.d/resume" , > # onlyif => "true" , > onlyif => "false" , > } > > # should not be created as Exec["echo"] is "false"! > # ... but "resume" is still created > file { "resume": > name => "/etc/initramfs-tools/conf.d/resume", > source => "puppet:///modules/hibernate/resume", > mode => 644, > require => Exec["echo"], > }I think you have a misconception here about the semantics of onlyif. OnlyIf does not cause an Exec to fail when its condition is false. Instead, it causes the Exec to *succeed* without actually running the command. This is intentional, with the express purpose that resources that depend on an Exec are still applied when the Exec is short- circuited. More generally, the Exec resource is not intended for the purpose to which you are trying to put it. You could probably force it to serve by making your onlyif command be the main command, but that''s ugly, and it will report errors on most runs. Instead, the puppetmaster should decide, based on the facts presented to it, what resources should be applied on each node. John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.