0

I have a hash with a list in hiera. I use this to automatically create the dbs in the RDBMS via create_resources

dbs:
  - dbname: something
  - dbname: or
  - dbname: other

I would like to use this same list to automatically create a plain text file with one entry per line, for use by our backup script. This list will always be present with an arbitrary number of entries >=1.

The resulting file should then look like

something
or
other

What's the best way to do this? I haven't been able to find anything.

1 Answers1

1

First you need to loop over your array, then you need to take the value of every hash, because you have an array with hash:

sqldbs = lookup('dbs')

each($sqldbs) |$db| {
  notice($db)
}

root@6bb0b3e9ec6c:/# puppet apply hello.pp -vvv
Notice: Scope(Class[main]): {dbname1 => something}
Notice: Scope(Class[main]): {dbname2 => or}
Notice: Scope(Class[main]): {dbname2 => other}

As you can notice, you have 3 elements, now you can take the element of every hash

Now with an small line of code.

$sqldbs = lookup('dbs')

each($sqldbs) |$db| {
  notice($db.values.join(','))
} 

root@6bb0b3e9ec6c:/# puppet apply hello.pp -vvv
Notice: Scope(Class[main]): something
Notice: Scope(Class[main]): or
Notice: Scope(Class[main]): other
Notice: Compiled catalog for 6bb0b3e9ec6c.cc.cec.eu.int in environment production in 0.04 seconds

Now you can use file_line to insert the data in a file

c4f4t0r
  • 5,301
  • 3
  • 31
  • 42
  • 1
    Except don't use `file_line` as it's nasty, it won't purge old entries for one thing. Use a `concat` resource instead. – bodgit Dec 11 '18 at 11:39