How to restart Jenkins using Ansible and wait for it to come back?

ansible, jenkins, jenkins-plugins

Solution

I solved it using curl + Ansible's `until`, which I just learned about. It will request the page until the http status code is 200 OK.

- name: Wait untils Jenkins web API is available
  shell: curl --head --silent http://localhost:8080/cli/
  register: result
  until: result.stdout.find("200 OK") != -1
  retries: 12
  delay: 5

Problem

I'm trying to restart the Jenkins service using Ansible: ``` - name: Restart Jenkins to make the plugin data available service: name=jenkins state=restarted - name: Wait for Jenkins to restart wait_for: host=localhost port=8080 delay=20 timeout=300 - name: Install Jenkins plugins command: java -jar {{ jenkins_cli_jar }} -s {{ jenkins_dashboard_url }} install-plugin {{ item }} creates=/var/lib/jenkins/plugins/{{ item }}.jpi with_items: jenkins_plugins ``` But on the first run, the third task throws lots of Java errors including this: `Suppressed: java.io.IOException: Server returned HTTP response code: 503 for URL`, which makes me think the web server (handled entirely by Jenkins) wasn't ready. Sometimes when I go to the Jenkins dashboard using my browser it says that Jenkins isn't ready and that it will reload when it is, and it does, it works fine. But I'm not sure if accessing the page is what starts the server, or what. So I guess what I need is to curl many times until the http code is 200? Is there any other way? Either way, how do I do that? How do you normally restart Jenkins?

Original source