Trigger jenkins Job using Ansible uri module does not work

Viewed 1107

I'm able to trigger jenkins job remotely using the below two commands:

Step 1: get the crumb

curl -s -k 'https://user1:118f32aa48601c136d29b11f3dd0e107f5@portal.myshop.com:9043/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)'

Output:

Jenkins-Crumb:09e4712ad43932fd474594aa3e1216a47c99030647e23f3f570dbfbc5e34ac4e

Step 2: Invoke Jenkins URL build successfully using the below curl command in verbose mode and pass string parameter myname=Tom

curl -vvv -k -X POST https://user1:118f32aa48601c136d29b11f3dd0e107f5@portal.myshop.com:9043/job/job5/buildWithParameters?myname=Tom  -H "Jenkins-Crumb:09e4712ad43932fd474594aa3e1216a47c99030647e23f3f570dbfbc5e34ac4e"
* About to connect() to portal.myshop.com port 9043 (#0)
*   Trying 10.9.9.54...
* Connected to portal.myshop.com (10.9.9.54) port 9043 (#0)
* Initializing NSS with certpath: sql:/etc/pki/nssdb
* skipping SSL peer certificate verification
* SSL connection using TLS_DHE_RSA_WITH_AES_256_CBC_SHA256
* Server certificate:
*       subject: CN=portal.myshop.com,OU=Information Technology,O=myshop  Ltd,L=Mumbai,ST=MH,C=IN
*       start date: Jul 01 08:11:48 2020 GMT
*       expire date: Jun 29 08:11:48 2030 GMT
*       common name: portal.myshop.com
*       issuer: CN=portal.myshop.com,OU=Information Technology,O=myshop  Ltd,L=Mumbai,ST=MH,C=IN
* Server auth using Basic with user 'user1'
> POST /job/job5/buildWithParameters?myname=Tom HTTP/1.1
> Authorization: Basic MTg2MDEyOjExOGYzMmFhNDg2MDFjMTM2ZDI5YjExZjNkZDBlMTA3ZjU=
> User-Agent: curl/7.29.0
> Host: portal.myshop.com:9043
> Accept: */*
> Jenkins-Crumb:09e4712ad43932fd474594aa3e1216a47c99030647e23f3f570dbfbc5e34ac4e
>
< HTTP/1.1 201 Created
< Date: Fri, 18 Sep 2020 22:19:56 GMT
< X-Content-Type-Options: nosniff
< Location: https://portal.myshop.com:9043/queue/item/19453/
< Content-Length: 0
< Server: Jetty(NOTHING)
<
* Connection #0 to host portal.myshop.com left intact

Now, I'm trying the same using ansible's uri module but it fails with Status code was 403 and not [200]: HTTP Error 403: Forbidden and HTTP ERROR 403 No valid crumb was included in the request error.

Below is my playbook:

---
- name: "Play 1"
  hosts: localhost
  tasks:

    - name: Add host
      uri:
        url: "https://user1:118f32aa48601c136d29b11f3dd0e107f5@portal.myshop.com:9043/job/job5"
        validate_certs: false
        method: POST
        body_format: form-urlencoded
#        body_format: json
        body:
          - [ myname, "Tom" ]
        http_agent: "Jenkins-Crumb:09e4712ad43932fd474594aa3e1216a47c99030647e23f3f570dbfbc5e34ac4e"

        headers:
          Jenkins-Crumb: "09e4712ad43932fd474594aa3e1216a47c99030647e23f3f570dbfbc5e34ac4e"

Here is the verbose output:

PLAY [Play 1] **********************************************************************************************************************************************************

TASK [Gathering Facts] *************************************************************************************************************************************************


TASK [Add host] ********************************************************************************************************************************************************
task path: /app/test/Ansible/playbook/uritest.yml:13
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: unixuser1
<127.0.0.1> EXEC /bin/sh -c 'echo ~unixuser1 && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/unixuser1/.ansible/tmp/ansible-tmp-1600455948.59-238281813476791 `" && echo ansible-tmp-1600455948.59-238281813476791="` echo /home/unixuser1/.ansible/tmp/ansible-tmp-1600455948.59-238281813476791 `" ) && sleep 0'
Using module file /usr/lib/python2.7/site-packages/ansible/modules/net_tools/basics/uri.py
Pipelining is enabled.
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/unixuser1/.ansible/tmp/ansible-tmp-1600455948.59-238281813476791/ > /dev/null 2>&1 && sleep 0'
fatal: [localhost]: 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>/job/job5</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:// NOTHING</a><hr/>\n\n</body>\n</html>\n",
    "content_length": "541",
    "content_type": "text/html;charset=iso-8859-1",
    "elapsed": 0,
    "invocation": {
        "module_args": {
            "attributes": null,
            "backup": null,
            "body": [
                [
                    "myname",
                    "Tom"
                ]
            ],
            "body_format": "json",
            "client_cert": null,
            "client_key": null,
            "content": null,
            "creates": null,
            "delimiter": null,
            "dest": null,
            "directory_mode": null,
            "follow": false,
            "follow_redirects": "safe",
            "force": false,
            "force_basic_auth": false,
            "group": null,
            "headers": {
                "Content-Type": "application/json",
                "Jenkins-Crumb": "09e4712ad43932fd474594aa3e1216a47c99030647e23f3f570dbfbc5e34ac4e"
            },
            "http_agent": "Jenkins-Crumb:09e4712ad43932fd474594aa3e1216a47c99030647e23f3f570dbfbc5e34ac4e",
            "method": "POST",
            "mode": null,
            "owner": null,
            "regexp": null,
            "remote_src": null,
            "removes": null,
            "return_content": false,
            "selevel": null,
            "serole": null,
            "setype": null,
            "seuser": null,
            "src": null,
            "status_code": [
                200
            ],
            "timeout": 30,
            "unix_socket": null,
            "unsafe_writes": null,
            "url": "https://user1:118f32aa48601c136d29b11f3dd0e107f5@portal.myshop.com:9043/job/job5",
            "url_password": null,
            "url_username": null,
            "use_proxy": true,
            "validate_certs": false
        }
    },
    "msg": "Status code was 403 and not [200]: HTTP Error 403: Forbidden",
    "redirected": false,
    "server": "Jetty(NOTHING)",
    "set_cookie": "ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE=; Path=/; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Secure; HttpOnly, JSESSIONID.6900aad5=node06k5hjy5nr15txhr0qj9kzxp3791.node0; Path=/; Secure; HttpOnly",
    "status": 403,
    "url": "https://user1:118f32aa48601c136d29b11f3dd0e107f5@portal.myshop.com:9043/job/job5",
    "x_content_type_options": "nosniff"
}

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

I also tried passing the `url_username` and url_password` but i still get 403  as below:

    - name: Add host
      uri:
        url: "https://portal.myshop.com:9043/job/job5"
        validate_certs: false
        method: POST
        url_username: "user1"
        url_password: "Monsoon2020@"
#        force_basic_auth: yes
        body_format: form-urlencoded
        body:
          - [ name, "DEKHTEHAIN" ]
        http_agent: "Jenkins-Crumb:51ef89606820baccc3aed6bb6c26e54af129d56b65be97b679fb89d14fa87a1e"

        headers:
          Jenkins-Crumb: "51ef89606820baccc3aed6bb6c26e54af129d56b65be97b679fb89d14fa87a1e"

Output:

TASK [Add host] ********************************************************************************************************************************************************
task path: /app/test/Ansible/playbook/checkssh/uritest.yml:13
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: unixuser1
<127.0.0.1> EXEC /bin/sh -c 'echo ~unixuser1 && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/unixuser1/.ansible/tmp/ansible-tmp-1600467804.84-53369931519150 `" && echo ansible-tmp-1600467804.84-53369931519150="` echo /home/unixuser1/.ansible/tmp/ansible-tmp-1600467804.84-53369931519150 `" ) && sleep 0'
Using module file /usr/lib/python2.7/site-packages/ansible/modules/net_tools/basics/uri.py
Pipelining is enabled.
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/unixuser1/.ansible/tmp/ansible-tmp-1600467804.84-53369931519150/ > /dev/null 2>&1 && sleep 0'
fatal: [localhost]: 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>/job/job5</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:// NOTHING</a><hr/>\n\n</body>\n</html>\n",
    "content_length": "541",
    "content_type": "text/html;charset=iso-8859-1",
    "elapsed": 0,
    "invocation": {
        "module_args": {
            "attributes": null,
            "backup": null,
            "body": [
                [
                    "name",
                    "DEKHTEHAIN"
                ]
            ],
            "body_format": "form-urlencoded",
            "client_cert": null,
            "client_key": null,
            "content": null,
            "creates": null,
            "delimiter": null,
            "dest": null,
            "directory_mode": null,
            "follow": false,
            "follow_redirects": "safe",
            "force": false,
            "force_basic_auth": false,
            "group": null,
            "headers": {
                "Content-Type": "application/x-www-form-urlencoded",
                "Jenkins-Crumb": "51ef89606820baccc3aed6bb6c26e54af129d56b65be97b679fb89d14fa87a1e"
            },
            "http_agent": "Jenkins-Crumb:51ef89606820baccc3aed6bb6c26e54af129d56b65be97b679fb89d14fa87a1e",
            "method": "POST",
            "mode": null,
            "owner": null,
            "regexp": null,
            "remote_src": null,
            "removes": null,
            "return_content": false,
            "selevel": null,
            "serole": null,
            "setype": null,
            "seuser": null,
            "src": null,
            "status_code": [
                200
            ],
            "timeout": 30,
            "unix_socket": null,
            "unsafe_writes": null,
            "url": "https://portal.myshop.com:9043/job/job5",
            "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "url_username": "user1",
            "use_proxy": true,
            "validate_certs": false
        }
    },
    "msg": "Status code was 403 and not [200]: HTTP Error 403: Forbidden",
    "redirected": false,
    "server": "Jetty(NOTHING)",
    "set_cookie": "ACEGI_SECURITY_HASHED_REMEMBER_ME_COOKIE=; Path=/; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Secure; HttpOnly, JSESSIONID.6900aad5=node0d8lg3gcureb671sndwrtobwm800.node0; Path=/; Secure; HttpOnly",
    "status": 403,
    "url": "https://portal.myshop.com:9043/job/job5",
    "x_content_type_options": "nosniff"
}

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

I then tried with force_basic_auth: yes but i get HTTP Error 401: Unauthorized error.

TASK [Add host] ********************************************************************************************************************************************************
task path: /app/test/Ansible/playbook/checkssh/uritest.yml:13
<127.0.0.1> ESTABLISH LOCAL CONNECTION FOR USER: unixuser1
<127.0.0.1> EXEC /bin/sh -c 'echo ~unixuser1 && sleep 0'
<127.0.0.1> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /home/unixuser1/.ansible/tmp/ansible-tmp-1600467896.88-164232533519804 `" && echo ansible-tmp-1600467896.88-164232533519804="` echo /home/unixuser1/.ansible/tmp/ansible-tmp-1600467896.88-164232533519804 `" ) && sleep 0'
Using module file /usr/lib/python2.7/site-packages/ansible/modules/net_tools/basics/uri.py
Pipelining is enabled.
<127.0.0.1> EXEC /bin/sh -c '/usr/bin/python2 && sleep 0'
<127.0.0.1> EXEC /bin/sh -c 'rm -f -r /home/unixuser1/.ansible/tmp/ansible-tmp-1600467896.88-164232533519804/ > /dev/null 2>&1 && sleep 0'
fatal: [localhost]: 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 401 Invalid password/token for user: user1</title>\n</head>\n<body><h2>HTTP ERROR 401 Invalid password/token for user: user1</h2>\n<table>\n<tr><th>URI:</th><td>/job/job5</td></tr>\n<tr><th>STATUS:</th><td>401</td></tr>\n<tr><th>MESSAGE:</th><td>Invalid password/token for user: user1</td></tr>\n<tr><th>SERVLET:</th><td>Stapler</td></tr>\n</table>\n<hr><a href=\"http://eclipse.org/jetty\">Powered by Jetty:// NOTHING</a><hr/>\n\n</body>\n</html>\n",
    "content_length": "532",
    "content_type": "text/html;charset=iso-8859-1",
    "elapsed": 0,
    "invocation": {
        "module_args": {
            "attributes": null,
            "backup": null,
            "body": [
                [
                    "name",
                    "DEKHTEHAIN"
                ]
            ],
            "body_format": "form-urlencoded",
            "client_cert": null,
            "client_key": null,
            "content": null,
            "creates": null,
            "delimiter": null,
            "dest": null,
            "directory_mode": null,
            "follow": false,
            "follow_redirects": "safe",
            "force": false,
            "force_basic_auth": true,
            "group": null,
            "headers": {
                "Content-Type": "application/x-www-form-urlencoded",
                "Jenkins-Crumb": "51ef89606820baccc3aed6bb6c26e54af129d56b65be97b679fb89d14fa87a1e"
            },
            "http_agent": "Jenkins-Crumb:51ef89606820baccc3aed6bb6c26e54af129d56b65be97b679fb89d14fa87a1e",
            "method": "POST",
            "mode": null,
            "owner": null,
            "regexp": null,
            "remote_src": null,
            "removes": null,
            "return_content": false,
            "selevel": null,
            "serole": null,
            "setype": null,
            "seuser": null,
            "src": null,
            "status_code": [
                200
            ],
            "timeout": 30,
            "unix_socket": null,
            "unsafe_writes": null,
            "url": "https://portal.myshop.com:9043/job/job5",
            "url_password": "VALUE_SPECIFIED_IN_NO_LOG_PARAMETER",
            "url_username": "user1",
            "use_proxy": true,
            "validate_certs": false
        }
    },
    "msg": "Status code was 401 and not [200]: HTTP Error 401: Unauthorized",
    "redirected": false,
    "server": "Jetty(NOTHING)",
    "status": 401,
    "url": "https://portal.myshop.com:9043/job/job5",
    "www_authenticate": "Basic realm=\"Jenkins\"",
    "x_content_type_options": "nosniff"
}

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

Note: The purpose of using uri module instead of [ curl with raw or command module ] is that; I'm having issues / difficulties passing Jenkins multiline parameters myname to curl which is unresolved here

Can you please suggest how can i fix the issue?

0 Answers
Related