kemra102
kemra102

Reputation: 531

How to add values to a YAML hash using Ruby

I have a bunch of Hashes in a YAML file (its for Puppet/Hiera based config of some servers) that looks like so:

---
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: '[email protected]'
    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: '$6$zix5AzRheEzQwadthjvLNh.8maO6o4DU4Y0POTaS6xfgjfdvihP2O/UQN6eVDHjG2hTCT6VTLk5HsXeB9FF0xMlYiYY9W1'
    password_max_age: '99999'
    password_min_age: '0'
    shell: '/sbin/nologin'
    uid: '500'

I need to be append to these hashes in Ruby in an automated fashion. The idea is that a request comes in and hits a webhook that runs a ruby script that's adds a new Apache VHost and accompanying user. The Ruby docs are woefully inadequate from what I can see in regards to manipulating YAML, and a Google search doesn't bring up anything quite relevant. Perhaps someone could point me in the right direction?

Upvotes: 3

Views: 7977

Answers (1)

limekin
limekin

Reputation: 1944

There isn't much to using YAML in Ruby. I think you only need to know two methods in here : YAML.load and YAML.dump.

Assuming the file is file.yml with the contents you provided :

# 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)

That's pretty much it I think.

UPDATE

Okay now it's actually about appending already parsed data. By parsed I mean the data format parsed should be consistent with the existing format.

Assume that you have a valid parsed info on a new user named new_user :

new_user_info = {"ensure"=>"present", "gid"=>"900", "managehome"=>true, "home"=>"/home/new_user"}

To append it to the original YAML contents (parsed into ruby objects), you can do this :

data["users"]["new_user"] = new_user_info

Once dumped, this will add another user entry named new_user at the bottom of the users list (under users: on YAML file). Hosts can be added in the same way too, once you get the domain name and other info, you can add them like this :

data["apache_vhosts"]["new_domain_name"] = info

Again it's important to have the information arranged in the right hierarchy.

Upvotes: 11

Related Questions