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
次に、最初に実行してa
から移動しますb
。 4つすべてを並列に実行し、それぞれに別々のログファイルを作成したいと思います。
ベストアンサー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