Ansible:如何在一定时间间隔内并行执行ansible剧本中的任务

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

让我尝试解释我的需求:

作为我们应用程序常规部署的一部分,我们有一个SQL脚本(它将更改表,添加表或进行更新等),例如,该脚本需要在一个区域中的3个模式上运行,而在另一个区域中的5个模式上运行。该应用程序位于AWS中,数据库为Arora db(RDS)-MySQL。此架构可能需要30分钟到3个小时之间的任何时间。

此SQL脚本需要并行运行,并且每个模式运行之间要有2分钟的延迟。

这是我到目前为止所取得的成就:

  1. 具有数据库详细信息的文件-dbdata.yml
---

conn_details:
    - { host: localhost, user: root, password: "Password1!" }
    - { host: localhost, user: root, password: "Password1!" }
  1. 剧本:
- hosts: localhost

  vars:
    script_file: "{{ path }}"
  vars_files:
    - dbdata.yml
  tasks:
  - name: shell command to execute script in parallel
    shell: |
      sleep 30s
      "mysql -h {{ item.host }} -u {{ item.user }} -p{{ item.password }} < {{ script_file }} >> /usr/local/testscript.log"
    with_items: "{{ conn_details }}"
    register: sql_query_output
    async: 600
    poll: 0

  - name: Wait for sql execution to finish
    async_status:
      jid: "{{ item.ansible_job_id }}"
    register: _jobs
    until: _jobs.finished
    delay: 20  # Check every 20 seconds. Adjust as you like.
    retries: 10
    with_items: "{{ sql_query_output.results }}"

第一部分-并行执行脚本,并且在每次执行之前还包括30秒的时间间隔。

第二部分-从注册的输出中选择可处理作业ID,并检查作业是否完成。

[请注意:在包含30秒的睡眠之前,此剧本运行良好。

我们在执行时有以下错误输出:

ansible-playbook parallel_local.yml --extra-vars "path=RDS_script.sql"
 [WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'


PLAY [localhost] **************************************************************************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************************************************************
ok: [localhost]

TASK [sample command- ansible-playbook my_sqldeploy.yml --extra-vars "path=/home/NICEONDEMAND/bsahu/RDS_create_user1.sql"] ****************************************************************************************
changed: [localhost] => (item={u'host': u'localhost', u'password': u'Password1!', u'user': u'root'})
changed: [localhost] => (item={u'host': u'localhost', u'password': u'Password1!', u'user': u'root'})

TASK [Wait for creation to finish] ********************************************************************************************************************************************************************************
FAILED - RETRYING: Wait for creation to finish (10 retries left).
FAILED - RETRYING: Wait for creation to finish (9 retries left).
failed: [localhost] (item={'ansible_loop_var': u'item', u'ansible_job_id': u'591787538842.77844', 'item': {u'host': u'localhost', u'password': u'Password1!', u'user': u'root'}, u'started': 1, 'changed': True, 'failed': False, u'finished': 0, u'results_file': u'/root/.ansible_async/591787538842.77844'}) => {"ansible_job_id": "591787538842.77844", "ansible_loop_var": "item", "attempts": 3, "changed": true, "cmd": "sleep 30s\n\"mysql -h localhost -u root -pPassword1! < RDS_script.sql >> /usr/local/testscript.log\"\n", "delta": "0:00:30.073191", "end": "2019-11-28 17:01:57.632285", "finished": 1, "item": {"ansible_job_id": "591787538842.77844", "ansible_loop_var": "item", "changed": true, "failed": false, "finished": 0, "item": {"host": "localhost", "password": "Password1!", "user": "root"}, "results_file": "/root/.ansible_async/591787538842.77844", "started": 1}, "msg": "non-zero return code", "rc": 127, "start": "2019-11-28 17:01:27.559094", "stderr": "/bin/sh: line 1: mysql -h localhost -u root -pPassword1! < RDS_script.sql >> /usr/local/testscript.log: No such file or directory", "stderr_lines": ["/bin/sh: line 1: mysql -h localhost -u root -pPassword1! < RDS_script.sql >> /usr/local/testscript.log: No such file or directory"], "stdout": "", "stdout_lines": []}
failed: [localhost] (item={'ansible_loop_var': u'item', u'ansible_job_id': u'999397686792.77873', 'item': {u'host': u'localhost', u'password': u'Password1!', u'user': u'root'}, u'started': 1, 'changed': True, 'failed': False, u'finished': 0, u'results_file': u'/root/.ansible_async/999397686792.77873'}) => {"ansible_job_id": "999397686792.77873", "ansible_loop_var": "item", "attempts": 1, "changed": true, "cmd": "sleep 30s\n\"mysql -h localhost -u root -pPassword1! < RDS_script.sql >> /usr/local/testscript.log\"\n", "delta": "0:00:30.120136", "end": "2019-11-28 17:01:58.694713", "finished": 1, "item": {"ansible_job_id": "999397686792.77873", "ansible_loop_var": "item", "changed": true, "failed": false, "finished": 0, "item": {"host": "localhost", "password": "Password1!", "user": "root"}, "results_file": "/root/.ansible_async/999397686792.77873", "started": 1}, "msg": "non-zero return code", "rc": 127, "start": "2019-11-28 17:01:28.574577", "stderr": "/bin/sh: line 1: mysql -h localhost -u root -pPassword1! < RDS_script.sql >> /usr/local/testscript.log: No such file or directory", "stderr_lines": ["/bin/sh: line 1: mysql -h localhost -u root -pPassword1! < RDS_script.sql >> /usr/local/testscript.log: No such file or directory"], "stdout": "", "stdout_lines": []}

PLAY RECAP ********************************************************************************************************************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0   

关于如何克服这一问题的任何建议。预先感谢您提供的所有帮助。

parallel-processing ansible execution
1个回答
0
投票

我不好。我犯了一个愚蠢的错误,正在制造麻烦。我需要从执行sql文件的行中删除“”。在下面复制正确的Yaml文件

- hosts: localhost

  vars:
    script_file: "{{ path }}"
  vars_files:
    - dbdata.yml
  tasks:
  - name: sample command- ansible-playbook my_sqldeploy.yml --extra-vars "path=/home/NICEONDEMAND/bsahu/RDS_create_user1.sql"
    shell: |
      sleep 30s
      mysql -h {{ item.host }} -u {{ item.user }} -p{{ item.password }} < {{ script_file }} >> /usr/local/testscript.log
    with_items: "{{ conn_details }}"
    register: sql_query_output
    async: 600
    poll: 0

  - name: Wait for creation to finish
    async_status:
      jid: "{{ item.ansible_job_id }}"
    register: _jobs
    until: _jobs.finished
    delay: 20  # Check every 5 seconds. Adjust as you like.
    retries: 10
    with_items: "{{ sql_query_output.results }}"

非常感谢您的帮助。

© www.soinside.com 2019 - 2024. All rights reserved.