Hi There,
Thanks for the additional information it actually helps me understand what you are trying to achieve a bit better.
As per the types documentation, this line is quite important to understand: "Classes are good for modelling singleton aspects of a system, but to model repeatable chunks of configuration — like a Git repository or an Apache vhost — you should use defined resource types."
In your case, its ideal because you are likely to want to manage a few admin accounts I am guessing.
In addition to this, you likely need to understand resource dependency, there is a good section on resource ordering here:
Based on your logic, I am assuming you wish to have the following functionality
* When an admin account is present
- Have a user created
- The new user has a home directory that needs to have certain permissions and ownership enforced
- We also create a dir called 'somedir' under the users home directory
* When an admin account is marked as absent
- The directories are removed
- The user account is removed
(In that order)
Here is a defined type that should help you out a little with how this can function:
---------------SNIP------------------
# Defined type called setup_admin_account that allows us to wrap admin tasks
# into a logical grouping
define setup_admin_account ($username , $ensure=present, $homedirpath = '/home') {
case $ensure {
present : {
user { $username :
ensure => present,
home => "${homedirpath}/${username}",
shell => '/bin/bash',
}
file { "${homedirpath}/${username}" :
ensure => directory,
owner => $username,
group => $username,
mode => 0700,
require => User[$username],
}
file { "${homedirpath}/${username}/somedir/" :
ensure => directory,
owner => $username,
group => $username,
mode => 0700,
require => File["${homedirpath}/${username}"],
}
}
absent : {
user { $username :
ensure => absent,
require => File["${homedirpath}/${username}"],
}
file { "${homedirpath}/${username}" :
ensure => absent,
force => true,
require => File["${homedirpath}/${username}/somedir/"],
}
file { "${homedirpath}/${username}/somedir/":
ensure => absent,
force => true,
}
}
}
}
#
# Using the type to manage a user called test user
# Change present to absent as required.
# Note we can have as many of these user definitions as required.
#
setup_admin_account { 'testuser' :
username => 'testuser',
ensure => present,
}
---------------SNIP------------------
Example output:
When we have:
setup_admin_account { 'testuser' :
username => 'testuser',
ensure => present,
}
# puppet apply setup_admin_user.pp
notice: /Stage[main]//Setup_admin_account[testuser]/User[testuser]/ensure: created
notice: /Stage[main]//Setup_admin_account[testuser]/File[/home/testuser]/ensure: created
notice: /Stage[main]//Setup_admin_account[testuser]/File[/home/testuser/somedir/]/ensure: created
notice: Finished catalog run in 0.35 seconds
When we have:
setup_admin_account { 'testuser' :
username => 'testuser',
ensure => absent,
}
# puppet apply setup_admin_user.pp
notice: /Stage[main]//Setup_admin_account[testuser]/File[/home/testuser/somedir/]/ensure: removed
notice: /Stage[main]//Setup_admin_account[testuser]/File[/home/testuser]/ensure: removed
notice: /Stage[main]//Setup_admin_account[testuser]/User[testuser]/ensure: removed
notice: Finished catalog run in 0.31 seconds
[root@lg2infra01 tmp]#
I hope this answers your question a little.
Cheers,
K
Extra note:
It is also worth noting that if you are doing destructive tasks, such as recursive directory removal that you validate you data.
Ie, check that the home directory your removing hasnt somehow been evaluated to / or /home due to bad variables, etc.