Ansible include Playbook を利用するサンプル

環境
Ubuntu 24.04

概要
includeを使ってタスクや Playbook は他からインクルードすることができます。

1.タスクを他からインクルードする場合は、[tasks] 内に [include: ***] と記述します。
$ vi test.yml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# [tasks] ディレクトリ配下の [included.yml] をインクルード
- hosts: target_servers
become: yes
become_method: sudo
tasks:
- include_tasks: tasks/included.yml
# [tasks] ディレクトリ配下の [included.yml] をインクルード - hosts: target_servers become: yes become_method: sudo tasks: - include_tasks: tasks/included.yml
# [tasks] ディレクトリ配下の [included.yml] をインクルード
- hosts: target_servers
  become: yes
  become_method: sudo
  tasks:
    - include_tasks: tasks/included.yml

$ mkdir tasks
$ vi tasks/included.yml

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# タスクの部分のみの記述
- name: General packages are installed
apt:
name: "{{ packages }}"
state: present
vars:
packages:
- tar
- wget
- unzip
tags: General_Packages
# タスクの部分のみの記述 - name: General packages are installed apt: name: "{{ packages }}" state: present vars: packages: - tar - wget - unzip tags: General_Packages
# タスクの部分のみの記述
- name: General packages are installed
  apt:
    name: "{{ packages }}"
    state: present
  vars:
    packages:
    - tar
    - wget
    - unzip
  tags: General_Packages

playbookを実行

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ ansible-playbook test.yml --ask-become-pass
$ ansible-playbook test.yml --ask-become-pass
$ ansible-playbook test.yml --ask-become-pass

2.Playbook を他からインクルードする場合は、[import_playbook] を利用します。
[httpd] の起動状態を保つ Playbook をインクルードします。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ vi test.yml
- hosts: target_servers
become: yes
become_method: sudo
tasks:
- include_tasks: tasks/included.yml
# 他の Playbook をインクルード
- import_playbook: httpd.yml
$ vi test.yml - hosts: target_servers become: yes become_method: sudo tasks: - include_tasks: tasks/included.yml # 他の Playbook をインクルード - import_playbook: httpd.yml
$ vi test.yml
- hosts: target_servers
  become: yes
  become_method: sudo
  tasks:
    - include_tasks: tasks/included.yml
# 他の Playbook をインクルード
- import_playbook: httpd.yml

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

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
- hosts: target_servers
become: yes
become_method: sudo
tasks:
- name: apache2 is installed
apt:
name: apache2
state: present
- name: apache2 is running and enabled
service:
name: apache2
state: started
enabled: yes
- hosts: target_servers become: yes become_method: sudo tasks: - name: apache2 is installed apt: name: apache2 state: present - name: apache2 is running and enabled service: name: apache2 state: started enabled: yes
- hosts: target_servers
  become: yes
  become_method: sudo
  tasks:
  - name: apache2 is installed
    apt:
      name: apache2
      state: present
  - name: apache2 is running and enabled
    service:
      name: apache2
      state: started
      enabled: yes

playbookを実行する

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
$ ansible-playbook test.yml --ask-become-pass
$ ansible-playbook test.yml --ask-become-pass
$ ansible-playbook test.yml --ask-become-pass

 

IT

Posted by arkgame