Help on Hiera automate

72 views
Skip to first unread message

alessandro mazzoli

unread,
Jan 13, 2014, 8:02:29 PM1/13/14
to puppet...@googlegroups.com
Hi all,
I'm trying to automate the set up of the machines staging,passing by an php application ServerName , ProjectType,DbCredentials etc..(all strings) and convert to yaml
Here's my common.yaml:

---
users:
  -project : Symfony
  -dbname : test
  -dbuser  : test
  -dbpwd : test
  -project : Idephix
  -dbname : test
  -dbuser  : test
  -dbpwd : test

Here's my hiera.yaml
---
:backends:
   -yaml
:yaml:
  :datadir: '/etc/puppet/hieradata'
:hierarchy:
  -common


Is there any possibility on manifest to get all those variables by not specifing the index as below ??
$users=hiera('users')
$sn0=$users[0]['sn']
$sn1=
......
...

I would like to preserve all the configurations if possible inside common.yaml

Thanks for the help



CD

unread,
Jan 13, 2014, 10:11:33 PM1/13/14
to puppet...@googlegroups.com
Hi Alessandro,

I think if you specify $users it has all the content of children but in a single string. Don't know whether this is what you need.

try:
$users=hiera('users')
notify { $users: }

Regards,
CD

alessandro mazzoli

unread,
Jan 14, 2014, 4:00:54 AM1/14/14
to puppet...@googlegroups.com
Yes, i can get the whole string but if i need the parameters inside that string as sn, project,dbname etc...

example:
   file{"/var/sites/$sn":
                        ensure=>directory,
                        mode=>0755,
                        owner=>$user,
                        group=>$user,
                        require=>File["/var/sites"],
                }

jcbollinger

unread,
Jan 14, 2014, 9:32:34 AM1/14/14
to puppet...@googlegroups.com


I don't understand.  If you didn't specify an index, then how would you expect Puppet to know which sn you're talking about?

Your data are laid out quite strangely, however, probably not as you intended.  The value of the top-level 'users' key is an array of one-element hashes, which are not all similar in apparent data content.  Perhaps this would be closer to what you want:

users:
  -
    sn: server.example.com
    project : Symfony
    dbname : test
    dbuser  : test
    dbpwd : test
  -
    sn: server2.example.com
    project : Idephix
    dbname : test
    dbuser  : test
    dbpwd : test

That makes 'users' an array containing only two elements, each one of them a hash with five keys.  You would still need to index into the array and then into the hash, but all the data associated with a given server would be in the same hash, accessed at the same array index.

Depending on how you want to use it, something like this might also be useful:

users:
    project : Symfony
    dbname : test
    dbuser  : test
    dbpwd : test
    project : Idephix
    dbname : test
    dbuser  : test
    dbpwd : test

That makes 'users' a hash of hashes, with server names as the outer hash keys.  You would access leaf elements like so:

  $users = hiera('users')
  $project = $users['server.example.com']['project']

If you don't want to rely on prior knowledge of the outer hash keys (i.e. the server names) then you can extract them from the data via the keys() function, available from Puppetlabs's "stdlib" add-in module (https://forge.puppetlabs.com/puppetlabs/stdlib).


John

alessandro mazzoli

unread,
Jan 15, 2014, 6:54:48 AM1/15/14
to puppet...@googlegroups.com
Thanks Jc,
i did what you said..i have changed .yaml like this:
  servers:
  s10:
    sn: de3de
    project: b
    user: b
    dbname: de
    dbuser: e
    dbpwd: de
  s16:
    sn: de3dedede
    project: b
    user: a
    dbname: de
    dbuser: e
    dbpwd: d
...
 and obtain params like this hiera('servers') and use create_resources function
Reply all
Reply to author
Forward
0 new messages