How to bulk add users (teachers & students) to Open edX

Hello,
openedx plateforme was recently installed in my school with tutor method,
the next step is to bulk add teachers and students to the plateforme, how to do it?
in the page http://myopenedx.mydomain/admin there is no way to do it, only by creating users manualy one by one and it’s not possible because of the huge number of users( thousends) .
can you give me the steps to follow ?
thank you

there’s a few ways to do it, you could use the API if you’re familiar with using API access.
You can also get users to self register and then you go edit users staff/superuser status.

The way I would do it (I’m not implying that this is the best way to do it, just a method I came up with) is with a for loop to loop through a list of users to add and have the loop run this for each user:
tutor local createuser --staff --superuser <name> <email> -p <password>
--staff will be for teachers and --superuser would be a super admin

So in a CSV file you can have a list of: username,email,password,staff,superuser
Example: users.csv

username,email,password,staff,superuser
testuser1,test1@domain.tld,password123,1,0 (staff user)
testuser2,test2@domain.tld,password123,0,1 (super user)
testuser3,test3@domain.tld,password123,1,1 (staff + super user)
testuser4,test4@domain.tld,password123,0,0 (student)
*this line should be left as blank (empty newline)*

This script below (BASH for Linux, if you’re using another backend then none of this will really apply to you, but the concept can be implemented in other scripting languages) will iterate through each user and run the command tutor local do createuser --staff testuser1 test1@domain.tld -p password123
Save this script as bulkaddusers.sh and run chmod +x bulkaddusers.sh to set it as executable, save your CSV file in the same location and run ./bulkaddusers.sh to start the script

#!/bin/bash

# Assuming your CSV file is named users.csv
CSV_FILE="users.csv"

# Read the CSV file line by line into an array
mapfile -t lines < "$CSV_FILE"

# Iterate over each line
for line in "${lines[@]:1}"; do
    # Split the line into fields
    IFS=',' read -r username email password staff superuser <<< "$line"

    # Trim leading and trailing whitespace from each field
    username=$(echo "$username" | tr -d '[:space:]')
    email=$(echo "$email" | tr -d '[:space:]')
    password=$(echo "$password" | tr -d '[:space:]')
    staff=$(echo "$staff" | tr -d '[:space:]')
    superuser=$(echo "$superuser" | tr -d '[:space:]')

    # Build the command string based on staff and superuser values
    command="tutor local do createuser"
    if [ "$staff" -eq 1 ]; then
        command+=" --staff"
    fi
    if [ "$superuser" -eq 1 ]; then
        command+=" --superuser"
    fi
    command+=" $username $email -p $password"

    # Print debug information
    echo "Executing command: $command"

    # Execute the command and capture the output
    output=$(eval "$command" 2>&1)

    # Check the exit code
    exit_code=$?
    if [ $exit_code -eq 0 ]; then
        echo "Command finished successfully for $username"
    else
        echo "Error executing command for $username. Exit code: $exit_code"
        echo "Output: $output"
    fi

    # Sleep for a short duration to allow the previous command to finish
    sleep 2
done

# Exit the script
exit 0

here’s the output when I run the script:

tutor@tutor-beta:~$ ./bulkaddusers.sh
Executing command: tutor local do createuser --staff testuser1 test1@domain.tld -p password123
Command finished successfully for testuser1
Executing command: tutor local do createuser --superuser testuser2 test2@domain.tld -p password123
Command finished successfully for testuser2
Executing command: tutor local do createuser --staff --superuser testuser3 test3@domain.tld -p password123
Command finished successfully for testuser3
Executing command: tutor local do createuser testuser4 test4@domain.tld -p password123
Command finished successfully for testuser4


Hope this is useful to you :slight_smile:
Sample files attached for your convenience:
bulkaddusers.zip (947 Bytes)

3 Likes

thanks a lot joel.edwards,
it worked for me, your reply is just very helpfull;
thank you.

1 Like

Fantastic, glad you found it useful! :slight_smile:

how to make with for windows?

Unfortunately I’m not at all familiar with running Tutor on Windows, can you tell me anything more about how your instance is set up? As far as I know it requires a unix backend. If you used WSL2 (Windows Subsystem for Linux) then chances are you already use a BASH interface so my old script should work there too.

That’s right, but the infrastructure in my company does not allow me to use WSL2, so I can’t answer exactly how tutor works for me through Hyper-V
That’s why I have to look for different ways to work with windows
I tried to use ai to help me rewrite linux code for windows and this is what I’v got:

# Assuming your CSV file is named users.csv
$CSV_FILE = "users.csv" 
# Read the CSV file line by line into an array 
$lines = Get-Content -Path $CSV_FILE 
# Split the line into fields
foreach ($line in $lines[1..$lines.Length]) { 
# Trim leading and trailing whitespace from each field
$fields = $line -split "," 
$username = $fields[0].Trim() 
$email = $fields[1].Trim() 
$password = $fields[2].Trim() 
$staff = $fields[3].Trim() 
$superuser = $fields[4].Trim() 
# Build the command string based on staff and superuser values 
$command = "tutor local do createuser" 
if ($staff -eq 1) { 
	$command += " --staff" 
} 
if ($superuser -eq 1) { 
	$command += " --superuser" 
} 
$command += " $username $email -p $password" 
# Print debug information
Write-Output "Executing command: $command" 
# Execute the command and capture the output
$output = Invoke-Expression $command 2>&1 
# Check the exit code
if ($LASTEXITCODE -eq 0) { 
	Write-Output "Command finished successfully for $username" 
} else { 
	Write-Output "Error executing command for $username. Exit code: $LASTEXITCODE" 
	Write-Output "Output: $output" } 
	# Sleep for a short duration to allow the previous command to finish 
	Start-Sleep -Seconds 15 
} 
# Exit the script 
exit 0

Oh I see you’re using HyperV as a virtualisation platform, are you also using Windows as the guest OS in your VM?

As far as I know Windows is not an officially supported backend for Tutor, so you should probably consider running a Linux system in your HyperV container. This is mentioned in the installation requirements:
Supported OS: Tutor runs on any 64-bit, UNIX-based OS. It was also reported to work on Windows (with WSL 2).

I do similar with my instance, I have my main Tutor instance running in a Debian virtual machine on my ESXi host, then I have a beta-testing instance on my Win11 laptop using HyperV.

Debian for me is just a matter of preference, many other distros (like Ubuntu, Mint, etc, there’s literally hundreds) are often based off Debian anyway, and I use headless (all CLI no GUI) systems anyway so don’t need fancy skins/desktops on my Linux.

Actually, I think, that this is a topic for a one different issue. I’ll create it later