jenkins - 我想通过 ansible 创建 jenkins 凭据

标签 jenkins ansible

我正在使用 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”。

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

enter image description here

也许是一个 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/

相关文章:

Ansible:是否有一个类似于 Python intspan 的过滤器可以转换列表内的整数范围?

ansible - 为什么 Ansible 忽略 ansible_become_password 变量?

jenkins - 如何与 Jenkins 并行运行相同的工作多次?

xml - 如何使用 XML API 获取 Jenkins 作业列表

android - 使用$ VARIABLE作为Jenkins中Gradle插件的构建任务

package - Ansible Galaxy 角色版本的定义是什么?

python - 无法将 ansible yml 文件中的 dict 键值获取到我的 after.yml 文件中

windows - 使用 powershell 更改 IIS SSL 设置

linux - Jenkins maven 构建在 linux 机器中失败

jenkins - Jenkins-X 和 Jenkins Helm 图表是否相同?