CentOS 7
Sponsored Link

Ansible : Playbook を利用する#62015/07/15

 
Ansible の Roles 機能の利用例です。
Roles は Ansible であらかじめ定義された Playbook の命名規則・ファイル配置に従うことで、 自動的にインクルードを実行する機能です。
具体的には以下のような構成(左図)になります。
playbook.yml, role01 は任意の名前で OK ですが、それ以外は固定となります。
ここでは例として、以下のような構成(右図) の Roles 適用 Playbook を作成します。

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

[1] 必要な Python モジュール と httpd をインストール & 起動する Roles 適用 Playbook です。
[cent@dlp ~]$
mkdir -p roles/ins_python_lib/{tasks,vars}

[cent@dlp ~]$
mkdir -p roles/ins_httpd/{files,tasks,vars}

[cent@dlp ~]$
vi playbook_sample.yml
- hosts: target_servers
  become: yes
  become_method: sudo
  roles:
    - ins_python_lib
    - ins_httpd

[cent@dlp ~]$
vi roles/ins_python_lib/vars/main.yml
setuptools:
  - python-setuptools

py_pip:
  - pip

py_libs:
  - httplib2

[cent@dlp ~]$
vi roles/ins_python_lib/tasks/main.yml
- name: setuptools is installed
  yum: name="{{ item }}" state=installed
  with_items:
    - "{{ setuptools }}"
  tags: install_setuptools

- name: pip is installed
  easy_install: name="{{ item }}"
  with_items:
    - "{{ py_pip }}"
  tags: install_pip

- name: httplib2 are installed
  pip: name="{{ item }}"
  with_items:
    - "{{ py_libs }}"
  tags: install_httplib2

[cent@dlp ~]$
vi roles/ins_httpd/vars/main.yml
packages:
  - httpd

[cent@dlp ~]$
vi roles/ins_httpd/tasks/main.yml
- name: httpd is installed
  yum: name="{{ item }}" state=installed
  with_items:
    - "{{ packages }}"
  tags: install_httpd

- name: edit httpd.conf
  lineinfile: >
    dest=/etc/httpd/conf/httpd.conf
    regexp="{{ item.regexp }}"
    line="{{ item.line }}"
  with_items:
  - { regexp: "^#ServerName", line: "ServerName {{ ansible_fqdn }}:80" }
  tags: edit_httpd.conf

- name: httpd is running and enabled
  service: name=httpd state=started enabled=yes

- name: put index.html
  copy: src=index.html dest=/var/www/html owner=root group=root mode=0644

- name: check httpd
  uri: url=http://"{{ ansible_fqdn }}"

[cent@dlp ~]$
echo "httpd index page" > roles/ins_httpd/files/index.html
[cent@dlp ~]$
ansible-playbook playbook_sample.yml --ask-become-pass

SUDO password:

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

GATHERING FACTS ***************************************************************
ok: [10.0.0.51]
ok: [10.0.0.52]

TASK: [ins_python_lib | setuptools is installed] ******************************
ok: [10.0.0.51] => (item=python-setuptools)
changed: [10.0.0.52] => (item=python-setuptools)

TASK: [ins_python_lib | pip is installed] *************************************
ok: [10.0.0.51] => (item=pip)
changed: [10.0.0.52] => (item=pip)

TASK: [ins_python_lib | httplib2 are installed] *******************************
ok: [10.0.0.51] => (item=httplib2)
changed: [10.0.0.52] => (item=httplib2)

TASK: [ins_httpd | httpd is installed] ****************************************
changed: [10.0.0.51] => (item=httpd)
changed: [10.0.0.52] => (item=httpd)

TASK: [ins_httpd | edit httpd.conf] *******************************************
changed: [10.0.0.51] => (item={'regexp': '^#ServerName', 'line': u'ServerName node01.srv.world:80'})
changed: [10.0.0.52] => (item={'regexp': '^#ServerName', 'line': u'ServerName node02.srv.world:80'})

TASK: [ins_httpd | httpd is running and enabled] ******************************
changed: [10.0.0.51]
changed: [10.0.0.52]

TASK: [ins_httpd | put index.html] ********************************************
changed: [10.0.0.52]
changed: [10.0.0.51]

TASK: [ins_httpd | check httpd] ***********************************************
ok: [10.0.0.52]
ok: [10.0.0.51]

PLAY RECAP ********************************************************************
10.0.0.51                  : ok=9    changed=7    unreachable=0    failed=0
10.0.0.52                  : ok=9    changed=7    unreachable=0    failed=0
関連コンテンツ