Ansible タスクの実行と管理対象ノードのサンプル

環境
RHEL8.6

概要
ansible.builtin.fail:
when: inventory_hostname == “ホスト名"
必ず 1 つのタスクをすべての管理対象ノードが実行した後に、
次のタスクの実行に移動します。タスクの実行に失敗した管理対象ノードは、
そのタスクで実行対象から除外します。

操作例
ファイル ptest.yml
サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
---
- name: Task execution and managed nodes
hosts: all
gather_facts: no
tasks:
- name: このタスクはすべての管理対象ノードによって実行されるs.
ansible.builtin.debug:
- name: Task execution fails on managed node testa.
ansible.builtin.fail:
when: inventory_hostname == "testa"
- name: This task will be performed by all remaining managed nodes.
ansible.builtin.debug:
- name: Task execution fails on managed node testb.
ansible.builtin.fail:
when: inventory_hostname == "testb"
- name: This task will be further performed by all remaining managed nodes.
ansible.builtin.debug:
--- - name: Task execution and managed nodes hosts: all gather_facts: no tasks: - name: このタスクはすべての管理対象ノードによって実行されるs. ansible.builtin.debug: - name: Task execution fails on managed node testa. ansible.builtin.fail: when: inventory_hostname == "testa" - name: This task will be performed by all remaining managed nodes. ansible.builtin.debug: - name: Task execution fails on managed node testb. ansible.builtin.fail: when: inventory_hostname == "testb" - name: This task will be further performed by all remaining managed nodes. ansible.builtin.debug:
---
- name: Task execution and managed nodes
  hosts: all
  gather_facts: no

  tasks:
    - name: このタスクはすべての管理対象ノードによって実行されるs.
      ansible.builtin.debug:
    
    - name: Task execution fails on managed node testa.
      ansible.builtin.fail:
      when: inventory_hostname == "testa"

    - name: This task will be performed by all remaining managed nodes.
      ansible.builtin.debug:

    - name: Task execution fails on managed node testb.
      ansible.builtin.fail:
      when: inventory_hostname == "testb"

    - name: This task will be further performed by all remaining managed nodes.
      ansible.builtin.debug:

実行結果
$ ansible-playbook -i hosts.yml ptest.yml

Ansible

Posted by arkgame