使用并行执行(串行)时,Ansible playbook 在 HTML 报告中缺少主机

问题描述 投票:0回答:1

我有一个 Ansible playbook,它在大量主机(大约 800 个)上运行,以生成包含主机名的 HTML 报告。该剧本旨在在所有主机上并行运行。

当我将串行参数设置为 500 或更高的值时,生成的 HTML 报告包含的条目少于预期(例如,仅 500 个主机)。但是,当我设置序列号:1时,我会得到报告中的所有条目。虽然 Serial: 1 有效,但这不是一个可行的解决方案,因为我希望剧本同时在所有主机上运行。

这是我的剧本的简化版本:

- name: Tail Logs and Generate Report
  hosts: all
  gather_facts: no
  ignore_unreachable: yes
  serial: 500  # The issue occurs with higher values

  tasks:
    - name: create header html
      lineinfile:
         path: /reports/daily_report.html
         line: "<!DOCTYPE html> <html> <head> <style>table{border-collapse:collapse;width:100%}td,th{border:1px solid #000;padding:8px;text-align:left}</style><title>Daily Report Details</title> </head> <body> <h1>Daily Report Details</h1> <table> <thead>
      <tr> 
      <th>PG Host Name</th>
      </tr> 
      </thead> <tbody>"
      create: yes
      delegate_to: localhost
      run_once: true
      ignore_errors: yes

    - name: Write content in report
      lineinfile:
      path: /reports/daily_report.html
      line: "<tr> 
          <td> {{ inventory_hostname }}</td>
          </tr>"
      insertafter: EOF
      delegate_to: localhost
      ignore_errors: yes
      no_log: true

    - name: create footer html
      lineinfile:
      path: /reports/daily_report.html
      line: "</tbody> </table> </body> </html>"
      delegate_to: localhost
      ignore_errors: yes
      run_once: true

我尝试过调试、检查主机迭代逻辑并查看错误消息,但无法确定根本原因。该剧本似乎可以与较低的串行值并行运行。

有人可以帮助我理解为什么增加序列值会导致 HTML 报告中缺少主机吗?在 Ansible playbook 中使用高串行值时是否有任何最佳实践或注意事项?

任何指导或建议将不胜感激。谢谢!

ansible ansible-inventory
1个回答
0
投票

添加 throttle: 1 有助于解决该问题。这有助于为所有服务器逐一运行特定任务,而不是同时运行。在所有服务器上同时执行可能是导致问题的原因。请参考了解更多信息。

- name: Tail Logs and Generate Report
  hosts: all
  gather_facts: no
  ignore_unreachable: yes

  tasks:
    - name: create header html
      lineinfile:
         path: /reports/daily_report.html
         line: "<!DOCTYPE html> <html> <head> <style>table{border-collapse:collapse;width:100%}td,th{border:1px solid #000;padding:8px;text-align:left}</style><title>Daily Report Details</title> </head> <body> <h1>Daily Report Details</h1> <table> <thead>
      <tr> 
      <th>PG Host Name</th>
      </tr> 
      </thead> <tbody>"
      create: yes
      delegate_to: localhost
      run_once: true
      ignore_errors: yes

    - name: Write content in report
      lineinfile:
      path: /reports/daily_report.html
      line: "<tr> 
          <td> {{ inventory_hostname }}</td>
          </tr>"
      insertafter: EOF
      delegate_to: localhost
      ignore_errors: yes
      no_log: true
      throttle: 1

    - name: create footer html
      lineinfile:
      path: /reports/daily_report.html
      line: "</tbody> </table> </body> </html>"
      delegate_to: localhost
      ignore_errors: yes
      run_once: true
© www.soinside.com 2019 - 2024. All rights reserved.