简体   繁体   中英

Install Jenkins Plugins with Ansible

I'm trying to install a jenkins master with ansible and so far everything seems to work just fine. But now I stumble with the plugins installation.

I found the jenkins_plugin directive from ansible but I can't figure out which parameters to give. I guess I'm just identifying the plugins wrong (ie not using the correct name). I have no clue how to figure out which name/id I should use.

- name: Install Plugin "active-directory"
  jenkins_plugin:
    name: "active-directory"
  environment: "{{proxy_env}}"

results in HTTP 404

fatal: [<SERVERNAME>]: FAILED! => {
    "changed": false,
    "details": "HTTP Error 404: Not Found",
    "invocation": {
        "module_args": {
            "attributes": null,
            "backup": null,
            "client_cert": null,
            "client_key": null,
            "content": null,
            "delimiter": null,
            "directory_mode": null,
            "follow": false,
            "force": false,
            "force_basic_auth": true,
            "group": "jenkins",
            "http_agent": "ansible-httpget",
            "jenkins_home": "/var/lib/jenkins",
            "mode": "0644",
            "name": "active-directory",
            "owner": "jenkins",
            "params": null,
            "regexp": null,
            "remote_src": null,
            "selevel": null,
            "serole": null,
            "setype": null,
            "seuser": null,
            "src": null,
            "state": "present",
            "timeout": 30.0,
            "unsafe_writes": null,
            "updates_expiration": 86400,
            "updates_url": "https://updates.jenkins.io",
            "url": "http://localhost:8080",
            "url_password": null,
            "url_username": null,
            "use_proxy": true,
            "validate_certs": true,
            "version": null,
            "with_dependencies": true
        }
    },
    "msg": "Cannot get CSRF"
}

I guess this is correct due to me not using the correct names/ids

Is there a way to figure out which identifiers to use? Can I get the name from jenkins ui or file system? Is there a pattern I don't get? Or is there some kind of documentation on the internet?

Thanks in advance guys. I'm grateful for any hint. Best regards, Sebastian

Update 2019-08-06 I tried some different ways to get the plugin. I realized that the problem is more likely to be proxy authentication. I can resolve the update site via curl.

curl -x http://proxy.server.com:80 --proxy-user myUsername https://updates.jenkins.io

This is the html response from the curl.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<html>
 <head>
  <title>Index of /</title>
 </head>
 <body>
<h1>Index of /</h1>
  <table>
   <tr><th valign="top"><img src="/icons/blank.gif" alt="[ICO]"></th><th><a href="?C=N;O=D">Name</a></th><th><a href="?C=M;O=A">Last modified</a></th><th><a href="?C=S;O=A">Size</a></th><th><a href="?C=D;O=A">Description</a></th></tr>
   <tr><th colspan="5"><hr></th></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="2.121/">2.121/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="2.138/">2.138/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="2.150/">2.150/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="2.164/">2.164/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="2.176/">2.176/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="current/">current/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="download/">download/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="experimental/">experimental/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="latest/">latest/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/text.gif" alt="[TXT]"></td><td><a href="latestCore.txt">latestCore.txt</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  5 </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[   ]"></td><td><a href="plugin-documentation-urls.json">plugin-documentation-urls.json</a></td><td align="right">2019-08-06 15:12  </td><td align="right">152K</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[   ]"></td><td><a href="plugin-versions.json">plugin-versions.json</a></td><td align="right">2019-08-06 15:12  </td><td align="right">8.3M</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/text.gif" alt="[TXT]"></td><td><a href="pluginCount.txt">pluginCount.txt</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  4 </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[   ]"></td><td><a href="release-history.json">release-history.json</a></td><td align="right">2019-08-06 15:12  </td><td align="right">2.7M</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="stable-2.121/">stable-2.121/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="stable-2.138/">stable-2.138/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="stable-2.150/">stable-2.150/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="stable-2.164/">stable-2.164/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="stable-2.176/">stable-2.176/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="stable/">stable/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="temporary-experimental-java11/">temporary-experimental-java11/</a></td><td align="right">2019-08-06 15:12  </td><td align="right">  - </td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[   ]"></td><td><a href="update-center.actual.json">update-center.actual.json</a></td><td align="right">2019-08-06 15:12  </td><td align="right">1.7M</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/unknown.gif" alt="[   ]"></td><td><a href="update-center.json">update-center.json</a></td><td align="right">2019-08-06 15:12  </td><td align="right">1.7M</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/text.gif" alt="[TXT]"></td><td><a href="update-center.json.html">update-center.json.html</a></td><td align="right">2019-08-06 15:12  </td><td align="right">1.7M</td><td>&nbsp;</td></tr>
