在下面的示例中,我尝试尽可能将配置文件设置为DRY,而不依赖于目标源中的eval。第一个看跌期权给出了预期,第二个看跌期权,它也应该给出“秘密”。 有关如何在YAML文件中对密钥进行别名的任何想法吗?
require 'yaml'
CONFIG = YAML.load DATA
puts CONFIG['org2']['ldappassword'] # Secret
puts CONFIG['org3']['ldappassword'] #<<:*DEFAULT['ldappassword'] should be Secret
__END__
org1: &DEFAULT
treebase: 'DC=Organisation,DC=be'
ldaphost: 9.9.9.9
ldapusername: ORG\999999
ldappassword: Secret
port: 389
org2:
<<: *DEFAULT
org3:
treebase: 'OU=Employees,OU=Accounts,OU=Organiationname,OU=Departments,DC=otherorganisation,DC=be'
ldaphost: 8.8.8.8
ldapusername: ORG\999998
ldappassword: <<:*DEFAULT['ldappassword']
port: 389
答案 0 :(得分:1)
你应该“重写”像这样的默认值
org1: &DEFAULT
treebase: 'DC=Organisation,DC=be'
ldaphost: 9.9.9.9
ldapusername: ORG\999999
ldappassword: Secret
port: 389
org2:
<<: *DEFAULT
org3:
<<: *DEFAULT
treebase: 'OU=Employees,OU=Accounts,OU=Organiationname,OU=Departments,DC=otherorganisation,DC=be'
ldaphost: 8.8.8.8
ldapusername: ORG\999998
port: 389