Issue
In my cookbook I have:
unless app_rpm.nil?
node.default['app_rpms'] = \
%w(agent1 agent2 agen3 agent4)
node['app_rpms'].each do |rpm_package|
package rpm_package do
action :remove
not_if { "rpm -qa | grep -qx '#{node['agents']['agent']['type']['rpm_full_name']}'" }
end
end
end
However, despite defining "node['agents']['agent']['type']['rpm_full_name']" in the role with a correct value along the lines of agent-version.arch (e.g. agent1-22.06-01.noarch), on running rake unit I get:
WARN: not_if block for yum_package[agent1] returned "rpm -qa | grep -qx ''", did you mean to run a command? If so use 'not_if "rpm -qa | grep -qx ''"' in your code
This happens for all the agents.
UPDATE: I have amended the code to now have a separate line to create the command in a var. This is then referenced in the not_if. i.e.:
rpm_check_command = "rpm -qa | grep -qx '#{node['agent']['type']['rpm_full_name']}'"
not_if { rpm_check_command }
Now I am getting:
WARN: not_if block for yum_package[agent1] returned "rpm -qa | grep -qx 'agent1-21.11.2-0.noarch'", did you mean to run a command? If so use 'not_if "rpm -qa | grep -qx 'agent1-21.11.2-0.noarch'"' in your code.
How do I make this into a command?
Solution
Following the examples on the documentation page, curly braces are not required to run command for guards.
So, we should be able to write it as:
rpm_check_command = "rpm -q #{node['agent']['type']['rpm_full_name']}"
# ...
node['app_rpms'].each do |rpm_package|
package rpm_package do
action :remove
not_if rpm_check_command
end
end
# ...
Since you have the complete package name in the attribute I've omitted the | grep
part. The rpm -q <packagename>
command itself will return non-zero if a matching package is not found.
Answered By - seshadri_c Answer Checked By - Clifford M. (WPSolving Volunteer)