我有一个在Puppet中使用的Facter自定义事实应该生成一个特定信息列表,但它并不总是正常工作,即使在运行完全相同版本的ruby,facter,puppet和mysql的服务器上也是如此。代码如下:
#backends.rb
puts "Backends retrieval"
if Facter.value('demainversion') == '2'
dbasename = Facter.value('dbasename')
if dbasename == ''
dbasename = 'database'
end
mysqlcmd = "mysql -uUser -pPassword #{dbasename} --skip_column_names -e "
if Facter.value('clustered')
dbasename = Facter.value('dbasename')
mysqlcmd = "mysql -uUser -pPassword -hclustered-host #{dbasename} --skip_column_names -e "
end
puts mysqlcmd
clients = Facter.value('databases').split(', ')
if clients != ''
clients.each do |client|
count = 0
client_id =
res =
backends = ""
facter_name = "#{client.gsub(' ','_')}_backends"
Facter.add("#{facter_name}") do
setcode do
puts "#{client} listing"
client_id = %x[#{mysqlcmd}"SELECT id FROM UserGroup WHERE groupName='#{client.strip}'"].strip
puts "#{client_id} listing"
puts backends
res = %x[#{mysqlcmd}"SELECT sourceMachine,sourceUser FROM GrabberJob WHERE clientGroup_id=#{client_id.strip}"].strip
res.each do |row|
machine = row.split(' ')[0]
login = row.split(' ')[1]
val = login.gsub('admin','')
if count == 0
backends << machine.strip << "(" << val << ")"
else
backends << ", " << machine.strip << "(" << val << ")"
end
count += 1
end
backends.strip
end
end
end
end
end
当木偶(或irb)运行时,它可能(或可能不)返回......
第1行的错误1046(3D000):未选择数据库
...但是如果服务器抛出错误,它将继续显示问题。我知道dbasename变量设置正确,由于用于调试的puts语句,但在实际运行mysql命令时忽略该变量。
我还尝试在client_id和res命令中添加USE #{dbasename};
语句无效。关于我能做些什么以使其始终如一地工作的任何建议都将非常受欢迎。