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

ChocoDeveloper picture ChocoDeveloper · May 28, 2014 · Viewed 19.4k times · Source

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?

Answer

chrism picture chrism · Dec 30, 2015

Using the URI module http://docs.ansible.com/ansible/uri_module.html

   - name: "wait for ABC to come up"
     uri:
       url: "http://127.0.0.1:8080/ABC"
       status_code: 200
     register: result
     until: result.status == 200
     retries: 60
     delay: 1