gitlab批量添加用戶的方法

gitlab批量添加用戶的方法
要在gitlab中添加新的用戶,可以用管理員賬戶在登錄gitlab後進入admin area頁面,進行添加用戶的操作。但是這樣只能手工進行操作,一次只能添加一個用戶。如果需要一次添加大量用戶,不僅操作速度慢,還很容易出錯。
要能夠​批量添加用戶,一個比較好的解決方案是使用gitlab提供的api接口。gitlab API的使用可以參考的gitlab的幫助文檔,gitlab提供了很多語言的api 客戶端。這裏只介紹最簡單的http方法。
所有的api請求都要進行身份驗證,需要在url或者header中傳遞private_token參數。用戶的private_token可以在profile settings頁面的account settings中找到。使用GET或者curl都可以發送api請求。

gitlab批量添加用戶的方法

gitlab批量添加用戶的方法
#獲取自己賬號的用戶信息
curl -X GET http://1.1.1.11/api/v3/user?private_token=uMRTjtFk8fqueB1XfJVH
curl http://1.1.1.11/api/v3/user?private_token=uMRTjtFk8fqueB1XfJVH (當前tokenr的信息既管理員的token,只顯示管理員的個人信息)

​#獲取所有用戶的信息
curl -X GET http://1.1.1.11/api/v3/users?private_token=uMRTjtFk8fqueB1XfJVH
curl http://1.1.1.11/api/v3/users?private_token=uMRTjtFk8fqueB1XfJVH(當前用管理員的token,顯示所有用戶的信息

#添加用戶賬戶(user2)
curl -d "projects_limit=8&can_create_group=false&password=nihao123&[email protected]&username=user2&name=user2&private_token=uMRTjtFk8fqueB1XfJVH" "http://1.1.1.11/api/v3/users"

#查詢一個用戶的信息(root)
curl "http://1.1.1.11/api/v3/users?search=root&private_token=uMRTjtFk8fqueB1XfJVH"
​#刪除一個用戶,查詢到的用戶ID信息進行刪除
新建一個用戶(user3),用來測試刪除命令
curl -d "projects_limit=8&can_create_group=true&password=nihao123&[email protected]&username=user3&name=user3&private_token=uMRTjtFk8fqueB1XfJVH" "http://1.1.1.11/api/v3/users"

記住此用戶的ID 後面的刪除測試要用到

刪除用戶user3(兩條命令一樣)
curl -X DELETE --header "PRIVATE-TOKEN:uMRTjtFk8fqueB1XfJVH" "http://1.1.1.11/api/v3/users/7"​

curl -X DELETE http://1.1.1.11/api/v3/users/7?private_token=uMRTjtFk8fqueB1XfJVH

以上就是以http方式使用gitlab api的簡單命令,可以在這些命令的基礎上製作shell腳本或python腳本,實現批量添加用戶。如果要使用api進行更復雜的操作,可以使用gitlab提供的客戶端,如:python-gitlab、pyapi-gitlab、libsaas-gitlab。
用一個txt 文件導入批量用戶的腳本:
#!/bin/bash
userinfo="userinfo.txt"
while read line
do
name=echo $line | awk '{print $1}'
username=echo $line | awk '{print $2}'
userpass=echo $line | awk '{print $3}'
mail=echo $line | awk '{print $4}'
curl -d "projects_limit=1&can_create_group=false&password=$userpass&email=$mail&username=$username&name=$name&private_token=uMRTjtFk8fqueB1XfJVH" "http://1.1.1.11/api/v3/users"
done <$userinfo

##########userinfo.txt################
user4 user4 userpass [email protected]
user5 user5 userpass [email protected]
user6 user6 userpass [email protected]
user7 user7 userpass [email protected]
user8 user8 userpass [email protected]
user9 user9 userpass [email protected]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章