This will be another example for #Opscode #Chef. We will just extend the cookbook in the previous post to add a new recipe; we will add some extra lines to attributes file and write new simple recipe.
Let's start with attributes file. To remove a user, required attribute is its name. Let's provide it:
default['ruser']['name'] = "username"
We will add an attribute which will specify whether to remove or keep home directory of removed user. Perhaps, it will be kept in the archive. Therefore, as default I set it false.
default['ruser']['removehome'] = false
As final part, let's write our short recipe to perform this action. Create a new file with a related name 'removeuser.rb'. We will write the codes in this file. Our first lines will remove a user in a system:
user node['ruser']['name'] do
action :remove
end
Let's pass to delete home directory part. The code is in an if clause to control this action as we mentioned above:
if node['ruser']['removehome']
directory "/home/"+node['ruser']['name'] do
recursive true
action :delete
end
end
I assume that home directory of a user is at /home/username directory. If this is not suitable for your case, it will not be hard for you to change directory name.
Let's start with attributes file. To remove a user, required attribute is its name. Let's provide it:
default['ruser']['name'] = "username"
We will add an attribute which will specify whether to remove or keep home directory of removed user. Perhaps, it will be kept in the archive. Therefore, as default I set it false.
default['ruser']['removehome'] = false
As final part, let's write our short recipe to perform this action. Create a new file with a related name 'removeuser.rb'. We will write the codes in this file. Our first lines will remove a user in a system:
user node['ruser']['name'] do
action :remove
end
Let's pass to delete home directory part. The code is in an if clause to control this action as we mentioned above:
if node['ruser']['removehome']
directory "/home/"+node['ruser']['name'] do
recursive true
action :delete
end
end
I assume that home directory of a user is at /home/username directory. If this is not suitable for your case, it will not be hard for you to change directory name.
Comments
Post a Comment