gpt4 book ai didi

ansible - 如何使用ansible playbook在 "nexus local user and role"中创建 "sonatype nexus"

转载 作者:行者123 更新时间:2023-12-02 18:11:51 24 4
gpt4 key购买 nike

我正在开发一个自动化项目,我需要使用 ansible playbook 安装 sonatype nexus,并创建一个 nexus 用户和角色。

到目前为止,我已经使用 playbook 安装了 sonatype nexus,并且我还可以登录该应用程序并创建一个“nexus 本地用户”。但是,我想使用 ansible playbook 自动创建角色和用户(在关系中)。

非常感谢您的帮助。

最佳答案

您可以查看 ansible-thoteam.nexus3-oss 角色 on galaxyon github它安装 nexus3 并负责其配置。为了完全透明,我是这个开源角色的当前维护者。

您会想看看 following groovy script to create users (作为完整示例粘贴在下面):

import groovy.json.JsonOutput
import groovy.json.JsonSlurper
import groovy.transform.Field
import org.sonatype.nexus.security.role.RoleIdentifier
import org.sonatype.nexus.security.user.InvalidCredentialsException
import org.sonatype.nexus.security.user.UserManager
import org.sonatype.nexus.security.user.UserNotFoundException
import org.sonatype.nexus.security.user.User

List<Map<String, String>> actionDetails = []
@Field Map scriptResults = [changed: false, error: false]
scriptResults.put('action_details', actionDetails)
authManager = security.securitySystem.getAuthorizationManager(UserManager.DEFAULT_SOURCE)

def updateUser(userDef, currentResult) {
User user = security.securitySystem.getUser(userDef.username)

user.setFirstName(userDef.first_name)
user.setLastName(userDef.last_name)
user.setEmailAddress(userDef.email)

if (user != security.securitySystem.getUser(userDef.username)) {
security.securitySystem.updateUser(user)
currentResult.put('status', 'updated')
scriptResults['changed'] = true
}

Set<RoleIdentifier> existingRoles = user.getRoles()
Set<RoleIdentifier> definedRoles = []
userDef.roles.each { roleDef ->
RoleIdentifier role = new RoleIdentifier("default", authManager.getRole(roleDef).roleId);
definedRoles.add(role)
}
if (! existingRoles.equals(definedRoles)) {
security.securitySystem.setUsersRoles(user.getUserId(), "default", definedRoles)
currentResult.put('status', 'updated')
scriptResults['changed'] = true
}

try {
security.securitySystem.changePassword(userDef.username, userDef.password, userDef.password)
} catch (InvalidCredentialsException ignored) {
security.securitySystem.changePassword(userDef.username, userDef.password)
currentResult.put('status', 'updated')
scriptResults['changed'] = true
}
log.info("Updated user {}", userDef.username)
}

def addUser(userDef, currentResult) {
try {
security.addUser(userDef.username, userDef.first_name, userDef.last_name, userDef.email, true, userDef.password, userDef.roles)
currentResult.put('status', 'updated')
scriptResults['changed'] = true
log.info("Created user {}", userDef.username)
} catch (Exception e) {
currentResult.put('status', 'error')
currentResult.put('error_msg', e.toString())
scriptResults['error'] = true
}
}

def deleteUser(userDef, currentResult) {
try {
security.securitySystem.deleteUser(userDef.username, UserManager.DEFAULT_SOURCE)
log.info("Deleted user {}", userDef.username)
currentResult.put('status', 'deleted')
scriptResults['changed'] = true
} catch (UserNotFoundException ignored) {
log.info("Delete user: user {} does not exist", userDef.username)
} catch (Exception e) {
currentResult.put('status', 'error')
currentResult.put('error_msg', e.toString())
scriptResults['error'] = true
}
}

/* Main */

parsed_args = new JsonSlurper().parseText(args)

parsed_args.each { userDef ->

state = userDef.get('state', 'present')

Map<String, String> currentResult = [username: userDef.username, state: state]
currentResult.put('status', 'no change')

if (state == 'absent') {
deleteUser(userDef, currentResult)
} else {
try {
updateUser(userDef, currentResult)
} catch (UserNotFoundException ignored) {
addUser(userDef, currentResult)
} catch (Exception e) {
currentResult.put('status', 'error')
currentResult.put('error_msg', e.toString())
scriptResults['error'] = true
}
}

scriptResults['action_details'].add(currentResult)
}

return JsonOutput.toJson(scriptResults)

该脚本期望收到用户列表。您可以引用README.md以及default vars有关预期数据结构的详细信息,如下所示:

nexus_local_users: 
- username: my_user
first_name: my
last_name: user
email: my@user.com
password: "s3cr3t"
roles:
- developers

我不会再次粘贴完整的示例,但还有一个groovy script和一个data structure对于角色

您可以查看该角色的其余部分以获取更多信息,更具体地说是 tasks to declare the script in nexus并发送至call it with its parameters

即使您不想使用完整的角色,我认为这仍然是一个很好的开始示例,可以根据您的意愿开发自己的解决方案。

希望对你有帮助。

关于ansible - 如何使用ansible playbook在 "nexus local user and role"中创建 "sonatype nexus",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57967608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com