如何使用async运行Ansible并行任务并检查日志是否成功/失败

时间:2017-10-05 20:04:34

标签: parallel-processing ansible uri

我正在尝试使用ansible playbook并行启动几个作业。 我必须POST到http才能开始工作;一旦工作排队,工作可能需要1-3分钟才能完成。我需要在后台并行启动几个作业,然后轮询日志以获取成功消息或失败消息,并且还需要超时。我有ssh访问SERVERNAME所以正则表达式搜索部分非常有用;但是,当它发现"开始失败时,我没有想办法让它失败"在日志中。尝试状态=缺席,但这似乎适用于其他wait_for组件。

这一切在Ansible中都可行吗?我想出了下面的yaml。

---

- hosts: localhost
  connection: local
  gather_facts: no


  tasks:

    - name: Launch an http POST
      async: 10
      poll: 0
      uri:
        url: "https://SERVERNAME/MYLINK1"
        method: POST
        headers:
          Content-Type: "application/x-www-form-urlencoded"
        status_code: 200
        validate_certs: no
        timeout: 10
        return_content: yes
      register: response1

    - name: Launch an http POST
      async: 10
      poll: 0
      uri:
        url: "https://SERVERNAME/MYLINK2"
        method: POST
        headers:
          Content-Type: "application/x-www-form-urlencoded"
        status_code: 200
        validate_certs: no
        timeout: 10
        return_content: yes
      register: response2

    - name: Wait Job to be ready
      async: 120
      delegate_to: SERVERNAME
      wait_for:
        path: /usr/local/logs/mylog1.log
        search_regex: "Started Success"
      register: wait_for_success1

    - name: Wait Job to be failed
      async: 120
      delegate_to: SERVERNAME
      wait_for:
        path: /usr/local/logs/mylog1.log
        search_regex: "Started Failed"
      register: wait_for_failed1

    - name: Wait Job to be ready
      async: 120
      delegate_to: SERVERNAME
      wait_for:
        path: /usr/local/logs/mylog2.log
        search_regex: "Started Success"
      register: wait_for_success=2

    - name: Wait Job to be failed
      async: 120
      delegate_to: SERVERNAME
      wait_for:
        path: /usr/local/logs/mylog2.log
        search_regex: "Started Failed"
      register: wait_for_failed2

1 个答案:

答案 0 :(得分:1)

您不应该在检查任务中使用timeout。您可以在wait_for模块中使用- hosts: SERVERNAME tasks: - name: Wait Job1 to be ready wait_for: path: /usr/local/logs/mylog1.log search_regex: "Started Success" timeout: 120 - name: Wait Job2 to be ready wait_for: path: /usr/local/logs/mylog2.log search_regex: "Started Success" timeout: 120 参数。

void myfunc()

我不会在检查"开始失败"出现在日志中。如果Ansible没有看到"开始成功"在超过你的超时的日志中(在这种情况下是2米)然后播放将失败。