FreeBSD 14
Sponsored Link

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

 

Ansible の Roles 機能の利用例です。
Roles は Ansible であらかじめ定義された Playbook の命名規則・ファイル配置に従うことで、自動的にインクルードを実行する機能です。

具体的には以下のような構成 (左図) になります。
[playbook.yml], [role01] は任意の名前で OK ですが、それ以外は固定となります。
当例では、以下のような構成 (右図) の Roles 適用 Playbook を作成します。

+--- playbook.yml                  +--- playbook_sample.yml
|                                  |
+--- roles/                        +--- roles/
     |                                  |
     +--- role01/                       +--- ins_mariadb/
          |                             |    |
          +--- files/                   |    +--- vars/
          |                             |    |    |
          +--- templates/               |    |    +--- main.yml
          |                             |    +--- tasks/
          +--- tasks/                   |         |
          |                             |         +--- main.yml
          +--- handlers/                |
          |                             +--- ins_nginx/
          +--- vars/                              |
          |                                       +--- files/
          +--- defaults/                          |    |
          |                                       |    +--- index.html
          +--- meta/                              +--- vars/
                                                  |    |
                                                  |    +--- main.yml
                                                  |
                                                  +--- tasks/
                                                       |
                                                       +--- main.yml

[1] Nginx と MariaDB をインストールして起動する Roles 適用 Playbook です。
freebsd@dlp:~ $
mkdir -p roles/ins_mariadb/tasks

freebsd@dlp:~ $
mkdir -p roles/ins_mariadb/vars

freebsd@dlp:~ $
mkdir -p roles/ins_nginx/files

freebsd@dlp:~ $
mkdir -p roles/ins_nginx/tasks

freebsd@dlp:~ $
mkdir -p roles/ins_nginx/vars

freebsd@dlp:~ $
vi playbook_sample.yml
- hosts: target_servers
  become: yes
  become_method: sudo
  roles:
    - ins_mariadb
    - ins_nginx

freebsd@dlp:~ $
vi roles/ins_mariadb/vars/main.yml
mariadb_package:
  - mariadb1011-server
  - py311-pymysql

freebsd@dlp:~ $
vi roles/ins_mariadb/tasks/main.yml
- name: mariadb is installed
  pkgng:
    name: "{{ mariadb_package }}"
    state: present
  tags: install_mariadb

- name: mariadb is running and enabled
  service:
    name: mysql-server
    state: started
    enabled: yes

freebsd@dlp:~ $
vi roles/ins_nginx/vars/main.yml
nginx_package:
  - nginx

freebsd@dlp:~ $
vi roles/ins_nginx/tasks/main.yml
- name: nginx is installed
  pkgng:
    name: "{{ nginx_package }}"
    state: present
  tags: install_nginx

- name: edit nginx.default
  lineinfile:
    path: /usr/local/etc/nginx/nginx.conf
    regexp: "^ *server_name  localhost;"
    line: "server_name {{ ansible_fqdn }};"
  tags: edit_nginx.default

- name: nginx is running and enabled
  service:
    name: nginx
    state: started
    enabled: yes

- name: put index.html
  copy:
    src: index.html
    dest: /usr/local/www/nginx
    owner: root
    group: wheel
    mode: 0644

- name: check nginx
  uri:
    url: http://{{ ansible_fqdn }}

freebsd@dlp:~ $
echo "nginx index page" > roles/ins_nginx/files/index.html
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 [ins_mariadb : mariadb is installed] ********************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

TASK [ins_mariadb : mariadb is running and enabled] **********************************
ok: [10.0.0.52]
ok: [10.0.0.51]

TASK [ins_nginx : nginx is installed] ************************************************
ok: [10.0.0.51]
ok: [10.0.0.52]

TASK [ins_nginx : edit nginx.default] ************************************************
changed: [10.0.0.52]
changed: [10.0.0.51]

TASK [ins_nginx : nginx is running and enabled] **************************************
ok: [10.0.0.51]
ok: [10.0.0.52]

TASK [ins_nginx : put index.html] ****************************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

TASK [ins_nginx : check nginx] *******************************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

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