简体   繁体   中英

puppet hiera array, loop and hash

I have currently an issue between hiera/puppet:

In my hiera I have:

mysql_user_mgmt:
     - mysql_user: 'toto@localhost'
       mysql_hash_password: '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29'
       mysql_grant_user: 'toto@localhost/*.*'
       mysql_user_table_privileges: '*.*'
     - mysql_user: 'test@localhost'
       mysql_hash_password: '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29'
       mysql_grant_user: 'test@localhost/*.*'
       mysql_user_table_privileges: '*.*'

In my puppet, I'm trying to make a loop to get data from hiera:

$mysql_user_mgmt = hiera('mysql_user_mgmt',undef)
define mysql_loop () {
$mysql_hash_password = $name['mysql_hash_password']
notify { "mysql_hash_password: ${mysql_hash_password}": }
}
mysql_loop { $mysql_user_mgmt: }

but I'm getting some weird errors. Can someone help me to figure out how to make the loop?

Resource titles are strings . Always.

You are trying to use the the title of a mysql_loop resource to feed a hash to the type definition. That does not work. A stringified version of the hash will end up being used instead, and your later attempts to retrieve components by hash index will fail, likely with some kind of type error.

You have a few options:

  1. You could restructure your definition and data a bit, and pass the aggregate data as a hash parameter. (Example below.)

  2. You could restructure your definition and data a bit, and use the create_resources() function.

  3. If you've moved up to Puppet 4, or if you are willing to enable the future parser in Puppet 3, then you could use one of the new(ish) looping functions such as each() .

Example of alternative (1):

Reorganize the data to a hash of hashes, keyed on the user id:

mysql_user_mgmt:
  'toto@localhost':
     mysql_hash_password: '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29'
     mysql_grant_user: 'toto@localhost/*.*'
     mysql_user_table_privileges: '*.*'
  'test@localhost':
     mysql_hash_password: '*94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29'
     mysql_grant_user: 'test@localhost/*.*'
     mysql_user_table_privileges: '*.*'

Modify the definition:

define mysql_user ($all_user_info) {
  $mysql_hash_password = $all_user_info[$title]['mysql_hash_password']
  notify { "mysql_hash_password: ${mysql_hash_password}": }
}

Use it like so:

$mysql_user_mgmt = hiera('mysql_user_mgmt',undef)
$mysql_user_ids = keys($mysql_user_mgmt)
mysql_user { $mysql_user_ids: all_user_info => $mysql_user_mgmt }

(The keys() function is available from the puppetlabs-stdlib module.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM