Ansible Documentation
fail – Fail with custom message機能
タスクを失敗させ、指定したメッセージを表示するパラメータ
| パラメータ | 選択肢/ Default | 説明 |
|---|---|---|
| msg | "Failed as requested from task" | タスクの失敗時に表示するメッセージ |
例
---
- hosts: all
tasks:
- name: OS が RedHat 系なら処理を中断する
fail:
msg: "RedHat 系は処理を中断します → {{ inventory_hostname }} / {{ ansible_facts['distribution'] }}"
when: ansible_facts['os_family'] == "RedHat"
- name: 非 RedHat 系の処理
debug:
msg: "非 RedHat 系 → {{ inventory_hostname }} / {{ ansible_facts['distribution'] }} / {{ ansible_facts['os_family'] }}"
実行結果
ansibleman@ubuntu-pc:~/ansible/fail$ ansible-playbook -i hosts.yml site.yml
PLAY [all] *********************************************************************************************************
TASK [Gathering Facts] *********************************************************************************************
ok: [node-c0706]
ok: [node-u1804]
TASK [OS が RedHat 系なら処理を中断する] **************************************************************************************
fatal: [node-c0706]: FAILED! => {"changed": false, "msg": "RedHat 系は処理を中断します → node-c0706 / CentOS"}
skipping: [node-u1804]
TASK [非 RedHat 系の処理] ***********************************************************************************************
ok: [node-u1804] => {
"msg": "非 RedHat 系 → node-u1804 / Ubuntu / Debian"
}
PLAY RECAP *********************************************************************************************************
node-c0706 : ok=1 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
node-u1804 : ok=2 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
ansibleman@ubuntu-pc:~/ansible/fail$