<tr><td valign="top"><img src="/icons/folder.gif" alt="[DIR]"></td><td><a href="updates/">updates/</a></td><td align="right">2019-08-06 12:20  </td><td align="right">  - </td><td>&nbsp;</td></tr>
   <tr><th colspan="5"><hr></th></tr>
</table>
<p>
    You can rsync these files via <tt>rsync -avz rsync://rsync.osuosl.org/jenkins/updates/  somewhere</tt>.
</p>
<p>
    For more information about the layout of update center,
    see <a href="https://github.com/jenkinsci/backend-update-center2/blob/master/site/LAYOUT.md">this document</a>.
</p></body></html>

But my ansible playbook does not get any response. It seems to act differently though when I change my task definition: - with proxy settings including username and passwort - with proxy settings including username but without passwort - without any proxy settings

Task definition:

- name: Install Plugin "active-directory"
  jenkins_plugin:
    name: "{{ item.key }}"
  environment:
    http_proxy: http://USER:PASS@proxy.server.com:80
    https_proxy: http://USER:PASS@proxy.server.com:80
  with_items:
    - { key: "active-directory", version: "2.16" }

Of cource I'm using real usernames, passwords and server names instead of the placeholders here.

# with password
failed: [s00260415uv] (item={u'version': u'2.16', u'key': u'active-directory'}) => {"ansible_loop_var": "item", "changed": false, "details": "HTTP Error 404: Not Found", "item": {"key": "active-directory", "version": "2.16"}, "msg": "Cannot get CSRF"}

# without password
failed: [s00260415uv] (item={u'version': u'2.16', u'key': u'active-directory'}) => {"ansible_loop_var": "item", "changed": false, "details": "HTTP Error 407: Proxy Authentication Required", "item": {"key": "active-directory", "version": "2.16"}, "msg": "Cannot get CSRF"}

# without any proxy settings
failed: [s00260415uv] (item={u'version': u'2.16', u'key': u'active-directory'}) => {"ansible_loop_var": "item", "changed": false, "details": "HTTP Error 403: Forbidden", "item": {"key": "active-directory", "version": "2.16"}, "msg": "Cannot get CSRF"}

Seems like without proxy settings access is denied, with username only proxy auth is incomplete and with username and password, the target is not found although the mentioned curl with the same username, password and proxy server works.

I still cannot manage to download a plugin.

Update 2019-08-07 I traced all HTTP requests during my last try and found a corresponding line to the 404 message in my trace.

2019-08-07 09:39:42     x.x.x.x   y.y.y.y    >       GET     localhost:8080  http://localhost:8080/api/json  HTTP/1.1        -       -
2019-08-07 09:39:42     y.y.y.y    x.x.x.x   <       -       -       -       HTTP/1.1        404     Not Found

A curl for that URL from my jenkins server (the one running ansible) results in this:

curl http://localhost:8080/api/json

Authentication required
<!--
You are authenticated as: anonymous

Is there a way I can add a jenkins unser to the plugins installation?

I found an answer (at least for the time beeing). The 404 status didn't come from any remote host but from my jenkins instance itself. Jenkins was not listening on localhost. So I changed the url to the actual servername . For now this works fine. But I still have to find out how to use localhost instead because I want to use the same playbook fpr several jenkins installations in different stages (= different server names).

Anyway, this is the correct snippet.

# Jenkins master: install plugins
- name: Install Plugins on behalf of jenkins user {{jenkins_admin_user}}
  jenkins_plugin:
    name: "{{ item.key }}"
    url: http://s00260415uv:8080
    url_username: "{{jenkins_admin_user}}"
    url_password: "{{jenkins_admin_pass}}"
  environment:
    http_proxy: http://<USER>:<PASS>@<PROXY_SERVER>:80
    https_proxy: http://<USER>:<PASS>@<PROXY_SERVER>:80
  register: plugin_result
  until: plugin_result is success
  retries: 10
  delay: 2
  with_items: # no version = latest
    - { key: "ace-editor"}
    - { key: "active-directory"}
    - { key: "..."}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM