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
...