Puppet, how to use the return value of exec as a trigger?

I have a bash script that do a check and return a boolean 0|1. An example of such a script is below:

# less /path/to/script/check_kernel.sh
#! /bin/bash
# Check if running kernel match default=0 kernel in grub.conf
KERNEL_RUNN=`/bin/uname -r | /bin/sed -e 's/^//' -e 's/[[:space:]]*$//'`
KERNEL_GRUB=`/bin/grep kernel /boot/grub/menu.lst | /bin/grep -v '#' \
| /bin/awk '{print $2}' | /bin/sed -e 's/\/vmlinuz-//g' | /usr/bin/head -1 \
| /bin/sed -e 's/^//' -e 's/[[:space:]]*$//'`

if [ "$KERNEL_RUNN" == "$KERNEL_GRUB" ]; then
  exit 0
else
  exit 1
fi

To run the specified shell script in Puppet, I would use the following code:

$check_kernel_cmd="/path/to/script/check_kernel.sh"
exec {'check_kernel':
  provider => shell,
  returns => [ "0", "1", ],
  command => "$check_kernel_cmd",
}

So now I need to use the return completion status above the exec resource Exec['check_kernel']as a trigger for another exec resource Exec['reboot_node'], something like:

if $check_kernel == '1' {
  $reboot = "/sbin/runuser - root -s /bin/bash -c '/sbin/shutdown -r'"
  exec {'reboot_node':
    provider => shell,
    command => "$reboot",
  }
}

or maybe a different style approach would be to use unlessas follows:

$reboot = "/sbin/runuser - root -s /bin/bash -c '/sbin/shutdown -r'"
exec {'reboot_node':
  provider => shell,
  command => "$reboot",
  unless => "/bin/echo $check_kernel",
  require => Exec['check_kernel'],
}

What would be the recommended approach / code to use the resource exit status execas a trigger for another resource execin the same manifest?

+4
source share
1

TL; DR . script , . , , script onlyif unless exec.


, , Master/Agent Puppet. , , . . , exec. , .

- - . , . .

, , exec script, , . , .

exec {
    '/sbin/shutdown -r':
        unless => '/path/to/script/check_kernel.sh';
}

:. Puppet - , , (, , , ).

+7

All Articles