user6378152
user6378152

Reputation: 277

chef: only_if when a pattern exists in a file

I need to run a sed command when 2 conditions are met-

  1. a file exists (eg MYFILE.txt)
  2. file contains one particular line (e.g. /home/user/tools/)

I know I can use only_if guard in chef, but I don't know how to mention second condition in the bash command.

Sample code:

bash 'changing path' do
  code <<-EOH
    cd /path
    sed -i -e "s:/home/user/tools/:/NEWPATH:" MYFILE.txt
  EOH
  only_if { ::File.exist?(MYFILE.txt && <<WHAT SHOULD COME HERE FOR (2)>> }
end

Upvotes: 0

Views: 940

Answers (1)

coderanger
coderanger

Reputation: 54181

Check out the line cookbook, it has helpers for this kind of thing which are automatically convergent.

Upvotes: 1

Related Questions