[英]Ruby aws-sdk - “.exists?” says the file doesn't exist even though I see it in the bucket
[英]Puppet RUBY template - Custom ruby fact is adding line even if the file doesn't exists
我創建了兩個 ruby 事實ecdsa.rb
和ed25519.rb
並檢查文件是否存在。 如果確實存在,則在文件中添加一行。 但即使第二個文件不存在,它也會添加兩行。
事實表明第二個文件不存在。
root@hostname:~# facter --json -p ecdsa_key_exists
{
"ecdsa_key_exists": true
}
root@hostname:~# facter --json -p ed25519_key_exists
{
"ed25519_key_exists": false
}
這是我寫的自定義事實。
ecdsa.rb content:
File /etc/ssh/ssh_host_ecdsa_key Exists.
Facter.add('ecdsa_key_exists') do
setcode do
File.exists?('/etc/ssh/ssh_host_ecdsa_key')
end
end
ed25519.rb content:
File /etc/ssh/ssh_host_ed25519_key has been deleted from the test server.
Facter.add('ed25519_key_exists') do
setcode do
File.exists?('/etc/ssh/ssh_host_ed25519_key')
end
end
Template test.erb:
<% if @ecdsa_key_exists -%>HostKey /etc/ssh/ssh_host_ecdsa_key<% end %>
<% if @ed25519_key_exists -%>HostKey /etc/ssh/ssh_host_ed25519_key<% end %>
但是當我運行 puppet agent -t 時,即使 @ed25519_key_exists 返回 false,這兩行也會被添加。
puppet module init.pp:
file { 'test.conf':
path => '/tmp/test.conf',
ensure => file,
content => template("ssh/test.erb"),
}
這取決於您使用的 puppet 版本,以及您是否打開或關閉了字符串化的事實(在較舊的 puppet 版本中,它們始終是字符串化的)。 當事實被字符串化時,您最終會得到字符串“false”或“true”,並且它們在 Ruby 中都被認為是 boolean true。
通過打印數據類型和事實值來檢查這一點。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.