我想通过ansible创建jenkins凭证

civ*_*ian 4 jenkins ansible

我正在使用ansible组建一个开发人员机器.在那台机器上我正在安装詹金斯.

我用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"
Run Code Online (Sandbox Code Playgroud)

并安装插件,通过cli等.

但现在我错过了工作的ssh证书; 我只想要一个用户"jenkins"的ssh凭证,并使用"来自Jenkins master~/.ssh".

这种类型的凭证是我正在谈论的:

在此输入图像描述

也许是一个时髦的脚本,但我没有找到很多关于它的信息.谢谢您的帮助.

小智 9

您可以在jenkins运行的机器上从命令行使用jenkins客户端:

java -jar jenkins-cli.jar -s http://localhost:8080/ groovy create-credential.groovy
Run Code Online (Sandbox Code Playgroud)

使用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')
Run Code Online (Sandbox Code Playgroud)

这将为用户'pinky'添加密码'narf'的全局凭证


小智 1

Jenkins 凭证插件不允许使用 API 创建凭证(https://issues.jenkins-ci.org/browse/JENKINS-28407)。

一个可行的解决方案是使用您喜欢的浏览器和 JMeter 代理或 Selenium IDE 记录凭证创建。并使用 JMeter CLI 重放它或将 Selenium 记录的测试保存为 groovy 脚本。

您还可以查看https://github.com/jenkinsci/credentials-plugin/pull/33