Forum: CFEngine Help
Subject: Editing /etc/shadow file using variables
Author: josephvj
Link to topic: https://cfengine.com/forum/read.php?3,24723,24723#msg-24723

Hi,
I'm out of scope in editing /etc/shadow file using variables. This is the 
script that I've done.The user is being created but not able to set the 
password directly to the shadow file. Please help.

body common control
{
bundlesequence => { "manage_users" };
inputs => { "/var/cfengine/inputs/cfengine_stdlib.cf" };
}

bundle agent create_users(user)
{
vars:
        "index" slist => getindices("$(user)");
classes:
        "add_user" not => userexists("$(index)");

commands:
        linux.add_user::
                "/usr/sbin/useradd $($(user)[$(index)]) -d $($(user)[$(index)]) 
-s $($(user)[$(index)]) -c '$($(user)[$(index)])' $(index)"
                ifvarclass => "add_user";

files:
        linux.add_user::
                "/etc/shadow"
                edit_line => set_user_field("$(index)", "2", 
"$($(user)[$(index)])");

reports:
        add_user::
                "User has been created and password is set";
        !add_user::
                "User not created / User exists";
#       verbose::
#               "Created user $(index)"
#               ifvarclass => "add_user";
}
#--------------------------------------------------------------------------------------------------#
bundle edit_line set_user_field(user,field,val)
{
field_edits:
        "$(user):.*"
        edit_field => line(":","$(field)","$(val)","set");
}

body edit_field line(split,col,newval,method)
{
        field_separator => "$(split)";
        select_field => "$(col)";
        value_separator => ",";
        field_value => "$(newval)";
        field_operation => "$(method)";
        extend_fields => "true";
}

bundle agent manage_users
{
vars:
  # Users to create
  "users"  string => "Test User";
  "users"       string => "1000";
  "users"       string => "100";
  "users"      string => "/home/testuser";
  "users"     string => "/bin/bash";
  "users"     string => "-m";
  "users"  string => "EnCrYpTeD_PAssWord_hErE";

methods:
  "users"   usebundle => create_users("manage_users.users");
}

_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to