optionally passing a parameter to puppet resource -
is there way pass parameter puppet resource if it's been set? example, have manifest creates new user, , may or may not want set uid , gid manually. best way can think this:
class example ( $uid = undef, $gid = undef, $username = 'default' ) { user { $username: ensure => present, # etc } if $uid != undef { user { "uid-${username}": name => $username, uid => $uid, require => user[$username] } } if $gid != undef { user { "gid-${username}": name => $username, gid => $gid, require => user[$username] } } }
but lot of code determine whether or not send uid and/or gid. there better way?
i think example fail compile due multiple declarations, because name
on user
resource being checked uniqueness. being said, there couple ways make better in opinion.
the first way involves attribute amending:
... user { $username: ensure => present, # etc } if $uid != undef { user[$username] { uid => $uid } } if $gid != undef { user[$username] { gid => $gid } }
the second involves setting attributes hash:
class example ( hash $id = {}, string $username = 'default' ) { user { $username: ensure => present, * => $id, # etc } }
and either passing additional parameters in via declaration:
class example { 'foo': username => 'bar', id => { uid => 1000, gid => 1000 } }
or using automatic hiera lookups similar to:
example::id: uid: 1000 gid: 1000
note not specifying id
in either case (declaration or hiera) still result in correct , expected behavior of user
resource without uid
or gid
altered.
you can find both methods documented @ https://docs.puppet.com/puppet/latest/reference/lang_resources_advanced.html, , may able figure out method or 2 document.
Comments
Post a Comment