如何通过循环加快任务

时间:2019-05-30 15:03:02

标签: ansible

我有一个剧本,每次循环迭代都会发出网络请求。不幸的是,这非常慢。每次迭代大约需要一秒钟。有没有办法加快任务?我尝试使用异步,但情况更糟

Async:

real    0m12.609s
user    0m3.307s
sys     0m1.082s


Not Async:
real    0m7.198s
user    0m3.562s
sys     0m0.955s

剧本:

- name: Push to Nagios
  uri:
    url: https://nagios/nagiosxi/api/v1/config/service?apikey={{ nagios_apikey  }}&config_name={{ ansible_fqdn }}-fs-{{ item.mount | replace('/','-') }}
    method: POST
    body_format: form-urlencoded
    body:
      host_name: "{{ ansible_fqdn }}"
      service_description: FS {{ item.mount }}
      register: 1
      check_command: "check_nrpe!check_fs_single!\'-w {{ warning }} -c {{ critical }} -p {{ item.mount }} -e -l -u {{ 'MB' if item.size_total is defined and item.size_total < 5368709120 else 'GB' }}\'!!!!!!"
      initial_state: "{{ fs[ansible_fqdn]['initial_state'] if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'initial_state' in fs[ansible_fqdn][item.mount]) else service[env]['initial_state']}}"
      max_check_attempts: "{{ fs[ansible_fqdn]['max_check_attempts'] if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'max_check_attempts' in fs[ansible_fqdn][item.mount]) else service[env]['max_check_attempts']}}"
      check_interval: "{{ fs[ansible_fqdn]['check_interval'] if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'check_interval' in fs[ansible_fqdn][item.mount]) else service[env]['check_interval']}}"
      retry_interval: "{{ fs[ansible_fqdn]['retry_interval'] if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'retry_interval' in fs[ansible_fqdn][item.mount]) else service[env]['retry_interval']}}"
      active_checks_enabled: "{{ fs[ansible_fqdn]['active_checks_enabled'] if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'active_checks_enabled' in fs[ansible_fqdn][item.mount]) else service[env]['active_checks_enabled']}}"
      passive_checks_enabled: "{{ fs[ansible_fqdn]['passive_checks_enabled'] if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'passive_checks_enabled' in fs[ansible_fqdn][item.mount]) else service[env]['passive_checks_enabled']}}"
      check_period: "{{ fs[ansible_fqdn]['check_period'] if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'check_period' in fs[ansible_fqdn][item.mount]) else service[env]['check_period']}}"
      obsess_over_service: "{{ fs[ansible_fqdn]['obsess_over_service'] if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'obsess_over_service' in fs[ansible_fqdn][item.mount]) else service[env]['obsess_over_service']}}"
      process_perf_data: "{{ fs[ansible_fqdn]['process_perf_data'] if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'process_perf_data' in fs[ansible_fqdn][item.mount]) else service[env]['process_perf_data']}}"
      notification_interval: "{{ fs[ansible_fqdn]['notification_interval'] if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'notification_interval' in fs[ansible_fqdn][item.mount]) else service[env]['notification_interval']}}"
      notification_period: "{{ fs[ansible_fqdn]['notification_period'] if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'notification_period' in fs[ansible_fqdn][item.mount]) else service[env]['notification_period']}}"
      notification_options: "{{ fs[ansible_fqdn]['notification_options'] if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'notification_options' in fs[ansible_fqdn][item.mount]) else service[env]['notification_options']}}"
      notifications_enabled: "{{ fs[ansible_fqdn]['notifications_enabled'] if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'notifications_enabled' in fs[ansible_fqdn][item.mount]) else service[env]['notifications_enabled']}}"
      contacts: "{{ fs[ansible_fqdn]['contacts'] | join(',') if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'contacts' in fs[ansible_fqdn][item.mount]) else service[env]['contacts'] | join(',') }}"

  loop: "{{ ansible_mounts }}"
  vars:
    warning: "{{ fs[ansible_fqdn][item.mount]['warning'] if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'warning' in fs[ansible_fqdn][item.mount]) else fs['defaults']['warning']}}"
    critical: "{{ fs[ansible_fqdn][item.mount]['critical'] if ((fs is defined) and ansible_fqdn in fs and item.mount in fs[ansible_fqdn] and 'critical' in fs[ansible_fqdn][item.mount]) else fs['defaults']['critical']}}"
  register: upload
  failed_when: 'upload.json is defined and upload.json.error is defined'
  when: item.fstype == 'xfs' or item.fstype == 'ext4'
  delegate_to: localhost
#  async: 60 
#  poll: 0

#- name: Wait for My long running task to finish
#  async_status:
#    jid: "{{ item.ansible_job_id }}"
#  register: _upload
#  retries: 20
#  delay: 2
#  until: _upload.finished
#  loop: "{{ upload.results }}"
#  delegate_to: localhost  

0 个答案:

没有答案