JahMyst
JahMyst

Reputation: 1686

Chef - execute vs bash resource

I used both the execute resource or the bash resource.

Both achieve the same result:

bash 'Execute my script' do 
  user 'root'
  cwd  '/mydir'
  code <<-EOH
    ./myscript.sh
  EOH
end

execute 'Execute my script' do 
  user    'root'
  cwd     '/mydir'
  command './myscript.sh'
end

The only difference I see is that bash actually creates a shell script (named /tmp/chef-script#{date}{#id}) where code is written.

What is the best practice to execute a shell script with Chef between execute or bash resource ?

Upvotes: 6

Views: 15128

Answers (2)

Sri
Sri

Reputation: 41

In bash & execute block we need to write code to catch the error as if you add more than one command & the chef takes the status of the last command. To make it more clear - when the Bash/execute block has only one command chef catches the issue ,if the next command is successful then it takes the last command status.

bash 'Execute my script' do 
  user 'root'
  cwd  '/mydir'
  code <<-EOH
    ./myscript.sh
    ls srini ##This will fail
    ls  ## this will be successful
  EOH
end

execute 'Execute my script' do 
  user    'root'
  cwd     '/mydir'
  command './myscript.sh'
  command 'ls srini' #will fail
  command 'ls' # will be successful
end

Upvotes: -1

coderanger
coderanger

Reputation: 54211

For a single script, use an execute. The bash resource is for including the script contents inline in the recipe code.

Upvotes: 8

Related Questions