DevOps 101 - Automating User Creation

DevOps 101 - Automating User Creation

Hey there, fellow tech enthusiasts! Today, I want to share a recent challenge I tackled in the HNG Internship program. If you haven't heard of it, you should check it out at HNG Internship. It's been an incredible learning experience so far!

So, picture this: You're a SysOps engineer, and your company just hired many new developers. Exciting times, right? Well, not so much when you realize you're the one who has to create all their user accounts. Manually. One by one. Yikes.

That's exactly the situation I found myself in. But instead of resigning myself to hours of tedious work, I decided to flex my DevOps muscles and automate the whole process. The result? A bash script that turned a mind-numbing task into a one-liner. Let me walk you through it.

The Challenge

The goal was simple: create a script that could read a text file with usernames and group assignments, then automatically create the users, set up their accounts, and assign them to the correct groups. Additionally, it needed to generate secure passwords for each user.

The Solution

I came up with a bash script that does all of this and more. Here's a breakdown of what it does:

  1. User and Group Creation: The script reads each line of the input file, creating a new user account and a personal group for each username it encounters. It also adds the user to any additional groups specified.

  2. Password Generation: For each new user, the script generates a random, secure password. No more "changeme123" passwords!

  3. Logging: Every action the script takes is logged. This was a lifesaver for troubleshooting and auditing.

  4. Error Handling: The script checks for existing users and groups, avoiding conflicts and keeping things running smoothly.

The Nitty-Gritty

Now, I won't bore you with every line of code (though if you're interested, the full script is in my GitHub repo). Instead, let's look at some of the more interesting bits:

while IFS=';' read -r username groups || [[ -n "$username" ]]; do
    # Remove leading/trailing whitespace
    username=$(echo "$username" | xargs)
    groups=$(echo "$groups" | xargs)
    # ... (user creation logic here)
done < "$1"

This loop is the heart of the script. It reads the input file line by line, splitting each line into a username and a list of groups. The xargs command is a neat trick to trim whitespace.

bashCopypassword=$(openssl rand -base64 12)
echo "$username:$password" | chpasswd
echo "$username,$password" >> $PASSWORD_FILE

Here's where the password magic happens. We generate a random password, set it for the user, and store it securely

Example Usage:

To better illustrate how this script works, let's walk through a practical example.

  1. Create an Input File: First, create a text file named new_users.txt with the following content:
Copyjohn; developers,marketing
sarah; developers,design
mike; marketing
lisa; developers,design,marketing

This file contains four users with their respective group assignments.

  1. Run the Script: Execute the script with the following command:
sudo ./create_users.sh new_users.txt
  1. What Happens: The script will process each line of new_users.txt:
  • For John: It will create a new user 'john', assign him to a personal group 'john', and add him to the 'developers' and 'marketing' groups.

  • For Sarah: A new user sarah will be created, assigned to a personal group 'sarah', and added to the 'developers' and 'design' groups.

  • For Mike: The script will create a new user 'mike', assign him to a personal group 'mike', and add him to the 'marketing' group.

  • For Lisa: A new user 'lisa' will be created, assigned to a personal group 'lisa', and added to the 'developers', 'design', and 'marketing' groups.

  1. Adding an Existing User to New Groups: If you run the script with a modified new_users.txt:
Copyjohn; developers,marketing,finance
alice; hr,finance

The script will:

  • Recognize that John already exists and add him to the 'finance' group.

  • Create a new user Alice and add her to the 'hr' and 'finance' groups.

This project was just one of many exciting challenges I've faced during the HNG Internship. If you're looking to level up your skills and work on real-world problems, I can't recommend it enough. Check out the HNG Premium program.