1

I have written the following snippet below using user resource in puppet to create unix users .

   node 'node2.example.com','node3.example.com'{
  user {
  'askar':
  ensure  => 'present',
  managehome => 'true',
  comment => 'man Home',
  home    => '/home/askar',
  shell   => '/bin/bash',
  expiry  => '2016-03-22',
  password => '$1$cs1j/t.D$4Q2Ocr0pulyNTUx/',
  password_min_age => '30',
  password_max_age => '60',
 }
exec {
'chage':
    path => '/usr/bin/',
    command => 'chage -d 0 askar',
  } 
}

It works fine for me, but the issue for me, I need to create 10 to 20 users. How can i reuse the above snippet to create 10 users. I am new to puppet , so pointers will be of great help.

Zama Ques
  • 523
  • 1
  • 9
  • 24

1 Answers1

2

You'll need to refactor your code to use a define, which you then reference from your node declarations.


modules/myuser/init.pp

define myuser(
  $password,
  $expiry = '2016-03-22',
  $shell  = '/bin/bash',
) {
  user { $title:
    ensure           => present,
    managehome       => true,
    comment          => 'Puppet managed user',
    home             => "/home/${title}",
    shell            => $shell,
    expiry           => $expiry,
    password         => $password,
    password_min_age => '30',
    password_max_age => '60',
  }
  exec { 'chage':
    command => "/usr/bin/chage -d 0 ${title}",
  } 
}

manifests/site.pp

node 'node2.example.com','node3.example.com'{
  myuser { 'user1':
    password => 'sdfsldkfldsfl'
  }

  myuser { 'user2':
    password => 'dfsdklsjflcd',
  }
}

Reference: https://docs.puppetlabs.com/puppet/latest/reference/lang_defined_types.html

Alternatively, you use use one of the many modules on the Puppet Forge to accomplish this. Personally, I use this one.

Craig Watson
  • 9,575
  • 3
  • 32
  • 47
  • Once My users are created on the first run , I want to disable that manifests otherwise the user's password will reset everytime when puppet agent pols the master . How this can be achieved – Zama Ques Mar 25 '16 at 14:08