FreeBSD 14
Sponsored Link

Ansible : Playbook を利用する (include)2024/09/11

 

タスクや Playbook は他からインクルードすることができます。

[1] タスクを他からインクルードする場合は、[tasks] 内に [include: ***] と記述します。
freebsd@dlp:~ $
vi playbook_sample.yml
# [tasks] ディレクトリ配下の [included.yml] をインクルード

- hosts: target_servers
  become: yes
  become_method: sudo
  tasks:
    - include_tasks: tasks/included.yml

freebsd@dlp:~ $
mkdir tasks

freebsd@dlp:~ $
vi tasks/included.yml
# タスクの部分のみの記述で OK

- name: General packages are installed
  pkgng:
    name: "{{ packages }}"
    state: present
  vars:
    packages:
    - curl
    - wget
    - unzip
  tags: General_Packages

freebsd@dlp:~ $
ansible-playbook playbook_sample.yml --ask-become-pass

BECOME password:

PLAY [target_servers] *******************************************************************

TASK [Gathering Facts] ******************************************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

TASK [include_tasks] ********************************************************************
included: /home/freebsd/tasks/included.yml for 10.0.0.51, 10.0.0.52

TASK [General packages are installed] ***************************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

PLAY RECAP ******************************************************************************
10.0.0.51                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
10.0.0.52                  : ok=3    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
[2] Playbook を他からインクルードする場合は、[import_playbook] を利用します。
[1] の例に加えて、[httpd] の起動状態を保つ Playbook をインクルードします。
freebsd@dlp:~ $
vi playbook_sample.yml
- hosts: target_servers
  become: yes
  become_method: sudo
  tasks:
    - include_tasks: tasks/included.yml
# 他の Playbook をインクルード
- import_playbook: httpd.yml

freebsd@dlp:~ $
vi httpd.yml
# 通常の Playbook と同様の書式で記述する

- hosts: target_servers
  become: yes
  become_method: sudo
  tasks:
  - name: apache httpd is installed
    pkgng:
      name: apache24
      state: present
  - name: apache httpd is running and enabled
    service:
      name: apache24
      state: started
      enabled: yes

freebsd@dlp:~ $
ansible-playbook playbook_sample.yml --ask-become-pass

BECOME password:

PLAY [target_servers] *********************************************************************

TASK [Gathering Facts] ********************************************************************
ok: [10.0.0.51]
ok: [10.0.0.52]

TASK [include_tasks] **********************************************************************
included: /home/freebsd/tasks/included.yml for 10.0.0.51, 10.0.0.52

TASK [General packages are installed] *****************************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

PLAY [target_servers] *********************************************************************

TASK [Gathering Facts] ********************************************************************
ok: [10.0.0.51]
ok: [10.0.0.52]

TASK [apache httpd is installed] **********************************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

TASK [apache httpd is running and enabled] ************************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

PLAY RECAP ********************************************************************************
10.0.0.51                  : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
10.0.0.52                  : ok=6    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
関連コンテンツ