CentOS 8
Sponsored Link

Ansible : Use Playbook (Roles)2020/03/17

 
This is the example of using the [Roles] feature.
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 this example.

+--- 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] This is the Playbook to use Roles that MariaDB and Nginx are installed and started.
[cent@dlp ~]$
mkdir -p roles/ins_mariadb/{tasks,vars}

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

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

[cent@dlp ~]$
vi roles/ins_mariadb/vars/main.yml
mariadb_package:
  - mariadb-server
  - python3-PyMySQL
mariadb_root_password: "P@ssw0rd01"

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

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

- name: Set Mariadb root password
  mysql_user:
    name: "root"
    password: "{{ mariadb_root_password }}"
    host: "{{ item }}"
  with_items:
    - "127.0.0.1"
    - "::1"
    - "localhost"

- name: Delete user
  mysql_user:
    user: "root"
    login_password: "{{ mariadb_root_password }}"
    host: "{{ ansible_fqdn }}"
    state: "absent"

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

[cent@dlp ~]$
vi roles/ins_nginx/tasks/main.yml
- name: nginx is installed
  dnf:
    name: "{{ nginx_package }}"
    state: installed
  tags: install_nginx

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

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

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

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

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

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

TASK [ins_mariadb : Set Mariadb root password] ****************************
changed: [10.0.0.51] => (item=127.0.0.1)
changed: [10.0.0.52] => (item=127.0.0.1)
changed: [10.0.0.52] => (item=::1)
changed: [10.0.0.51] => (item=::1)
changed: [10.0.0.52] => (item=localhost)
changed: [10.0.0.51] => (item=localhost)

TASK [ins_mariadb : Delete user] ***********************************************
changed: [10.0.0.51]
changed: [10.0.0.52]

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

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

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

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

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

PLAY RECAP *********************************************************************
10.0.0.51                  : ok=10   changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
10.0.0.52                  : ok=10   changed=8    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
Matched Content