さまざまな種類の出力のキャプチャ
前のステップでは、スクリプトの標準出力をキャプチャして表示しました。ただし、スクリプトを実行する際には、キャプチャしたい出力がいくつかあります。
- 標準出力 (stdout): スクリプトの通常の出力
- 標準エラー (stderr): エラーメッセージと警告
- リターンコード (rc): スクリプトの終了ステータス (通常、0 は成功を意味し、0 以外の値はエラーを示します)
3 種類のすべての出力をキャプチャして表示する新しいプレイブックを作成しましょう。
~/project/ansible-output-demo ディレクトリに capture_all_output.yml という名前の新しいファイルを作成します。
- 「File」メニューをクリックします。
- 「New File」を選択します。
capture_all_output.yml として保存します。
capture_all_output.yml ファイルに次の内容を追加します。
---
- name: Capture All Types of Script Output
hosts: local
gather_facts: no
tasks:
- name: Execute the info.sh script
command: "{{ playbook_dir }}/scripts/info.sh"
register: script_output
- name: Display standard output
debug:
msg: "Standard Output (stdout):"
- name: Display stdout content
debug:
var: script_output.stdout_lines
- name: Display standard error
debug:
msg: "Standard Error (stderr):"
- name: Display stderr content
debug:
var: script_output.stderr_lines
- name: Display return code
debug:
msg: "Return Code: {{ script_output.rc }}"
このプレイブックは、スクリプトを実行し、次に以下を表示します。
script_output.stdout_lines を使用した標準出力
script_output.stderr_lines を使用した標準エラー
script_output.rc を使用したリターンコード
拡張プレイブックの実行
新しいプレイブックを実行しましょう。
cd ~/project/ansible-output-demo
ansible-playbook -i inventory capture_all_output.yml
3 種類のすべての出力が包括的に表示されるはずです。
PLAY [Capture All Types of Script Output] *****************************
TASK [Execute the info.sh script] *************************************
changed: [localhost]
TASK [Display standard output] ****************************************
ok: [localhost] => {
"msg": "Standard Output (stdout):"
}
TASK [Display stdout content] *****************************************
ok: [localhost] => {
"script_output.stdout_lines": [
"=== System Information ===",
"Hostname: ubuntu",
"Date: Tue Oct 17 12:40:22 UTC 2023",
"Kernel: 5.15.0-1031-aws",
"Memory:",
" total used free shared buff/cache available",
"Mem: 7.7Gi 1.2Gi 5.2Gi 12Mi 1.3Gi 6.3Gi",
"Swap: 0B 0B 0B",
"=== Standard Output ===",
"This is standard output",
"Hello from the script!"
]
}
TASK [Display standard error] *****************************************
ok: [localhost] => {
"msg": "Standard Error (stderr):"
}
TASK [Display stderr content] *****************************************
ok: [localhost] => {
"script_output.stderr_lines": [
"=== Standard Error ===",
"This is standard error",
"An example error message"
]
}
TASK [Display return code] ********************************************
ok: [localhost] => {
"msg": "Return Code: 0"
}
PLAY RECAP **********************************************************
localhost : ok=6 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
これで、スクリプトからのすべての種類の出力を見ることができます。
- 標準出力には、システム情報と通常のメッセージが表示されます。
- 標準エラーには、エラーメッセージが表示されます。
- リターンコードは 0 で、正常な実行を示しています。
エラーのあるスクリプトの作成
エラーを生成し、0 以外の終了コードを返すスクリプトを作成して、Ansible がそれをどのように処理するかを確認しましょう。
scripts ディレクトリに error.sh という名前の新しいファイルを作成します。
- 「File」メニューをクリックします。
- 「New File」を選択します。
scripts/error.sh として保存します。
error.sh ファイルに次の内容を追加します。
#!/bin/bash
## Print some standard output
echo "Starting error demonstration script"
echo "This will appear in stdout"
## Print some standard error
echo "This will appear in stderr" >&2
echo "Error: Something went wrong!" >&2
## Exit with a non-zero code to indicate error
exit 1
スクリプトを実行可能にします。
chmod +x ~/project/ansible-output-demo/scripts/error.sh
次に、このスクリプトを実行してエラーを処理するプレイブックを作成しましょう。 handle_errors.yml という名前の新しいファイルを作成します。
- 「File」メニューをクリックします。
- 「New File」を選択します。
handle_errors.yml として保存します。
handle_errors.yml ファイルに次の内容を追加します。
---
- name: Handle Script Errors
hosts: local
gather_facts: no
tasks:
- name: Execute the error script
command: "{{ playbook_dir }}/scripts/error.sh"
register: script_output
ignore_errors: yes
- name: Display standard output
debug:
var: script_output.stdout_lines
- name: Display standard error
debug:
var: script_output.stderr_lines
- name: Display return code
debug:
msg: "Return Code: {{ script_output.rc }}"
- name: Check if script failed
debug:
msg: "The script failed with return code {{ script_output.rc }}"
when: script_output.rc != 0
ignore_errors: yes が追加されていることに注意してください。これは、コマンドが失敗した場合 (0 以外の終了コードを返した場合) でも、Ansible にプレイブックの実行を続行するように指示します。
このプレイブックを実行しましょう。
ansible-playbook -i inventory handle_errors.yml
次のような出力が表示されるはずです。
PLAY [Handle Script Errors] *******************************************
TASK [Execute the error script] ***************************************
changed: [localhost]
TASK [Display standard output] ****************************************
ok: [localhost] => {
"script_output.stdout_lines": [
"Starting error demonstration script",
"This will appear in stdout"
]
}
TASK [Display standard error] *****************************************
ok: [localhost] => {
"script_output.stderr_lines": [
"This will appear in stderr",
"Error: Something went wrong!"
]
}
TASK [Display return code] ********************************************
ok: [localhost] => {
"msg": "Return Code: 1"
}
TASK [Check if script failed] *****************************************
ok: [localhost] => {
"msg": "The script failed with return code 1"
}
PLAY RECAP **********************************************************
localhost : ok=5 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
この例では、次の方法を示しています。
- エラーを生成するスクリプトからの出力をキャプチャする
- エラーが発生してもプレイブックの実行を続行する
- スクリプトのリターンコードに基づいてタスクを条件付きで実行する
次のステップでは、Ansible でスクリプト出力を操作するための、より高度なユースケースとベストプラクティスを探求します。