【问题标题】:Error Create a Jenkins Credential with Ansible使用 Ansible 创建 Jenkins 凭据时出错
【发布时间】:2020-10-14 02:49:25
【问题描述】:

我在 jenkins 中使用凭证插件在正文中使用私钥创建用户的角色存在问题。 我可以得到面包屑,但是当我请求创建凭证时,我得到了无效的面包屑错误。

我的角色:

- name: Get Jenkins Crumb
  uri:
    url: "{{ master_url }}/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,\":\",//crumb)"
    user: '{{ master_username }}'
    password: "my_token"
    method: GET
    return_content: yes
    force_basic_auth: yes
    body_format: form-urlencoded
  register: crumb

- debug:
    msg: "{{ crumb.content.split(':')[1]}}"

- name: Add credential to Node
  uri:
    url: "{{ master_url }}/credentials/store/system/domain/_/createCredentials"
    user: "{{ master_username }}"
    password: "my_token"
    method: POST
    status_code: 302
    body_format: form-urlencoded
    headers:
      Jenkins-Crumb: "{{ crumb.content.split(':')[1]}}"
      Cookie: "{{ crumb.set_cookie }}"
    body: |
      json={
        "": "0",
        "credentials": {
          "scope": "GLOBAL",
          "id": "jenkins_linux_slave1_auth",
          "username": "jenkins",
          "password": "jenkins",
          "privateKeySource": {
            "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource",
            "privateKey": "{{ private_key_file['content'] | b64decode | to_json}}"
          },
          "description": "Jenkins Linux Slave1 Authentication",
          "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"
        }
      }

输出\错误:

TASK [jenkins : Get Jenkins Crumb] *********************************************
ok: [protheus]

TASK [jenkins : debug] *********************************************************
ok: [protheus] => {
    "msg": "f87df3f83a7ecfb5f5b2e7af0c0beb218dd239ec460fba832ff36f0a90e42287"
}

TASK [jenkins : Add credential to Node] ****************************************
fatal: [protheus]: FAILED! => {"cache_control": "must-revalidate,no-cache,no-store", "changed": false, "connection": "close", "content": "<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html;charset=utf-8\"/>\n<title>Error 403 No valid crumb was included in the request</title>\n</head>\n<body><h2>HTTP ERROR 403 No valid crumb was included in the request</h2>\n<table>\n<tr><th>URI:</th><td>/credentials/store/system/domain/_/createCredentials</td></tr>\n<tr><th>STATUS:</th><td>403</td></tr>\n<tr><th>MESSAGE:</th><td>No valid crumb was included in the request</td></tr>\n<tr><th>SERVLET:</th><td>Stapler</td></tr>\n</table>\n<hr><a href=\"http://eclipse.org/jetty\">Powered by Jetty:// 9.4.27.v20200227</a><hr/>\n\n</body>\n</html>\n", "content_length": "593", "content_type": "text/html;charset=iso-8859-1", "elapsed": 0, "msg": "Status code was 403 and not [302]: HTTP Error 403: Forbidden", "redirected": false, "server": "Jetty(9.4.27.v20200227)", "status": 403, "url": "http://192.168.0.120:9080/credentials/store/system/domain/_/createCredentials", "x_content_type_options": "nosniff"}

PLAY RECAP *********************************************************************
protheus                   : ok=9    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Ansible 未能成功完成。任何错误输出应该是 上面可见。请修正这些错误,然后重试。

有谁知道怎么回事?

谢谢!

【问题讨论】:

    标签: json jenkins ansible roles credentials


    【解决方案1】:

    由于您没有调用 API,因此您可以删除 Jenkins-Crumb 标头并使用用户名和用户名令牌。 同时添加选项 force_basic_auth: true

    所以你的任务是这样的:

    - name: Add credential to Node
      uri:
        url: "{{ master_url }}/credentials/store/system/domain/_/createCredentials"
        user: "{{ master_username }}"
        password: "my_token"
        method: POST
        status_code: 302
        body_format: form-urlencoded
        force_basic_auth: true
        body: |
          json={
          "": "0",
          "credentials": {
          "scope": "GLOBAL",
          "id": "jenkins_linux_slave1_auth",
          "username": "jenkins",
          "password": "jenkins",
          "privateKeySource": {
            "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource",
            "privateKey": "{{ private_key_file['content'] | b64decode | to_json}}"
          },
          "description": "Jenkins Linux Slave1 Authentication",
          "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"
          }
        }
    

    【讨论】:

    • 感谢您的回复@sadok-f 将任务修改为您建议的任务后,我收到以下错误:“msg”:“状态代码为 500 而不是 [302]:HTTP 错误500:服务器错误”,“重定向”:false,“referrer_policy”:“same-origin”,“server”:“Jetty (9.4.27. v20200227)”,“set_cookie”:“JSESSIONID.af9bf050 = node0uhw0itxug8m42mnhpspodrxe3.node0; Path = /; HttpOnly","status":500,"url":"192.168.0.120:9080/credentials/store/domain/_/createCredentials","
    • 问题现在似乎出在我传递私钥的方式上,在这一行:“privateKey”:“{{private_key_file ['content'] | b64decode | to_json}}”测试,我删除了变量并创建了凭据,但没有密钥。我的测试是这样的,角色执行成功,但是没有私钥:“privateKey”:“foo”如何传递私钥来创建凭证?
    • 能否尝试上传文件并使用源作为私钥文件"privateKeyFile": "'{{ path_private_key_file }}.pem'",
    • 使用 .pem,角色完成 ok,但 jenkins 节点未启动。错误:错误:服务器拒绝了 vagrant (credentialId:app_protheus/method:publickey) [07/01/20 22:29:05] [SSH] 身份验证失败的 1 个私钥。
    • {{ path_private_key_file }}.pem 是否存在?
    猜你喜欢
    • 2012-10-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 2016-12-07
    • 1970-01-01
    相关资源
    最近更新 更多