anonymous
anonymous

Reputation: 1968

chef running resource only when a package is present

I want to unzip one file in chef recipe but I want to ensure that unzip package is install on that machine before I unzip it. How to do it in chef ?

I tried below recipe but on machine where unzip package is install it simply don't execute it.

bash 'extract' do
    cwd '/home/norun/'
    code <<-EOH
        unzip wso2.zip
        EOH
    only_if { ::File.exists?('/home/norun/wso2.zip') }
    not_if { ::File.exists?('/home/norun/wso2as-5.2.1') }
end

apt_package 'unzip' do
    action :install
    notifies :run , 'bash[extract]', :immediately
end

Thanks In Advance

Upvotes: 1

Views: 373

Answers (1)

StephenKing
StephenKing

Reputation: 37580

Just change the order, as order matters in chef:

package 'unzip' do
    action :install
    # notifies :run , 'bash[extract]', :immediately
end

This will make sure that the unzip utility is installed. Btw you can use the package resource, as this makes your code more portable. Chef will automatically select the right provider. Further, I suggest to remove the notification from package[unzip], as the unzip process will not be triggered, if unzip was already installed.

bash 'extract_wso2' do
    cwd '/home/norun/'
    code 'unzip wso2.zip'
    only_if { ::File.exists?('/home/norun/wso2.zip') }
    not_if { ::File.exists?('/home/norun/wso2as-5.2.1') }
end

You don't need to notify the bash[extract_wso2] resource, as it is guarded by the not_if.

Instead of manually unzipping, you might also want to use the ark cookbook.

Upvotes: 2

Related Questions