2

I have written the following code and this works fine if swap is not in place, but once swap is created and if the script is executed again, it leads to error.

I tried adding conditions using onlyif and unless which are commented in the following code but none of them seem to be working

class swap {

   exec { "create swap file":
      command => "dd if=/dev/zero of=/swapfile bs=1M count=1024",
      path     => "/bin/",
      creates => "/swapfile",
   }

   exec { "makeswap":
      command => "mkswap /swapfile",
      path     => "/sbin/",
      # condition so that the block is executed only if the swap is not place
      onlyif => "/sbin/swapon -s | /bin/grep file > /dev/null",
      require => Exec['create swap file'],
   }

   exec { "enable swap":
      command => "swapon /swapfile",
      path     => "/sbin/",
      require => Exec['makeswap'],
   }
}
include swap

Below is the console log on debug

[root@puppet testpuppet]# puppet apply swap.pp --debug
...
Debug: Finishing transaction 70032061368300
Debug: Loaded state in 0.00 seconds
Debug: Loaded state in 0.00 seconds
Debug: /Stage[main]/Swap/Exec[makeswap]/require: requires Exec[create swap file]
Debug: /Stage[main]/Swap/Exec[enable swap]/require: requires Exec[makeswap]
Info: Applying configuration version '1404202521'
Debug: Exec[makeswap](provider=posix): Executing check '/sbin/swapon -s | /bin/grep file > /dev/null'
Debug: Executing '/sbin/swapon -s | /bin/grep file > /dev/null'
Debug: Exec[makeswap](provider=posix): Executing 'mkswap /swapfile'
Debug: Executing 'mkswap /swapfile'
Notice: /Stage[main]/Swap/Exec[makeswap]/returns: executed successfully
...
Felix Frank
  • 3,063
  • 1
  • 15
  • 22
user228708
  • 63
  • 1
  • 1
  • 7
  • First off, please enable the parameter you wish to use, then run with the `--debug` flag to get an output of what Puppet is running. Paste the command to your shell and check the return code. - Add resulting information to your question please. – Felix Frank Jul 01 '14 at 07:26
  • I have provided the required info... Thanks a lot for showing interest in this query. – user228708 Jul 01 '14 at 08:32
  • Please keep the information at the necessary minimum. I removed the spurious information and fixed your formatting. You also failed to debug the execution of `/sbin/swapon -s | /bin/grep file > /dev/null`, which is the actual point I'm getting at. – Felix Frank Jul 01 '14 at 11:16
  • This log doesn't show the _failure_, it shows the _success_. Try again and this time show the failure. – Michael Hampton Jul 01 '14 at 12:08
  • onlyif => "/sbin/swapon -s | /bin/grep file > /dev/null" - This step works only if there is no swap in place, but if the swap file is already created, then the step fails. The whole idea of adding this step is to check and execute the command- makeswap only if the swap is not in place – user228708 Jul 01 '14 at 17:43

1 Answers1

3

The issue has been resolved by adding following statement in

unless => "/sbin/swapon -s | /bin/grep file > /dev/null"

user228708
  • 63
  • 1
  • 1
  • 7