如何将参数从文件传递给 for 循环并并行运行,并为每个参数创建日志

如何将参数从文件传递给 for 循环并并行运行,并为每个参数创建日志
for test in "${a[@]}"
do
    
    sh ansiblescript.sh -s $a

    if [[ $? -eq 0 ]]; then
        echo "Success"
    else
        echo "Failed"
        exit 1
    fi
done

我有上面的 for 循环,它接受来自以下文件的输入,例如

测试.txt

a b c d 

现在它首先运行 fora然后转到b。我想并行运行所有四个并为每个写入单独的日志文件。

答案1

例如,给定测试脚本

shell> cat ansiblescript.sh
timestamp=`date +"%b %d %H:%M:%S"`
printf "$timestamp ansiblescript.sh: started\n"
sleep 5
timestamp=`date +"%b %d %H:%M:%S"`
printf "$timestamp ansiblescript.sh: finished\n"
exit 0

下面的剧本将创建目录/tmp/ansible对于日志。然后,文件的内容测试.txt将被分割和迭代。该脚本将异步执行。看异步操作和轮询

shell> cat playbook.yml
- hosts: localhost
  gather_facts: false
  tasks:
    - file:
        state: directory
        path: /tmp/ansible

    - debug:
        msg: "{{ '%b %d %H:%M:%S'|strftime }} Sctipts started."

    - shell:
        cmd: "sh ansiblescript.sh -s {{ item }} >>
              /tmp/ansible/ansiblescript-{{ item }}.log"
      register: result
      async: 30
      poll: 0
      loop: "{{ lookup('file', 'test.txt')|split(' ') }}"

    - async_status:
        jid: "{{ item.ansible_job_id }}"
      loop: "{{ result.results }}"
      loop_control:
        label: "{{ item.item }} job_id={{ item.ansible_job_id }}"
      register: ap_result
      until: ap_result.finished
      retries: 30

    - debug:
        msg: "{{ '%b %d %H:%M:%S'|strftime }} Scripts finished."

    - debug:
        msg: "{{ (item.rc == 0)|ternary('Success', 'Failed') }}
              {{ item.start }}
              {{ item.end }}"
      loop: "{{ ap_result.results }}"
      loop_control:
        label: "{{ item.item.item }}"

给出

shell> ansible-playbook playbook.yml

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

TASK [file] ****************************************************************
ok: [localhost]

TASK [debug] ***************************************************************
ok: [localhost] => 
  msg: Apr 13 22:35:23 Sctipts started.

TASK [shell] ***************************************************************
changed: [localhost] => (item=a)
changed: [localhost] => (item=b)
changed: [localhost] => (item=c)
changed: [localhost] => (item=d)

TASK [async_status] ********************************************************
FAILED - RETRYING: [localhost]: async_status (30 retries left).
changed: [localhost] => (item=a job_id=881555332548.1994349)
changed: [localhost] => (item=b job_id=644490646974.1994376)
changed: [localhost] => (item=c job_id=859370052106.1994407)
changed: [localhost] => (item=d job_id=704644892779.1994438)

TASK [debug] ***************************************************************
ok: [localhost] => 
  msg: Apr 13 22:35:31 Scripts finished.

TASK [debug] ***************************************************************
ok: [localhost] => (item=a) => 
  msg: Success 2022-04-13 22:35:24.569468 2022-04-13 22:35:29.581256
ok: [localhost] => (item=b) => 
  msg: Success 2022-04-13 22:35:24.846117 2022-04-13 22:35:29.858153
ok: [localhost] => (item=c) => 
  msg: Success 2022-04-13 22:35:25.112447 2022-04-13 22:35:30.127829
ok: [localhost] => (item=d) => 
  msg: Success 2022-04-13 22:35:25.393390 2022-04-13 22:35:30.405189
PLAY RECAP *****************************************************************
localhost: ok=6 changed=2 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0

查看日志文件

shell> cat /tmp/ansible/ansiblescript-a.log 
Apr 13 22:35:24 ansiblescript.sh: started
Apr 13 22:35:29 ansiblescript.sh: finished
shell> cat /tmp/ansible/ansiblescript-b.log 
Apr 13 22:35:24 ansiblescript.sh: started
Apr 13 22:35:29 ansiblescript.sh: finished
shell> cat /tmp/ansible/ansiblescript-c.log 
Apr 13 22:35:25 ansiblescript.sh: started
Apr 13 22:35:30 ansiblescript.sh: finished
shell> cat /tmp/ansible/ansiblescript-d.log 
Apr 13 22:35:25 ansiblescript.sh: started
Apr 13 22:35:30 ansiblescript.sh: finished

答案2

您可以&在后台使用并运行一个进程,如下答案所示:https://stackoverflow.com/a/3643961/1572593

sh ansiblescript.sh -s $a &

您可能添加了处理挂断信号的逻辑。换句话说,当脚本结束并且进程仍在运行时会发生什么。这方面的研究wait也是如此nohup

答案3

使用 GNU Parallel 时,它看起来像这样:

parallel -j0 --result {}.out --joblog my.log sh ansiblescript.sh -s {} ::: "${a[@]}" || echo one or more failed

相关内容