Error Lookup Private Key in Post Request

12 views
Skip to first unread message

Marcos Roberto de Abreu Silvestrini

unread,
Jul 21, 2020, 12:04:57 PM7/21/20
to Ansible Development
I have an error in the post with the uri module.
I need to create a credential for my Vagrant VM on Jenkins via api.
In my tests, I realized that the error is in passing the value of the private key, in this line:

privateKey": "{{ lookup('file', '../.vagrant/machines/protheus/virtualbox/private_key')}}"

If I pass the value of the private key manually, in just one line the credential is created without errors.

My task:

- name: Get Private Key Value
  debug: msg="Private Key Value is {{ lookup('file', '../.vagrant/machines/protheus/virtualbox/private_key')}}"

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

- name: Create a Jenkins Credential
  uri:
    method: POST
    user: "{{ master_username }}"
    password: "{{ master_password }}"
    force_basic_auth: yes
    #body_format: json
    #body_format: form-urlencoded
    url: "{{ master_url }}/credentials/store/system/domain/_/createCredentials"
    headers:
      Jenkins-Crumb: "{{ crumb.content.split(':')[1] }}"
      Cookie: "{{ crumb.set_cookie }}"
    body: |
      json={
        "credentials": {
          "scope": "GLOBAL",
          "id": "{{ slave_linux_jenkins_cred_id }}",
          "username": "{{ slave_linux_jenkins_username }}",
          "privateKeySource": {
            "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey$DirectEntryPrivateKeySource",
            "privateKey": "{{ lookup('file', '../.vagrant/machines/protheus/virtualbox/private_key')}}"
            },
          "description": "Node app-protheus",
          "stapler-class": "com.cloudbees.jenkins.plugins.sshcredentials.impl.BasicSSHUserPrivateKey"
        }
      }
    #status_code: 302
    validate_certs: no
    follow_redirects: yes
  #ignore_errors: true


Error:
TASK [jenkins : Get Private Key Value] ******************************************************************************************************************************************
ok: [192.168.0.133] => {
    "msg": "Private Key Value is -----BEGIN RSA PRIVATE KEY-----\nMIIEpQIBAAKCAQEA1HDltyx+rUOngUQEggu2DRj5luG929fBrFhyUTzNmhTfxuO+\nsZbkqvL5OWb5K3cMsBYH2U3LPeN8ag/ERgI3ktEH2L8aOqdCJROYP74zXJpa68Wx\nXPmfOuEsxTbkYUe65Ga7q8mwRMWLFL8mRS85DijbSkpHrrYaMRPKW/zr6P7ciZL6\nXCcMgReNzZiRCKRFWAE3HT6Pd7+BSBluJIXi52Ey2L+Wx38OdvbrStSJcp83YZc8\nJv9uVEVCeeW3NAN0yF8TL6Gbv1o7zIl1dMhLaNIppGU1wGoLZ2jGHHSKj3/MimmpwtWPAhg3V4dM\nblbo8+3V3Z9AsycAiSD7JyECgYEA2CFJla8GeRMGEujruuIVOF+wIdbI5zssic/s\nei/AFpdE71jQNxhCxgD/hjShcxFp18e8N13LCYjZIwUeUaAmH30Vs1Y9YWCFcIiC\nj/Z9rpHem8/4u6EiLfnF2WOY05XHEtRwpFBR+pzSR8F3rQMF4BhOF0zNP6XWwiay\nerwkCqkCgYEApL/F7ewsl+9MoA9Ys6fL6D2DpfKK8Bl3gWVs/wll9TzF2NfGszEx\nnc7axyEFd99qIYb2wcoB4+yjlEQKGxDzMCAjMjAvpQPJ+GCYqGQpFQPNvv/F49ET\nUHni4DBp3RnIWfsOzrqLpRMeGsB1+pnphhytbFdNirLjgXj6MFMWZB4=\n-----END RSA PRIVATE KEY-----"
}

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

TASK [jenkins : Create a Jenkins Credential] ************************************************************************************************************************************
[WARNING]: The value True (type bool) in a string field was converted to 'True' (type string). If this does not look like what you expect, quote the entire value to ensure it
does not change.
fatal: [192.168.0.133]: 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 400 Nothing is submitted</title>\n</head>\n<body><h2>HTTP ERROR 400 Nothing is submitted</h2>\n<table>\n<tr><th>URI:</th><td>/credentials/store/system/domain/_/createCredentials</td></tr>\n<tr><th>STATUS:</th><td>400</td></tr>\n<tr><th>MESSAGE:</th><td>Nothing is submitted</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": "527", "content_type": "text/html;charset=iso-8859-1", "elapsed": 0, "msg": "Status code was 400 and not [200]: HTTP Error 400: Bad Request", "redirected": false, "server": "Jetty(9.4.27.v20200227)", "status": 400, "url": "http://192.168.0.120:9080/credentials/store/system/domain/_/createCredentials", "x_content_type_options": "nosniff"}

PLAY RECAP **********************************************************************************************************************************************************************
192.168.0.133              : ok=6    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0



Does anyone know what could be wrong with my task?

Reply all
Reply to author
Forward
0 new messages