How can i run a next action resource after the package has been installed during chef-client converge run?

Here’s my recipe logic:

kernel = rpm -qa | grep -P -o '(?<=kernel-devel-).*'.strip
bash ‘kernel-patch’ do
code <<-EOH
yum install -y kexec-tools
yum install -y kernel-devel
KERN_DIR=/usr/src/kernels/#{kernel}
EOH
action :run
notifies :run, “ruby_block[kernel-exec #{kernel}]”, :immediate
not_if { node[‘kernel’][‘release’] == kernel }
Chef::Log.debug(‘DEBUG level message’)
end

Kernel boot img repatch. This resource will be called if the kernel update did not update the current kernel version.

ruby_block “kernel-exec #{kernel}” do
block do
shell_out!(“kexec -l /boot/vmlinuz-#{kernel} --initrd=/boot/initramfs-#{kernel}.img --reuse-cmdline”)
end
end

The variable #(kernel} is empty on first chef-client run (converged stage), since the resources are complied before with it’s current settings. But how can i run a next action resource after the package has been installed during chef-client converge run?

Have you tried using lazy evaluation of #{kernel} in the ruby_block ?

Hello,

Thanks for your response, how to use lazy or lambda?

Read here for information on lazy evaluation: https://docs.chef.io/resource_common.html#lazy-evaluation

But I am not sure if it will work inside ruby_block but you may be able to run your commands using bash or execute.

Another option is to use node.run_state which also works with ruby_block.

Hi,
Tried using node.run_state with new attribute variable and its getting data but it throw's out an error.

### Kernel code block
ruby_block 'kernel_var' do
  block do
    node.run_state['kernel_devel'] = `rpm -qa |  grep -P -o '(?<=kernel-devel-).*'`
  end
end
bash 'kernel-kexec-apply' do
  code lazy { "kexec -l /boot/vmlinuz-#{node.run_state['kernel_devel']} --initrd=/boot/initramfs-#{node.run_state['kernel_devel']}.img --reuse-cmdline && kexec -e" }
  notifies :reboot_now, 'reboot[node]', :delayed
  action :nothing
  not_if { kernel_release == kernel_devel }
end

But throw's out command not found, I am not sure how chef interprets this:

image

Looks like it is not able to run that as a single command and so it throws such an error. All you need to do is build up the command before giving it to code or try using it like this:

bash 'kernel-kexec-apply' do
  code <<-EOH
      lazy { kexec -l /boot/vmlinuz-#{node.run_state['kernel_devel']} --initrd=/boot/initramfs-#{node.run_state['kernel_devel']}.img --reuse-cmdline && kexec -e }
  EOH 
  notifies :reboot_now, 'reboot[node]', :delayed
  action :nothing
  not_if { kernel_release == kernel_devel }
end
1 Like