Newbie question about fetching specific value of a key from hash

33 views
Skip to first unread message

Sam

unread,
Feb 3, 2016, 3:28:11 PM2/3/16
to Puppet Users
My hiera entries -

mysqlconfig::custom_mysql_options:
  mysqld:
    replicate-ignore-db: 'test'
    server-id: 12
    datadir: '/data/mysql_data'


I want to get the value of datadir in my puppet module. Please let me know what is the syntax I need to use to fetch this value. I tried something as follows but it doesn't work. 


$datadir = hiera('mysqlconfig::custom_mysql_options[datadir]')

  file { "$datadir":
    ensure => directory,
    owner  => 'mysql',
    group  => 'mysql',
    mode   => '0755',
  }


Thanks in advance
Sam

Martin Alfke

unread,
Feb 4, 2016, 1:21:14 PM2/4/16
to puppet...@googlegroups.com
Hi Sam
On 03 Feb 2016, at 16:28, Sam <kaila...@gmail.com> wrote:

> My hiera entries -
>
> mysqlconfig::custom_mysql_options:
> mysqld:
> replicate-ignore-db: 'test'
> server-id: 12
> datadir: '/data/mysql_data'
>
>
> I want to get the value of datadir in my puppet module. Please let me know what is the syntax I need to use to fetch this value. I tried something as follows but it doesn't work.
>
>
> $datadir = hiera('mysqlconfig::custom_mysql_options[datadir]’)

With hiera you can only specify keys, not elements of a hash.

you should change this to:

$custom_mysql_options = hiera(‘mysqlconfig::custom_mysql_options’)

You can now set a new variable reading the hash element:

$datadir = $custom_mysql_options[‘datadir’]

>
> file { "$datadir":
> ensure => directory,
> owner => 'mysql',
> group => 'mysql',
> mode => '0755',
> }
>
>
hth,
Martin

> Thanks in advance
> Sam
>
> --
> You received this message because you are subscribed to the Google Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/95efaffa-4873-4fda-9caa-3a38e7b3c1de%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Craig Dunn

unread,
Feb 4, 2016, 1:31:33 PM2/4/16
to puppet...@googlegroups.com
>> I want to get the value of datadir in my puppet module. Please let me know what is the syntax I need to use to fetch this value. I tried something as follows but it doesn't work.
>>
>>
>> $datadir = hiera('mysqlconfig::custom_mysql_options[datadir]’)
>
> With hiera you can only specify keys, not elements of a hash.

That's not the case with Hiera 2.0+ which supports drilling down into
hashes and arrays... so you should also be able to do....

$datadir = hiera('mysqlconfig::custom_mysql_options.datadir')

If you're still on Hiera 1.0 though, what Martin said.
Reply all
Reply to author
Forward
0 new messages