我正在使用 ansible 组装一台开发机器。在那台机器上,我正在安装 Jenkins 。
我用 ansible 为 jenkins 创建了工作:
- shell: "java -jar {{ jenkins.cli_jar }} -s {{ jenkins.server }} create-job \
{{ item.name }} < {{ jenkins.jobs_dir }}/{{ item.xml_file }}"
with_items: "jenkins.jobs"
并通过 cli 等安装插件。
但是现在我缺少工作的 ssh 凭据;我只想要一个带有用户“jenkins”的 ssh 凭证,并且使用“来自 Jenkins 大师 ~/.ssh”。
这种类型的凭据是我正在谈论的凭据:
也许是一个 groovy 脚本,但我还没有找到很多关于它的信息。谢谢您的帮助。
最佳答案
您可以在 jenkins 运行的机器上从命令行使用 jenkins 客户端,如下所示:
java -jar jenkins-cli.jar -s http://localhost:8080/ groovy create-credential.groovy
使用 create-credential.groovy:
import jenkins.model.*
import com.cloudbees.plugins.credentials.CredentialsScope
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl
def addPassword = { username, new_password ->
def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class,
Jenkins.instance
)
def c = creds.findResult { it.username == username ? it : null }
if ( c ) {
println "found credential ${c.id} for username ${c.username}"
} else {
def credentials_store = Jenkins.instance.getExtensionList(
'com.cloudbees.plugins.credentials.SystemCredentialsProvider'
)[0].getStore()
def scope = CredentialsScope.GLOBAL
def description = ""
def result = credentials_store.addCredentials(
com.cloudbees.plugins.credentials.domains.Domain.global(),
new UsernamePasswordCredentialsImpl(scope, null, description, username, new_password)
)
if (result) {
println "credential added for ${username}"
} else {
println "failed to add credential for ${username}"
}
}
}
addPassword('pinky', 'narf')
这将为用户“pinky”添加全局凭证,密码为“narf”
关于jenkins - 我想通过 ansible 创建 jenkins 凭据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35025829/