title: How to restart Jenkins using Ansible and wait for it to come back?
body: <p>I'm trying to restart the Jenkins service using Ansible:</p>
  
  <pre><code>- 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
  </code></pre>
  
  <p>But on the first run, the third task throws lots of Java errors including this: <code>Suppressed: java.io.IOException: Server returned HTTP response code: 503 for URL</code>, 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.</p>
  
  <p>So I guess what I need is to curl many times until the http code is 200? Is there any other way?</p>
  
  <p>Either way, how do I do that?</p>
  
  <p>How do you normally restart Jenkins?</p>