Search code examples
puppet

Function Reference - create_resources in puppet


refer https://docs.puppetlabs.com/references/latest/function.html#createresources

# A hash of user resources:
$myusers = {
  'nick' => { uid    => '1330',
              gid    => allstaff,
              groups => ['developers', 'operations', 'release'], },
  'dan'  => { uid    => '1308',
              gid    => allstaff,
              groups => ['developers', 'prosvc', 'release'], },
}

create_resources(user, $myusers)

Read the explanation for this function create_resources, but not sure what result after create_resources(user, $myusers)

Does it create two users nick and dan with nominated uid, gid and groups?

updates:

some explanations from web.

Listing 12-79. Hiera lookup of sysadmin hash
root@puppet-master-hiera-ubuntu:/etc/puppet/data# hiera sysadmins
{"spencer"=>{"uid"=>1861, "groups"=>"root", "gid"=>300}, "william"=>{"uid"=>11254,
"groups"=>"root", "gid"=>300}}

   Now we can use a function called create_resources() to generate Puppet resources from this hash, as shown in

Listing 12-80. create_resources example

$sysadmins = hiera('sysadmins')

create_resources(user, $sysadmins)

    Listing 12-81 shows the output.

Listing 12-81. Applying the create_resources example from Listing 12-80

root@puppet-master-hiera-ubuntu:/etc/puppet# puppet apply create_resources.pp
Notice: Compiled catalog for puppet-master-hiera-ubuntu.green.gah in environment production in
0.11 seconds
Notice: /User[spencer]/ensure: created
Notice: /User[william]/ensure: created
Notice: Finished catalog run in 0.32 seconds

I can't properly set and prove it in my environment, but above sample gives the answer, it DOES create user accounts with hash in create_resources function.


Solution

  • create_resources will just map

    # A hash of user resources:
    $myusers = {
      'nick' => { uid    => '1330',
                  gid    => allstaff,
                  groups => ['developers', 'operations', 'release'], },
      'dan'  => { uid    => '1308',
                  gid    => allstaff,
                  groups => ['developers', 'prosvc', 'release'], },
    }
    
    create_resources(user, $myusers)
    

    into

      user{'nick':
        uid    => '1330',
        gid    => allstaff,
        groups => ['developers', 'operations', 'release'], },
      }
      user{'dan':
        uid    => '1308',
        gid    => allstaff,
        groups => ['developers', 'prosvc', 'release'], },
      }
    

    So those users will be passed to user provider. You can test it easily creating a users.pp file with your recipes, and testing it with puppet apply --noop :

    # puppet apply --noop users.pp 
    Notice: Compiled catalog for yourfqdn in environment production in 0.15 seconds
    Notice: /Stage[main]/Main/User[nick]/ensure: current_value absent, should be present (noop)
    Notice: /Stage[main]/Main/User[dan]/ensure: current_value absent, should be present (noop)
    

    Notice that if users already exist, puppet apply wont do anything