CentOS 7
Sponsored Link

Ansible : Use Playbook#62015/07/15

 
This is the example of using the "Roles" function.
To use Roles, it's possible to include other tasks or Playbooks without writing "include" sentence.
It's necessary to configure directory tree like follows to use Roles.
The left image shows all tree, the right image shows an example of a Playbook on here.

+--- 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] For example, create a Playbook which install Python modules and httpd.
[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
Matched Content