如何使用 Ruby 将值添加到 YAML 哈希
How to add values to a YAML hash using Ruby
我在 YAML 文件中有一堆哈希值(它用于某些服务器的基于 Puppet/Hiera 的配置),看起来像这样:
---
apache_vhosts:
'webuser.co.uk':
ip: '*'
port: '80'
serveraliases: ['www.webuser.co.uk',]
add_listen: false
docroot: '/home/webuser/public_html'
docroot_owner: 'webuser'
docroot_group: 'apache'
serveradmin: 'webmaster@webuser.co.uk'
scriptalias: '/home/webuser/public_html/cgi-bin/'
access_log_format: '\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"'
override: 'all'
users:
'webuser':
ensure: 'present'
gid: '500'
managehome: true
home: '/home/webuser'
password: '$zix5AzRheEzQwadthjvLNh.8maO6o4DU4Y0POTaS6xfgjfdvihP2O/UQN6eVDHjG2hTCT6VTLk5HsXeB9FF0xMlYiYY9W1'
password_max_age: '99999'
password_min_age: '0'
shell: '/sbin/nologin'
uid: '500'
我需要以自动方式附加到 Ruby 中的这些哈希值。这个想法是一个请求进入并命中一个运行 ruby 脚本的 webhook,该脚本添加了一个新的 Apache VHost 和伴随用户。 Ruby 文档在操作 YAML 方面的内容严重不足,Google 搜索也没有提供任何相关信息。也许有人可以指出正确的方向?
在 Ruby 中使用 YAML 并不多。我想你只需要知道这里的两个方法:YAML.load
和 YAML.dump
。
假设文件 file.yml 包含您提供的内容:
# YAML is part of the standard library.
require 'yaml'
# YAML.load parses a YAML string to appropriate Ruby objects.
# So you can first load the contents of the file with File#read,
# then parse it.
yaml_string = File.read "file.yml"
data = YAML.load yaml_string
# Now you have all of it in data.
data["apache_vhosts"]
# => {"webuser.co.uk"=>{"ip"=>"*", ...
# Once you are done manipulating them, dump it back with YAML.dump
# to convert it back to YAML.
output = YAML.dump data
File.write("file.yml", output)
我想差不多就是这样了。
更新
好的,现在实际上是关于附加已解析的数据。解析的意思是解析出来的数据格式要和现有的格式一致。
假设您有一个名为 new_user
的新用户的有效解析信息:
new_user_info = {"ensure"=>"present", "gid"=>"900", "managehome"=>true, "home"=>"/home/new_user"}
要将其附加到原始 YAML 内容(解析为 ruby 个对象),您可以这样做:
data["users"]["new_user"] = new_user_info
转储后,这将在用户列表底部添加另一个名为 new_user
的用户条目(在 YAML 文件的 users:
下)。主机也可以用同样的方式添加,一旦你得到域名和其他信息,你可以这样添加它们:
data["apache_vhosts"]["new_domain_name"] = info
同样重要的是将信息安排在正确的层次结构中。
我在 YAML 文件中有一堆哈希值(它用于某些服务器的基于 Puppet/Hiera 的配置),看起来像这样:
---
apache_vhosts:
'webuser.co.uk':
ip: '*'
port: '80'
serveraliases: ['www.webuser.co.uk',]
add_listen: false
docroot: '/home/webuser/public_html'
docroot_owner: 'webuser'
docroot_group: 'apache'
serveradmin: 'webmaster@webuser.co.uk'
scriptalias: '/home/webuser/public_html/cgi-bin/'
access_log_format: '\"%{X-Forwarded-For}i\" %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"'
override: 'all'
users:
'webuser':
ensure: 'present'
gid: '500'
managehome: true
home: '/home/webuser'
password: '$zix5AzRheEzQwadthjvLNh.8maO6o4DU4Y0POTaS6xfgjfdvihP2O/UQN6eVDHjG2hTCT6VTLk5HsXeB9FF0xMlYiYY9W1'
password_max_age: '99999'
password_min_age: '0'
shell: '/sbin/nologin'
uid: '500'
我需要以自动方式附加到 Ruby 中的这些哈希值。这个想法是一个请求进入并命中一个运行 ruby 脚本的 webhook,该脚本添加了一个新的 Apache VHost 和伴随用户。 Ruby 文档在操作 YAML 方面的内容严重不足,Google 搜索也没有提供任何相关信息。也许有人可以指出正确的方向?
在 Ruby 中使用 YAML 并不多。我想你只需要知道这里的两个方法:YAML.load
和 YAML.dump
。
假设文件 file.yml 包含您提供的内容:
# YAML is part of the standard library.
require 'yaml'
# YAML.load parses a YAML string to appropriate Ruby objects.
# So you can first load the contents of the file with File#read,
# then parse it.
yaml_string = File.read "file.yml"
data = YAML.load yaml_string
# Now you have all of it in data.
data["apache_vhosts"]
# => {"webuser.co.uk"=>{"ip"=>"*", ...
# Once you are done manipulating them, dump it back with YAML.dump
# to convert it back to YAML.
output = YAML.dump data
File.write("file.yml", output)
我想差不多就是这样了。
更新
好的,现在实际上是关于附加已解析的数据。解析的意思是解析出来的数据格式要和现有的格式一致。
假设您有一个名为 new_user
的新用户的有效解析信息:
new_user_info = {"ensure"=>"present", "gid"=>"900", "managehome"=>true, "home"=>"/home/new_user"}
要将其附加到原始 YAML 内容(解析为 ruby 个对象),您可以这样做:
data["users"]["new_user"] = new_user_info
转储后,这将在用户列表底部添加另一个名为 new_user
的用户条目(在 YAML 文件的 users:
下)。主机也可以用同样的方式添加,一旦你得到域名和其他信息,你可以这样添加它们:
data["apache_vhosts"]["new_domain_name"] = info
同样重要的是将信息安排在正确的层次结构中。