Copy files only if pattern found


#1

Hi,

I'm trying to create a recipe that will search pattern in files.
then it will copy them to /tmp.
anyone?

My recipe does not work.

execute 'cp_repo' do
cwd '/etc/yum.repos.d'
command 'cp /etc/yum.repos.d/.repo /tmp'
only_if "grep pattern /etc/yum.repos.d/
"
action :run
end


#2

Hey Guys,
Below my recipe that helped me with my question.
The solution is combine with ruby and chef resource.
Please tell me what do you think if i need to change something.

require 'open3'
get_repo_files = "grep -l pattern /etc/yum.repos.d/* | tr '/' ' ' | awk '{print $3}'"

stdout, stdeerr, status = Open3.capture3(get_repo_files)

array = stdout.split("\n")
array.each do | repo|
execute 'cp pattern file' do
command "cp /etc/yum.repos.d/#{repo} /tmp/backup/centos7/#{node['hostname']}/yum.repos.d/"
action :run
end
end