【问题标题】:How to restart Jenkins using Ansible and wait for it to come back?如何使用 Ansible 重启 Jenkins 并等待它回来?
【发布时间】:2014-05-28 18:58:49
【问题描述】:

我正在尝试使用 Ansible 重新启动 Jenkins 服务:

- 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

但在第一次运行时,第三个任务引发了很多 Java 错误,包括:Suppressed: java.io.IOException: Server returned HTTP response code: 503 for URL,这让我觉得 Web 服务器(完全由 Jenkins 处理)还没有准备好。有时,当我使用浏览器访问 Jenkins 仪表板时,它会说 Jenkins 尚未准备好,并且会在准备好时重新加载,并且确实可以正常工作。但是我不确定访问页面是什么启动了服务器,还是什么。

所以我想我需要多次 curl 直到 http 代码为 200?有没有其他办法?

不管怎样,我该怎么做?

你通常如何重启 Jenkins?

【问题讨论】:

  • 致投票结束此主题的人:我正在尝试使用这些工具为我的开发团队设置一个持续部署服务器,并且标签已经存在。你怎么能说这与编程无关?如果我改用 Bash,你认为会好吗?
  • 你可以盲目重启 Jenkins,然后使用 wait_for 直到它备份。我有一个更好的 wait_for_http,但还没有为它做 pull req。
  • @tedder42 我发布的代码中已经有一个“wait_for”。并且在第一个任务之后服务已经启动。出不来的是 web 部件,我不知道如何等待它。我想我需要你提到的那种 wait_for_http 。
  • 抱歉,您是对的,您需要 wait_for_http 因为 port 已启动,即使 service 未启动。给我发电子邮件,我可以给你一些东西放在你的图书馆进行 beta 测试。 (是的,我可能会把它作为一个答案,只需要完善它)

标签: jenkins jenkins-plugins ansible ansible-playbook


【解决方案1】:

使用 URI 模块 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

【讨论】:

    【解决方案2】:

    我使用 curl + Ansible 的 until 解决了这个问题,这是我刚刚了解到的。它将请求页面,直到 http 状态码为 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
    

    【讨论】:

      【解决方案3】:

      我发现 Jenkins 在启动但未准备好时总是返回 503 响应(服务不可用)。因此,最可靠的方法是单独隔离和检查响应代码标头。

      - name: Wait until Jenkins web interface is available
        wait_for: "host={{ jenkins_host }} port={{ jenkins_port }} state=present delay=5 timeout=300"
      
      - name: Wait until Jenkins web interface is ready
        command: 'curl -s -o /dev/null -w "%{http_code}" http://{{ jenkins_host }}:{{ jenkins_port}}/cli/'
        register: result
        until: 'result.stdout[0] in ["2", "3"]' # 2xx or 3xx status code
        retries: 50
        delay: 3
        changed_when: false
      

      【讨论】:

        【解决方案4】:

        @ChocoDeveloper 答案的稍微更精确的版本,使用此版本,您可以检查特定的响应代码,而不是从 HTTP 响应中搜索原始标准输出

        - name: "wait for ABC to come up"
          shell: "curl --silent -o /dev/null -w '%{http_code}' http://127.0.0.1:8080/ABC"
          register: result
          until: result.stdout.find("401") != -1
          retries: 60
          delay: 1
        

        【讨论】:

          【解决方案5】:

          也许这会对其他人有所帮助,我试图为不同的服务实现类似的事情。我遇到的问题是 result.find("200 OK") != -1result.status == 200 在我的情况下不起作用。

          这对我有用:

          - name: wait for service to boot
            uri:
              url: "https://10.221.54.18"
              status_code: 200
              validate_certs: False
            register: result
            until: result | succeeded
            retries: 8
            delay: 20
          

          【讨论】:

            猜你喜欢
            • 2014-07-15
            • 1970-01-01
            • 2015-11-01
            • 2015-11-02
            • 1970-01-01
            • 1970-01-01
            • 2015-11-02
            相关资源
            最近更新 更多