I. Introduction
New features introduced after Ansible1.2 version for hierarchical, structured organization playbook. roles can be loaded automatically variable according to the hierarchical file structure, tasks and other handlers. To use roles only need to use the include directive in the playbook. Simply put, roles is through variable respectively, files, tasks, templates, and the processor is placed in a separate directory, and can easily include a mechanism for them. Roles are generally used for host-based building services scenario, but can also be used to build daemons scene
Two, roles directory structure
The official recommended roles in / etc / ansible / roles directory, but not necessarily, you can create your own roles directory
main tasks- list contains the task you want to perform the role, at least there should be main.yml documents, there are other files yml include conduct that contains
handlers- contains handler (task notify triggered), the role even anywhere outside of that role can use these handlers. There should be at least main.yml file
default variable defaults- role, at least there should be main.yml file
other variables vars- role, at least there should be main.yml file
files- files can contain this role deployment.
templates- contains templates can be deployed by this role.
meta- define some of the metadata for this role, at least there should be main.yml file
To deploy grafana + influxdb + telegraf an example
directory structure is as follows:
in the project directory, and plays roles same level directory, the directory contains the roles directory roles, each role to use directory contains files, handlers, tasks, templates, vars ; for meta directory and defaults are not used, it is best to exclude.
Three, roles examples
To deploy grafana + influxdb + telegraf A Case Study
3.1 Demand
Once deployed on node2 three-piece, deploy influxdb on node3, some parameters in the configuration file using variables, configuration files using the template, change the configuration file to restart the service
3.2, the deployment script deploy.yml
---
- hosts: node2
remote_user: root
roles:
- grafana
- influxdb
- telegraf
- hosts: node3
remote_user: root
roles:
- influxdb
3.3, for example role to grafana
Roles / grafana / Tasks / main.yml 文件
---
- name: "copy grafana to destination server"
copy:
src: grafana-6.3.0-1.x86_64.rpm
dest: /tmp/
- name: "Install the grafana rpm package locally"
yum:
name: /tmp/grafana-6.3.0-1.x86_64.rpm
state: present
- name: "template the grafana.ini"
template:
src: grafana.ini.j2
dest: /etc/grafana/grafana.ini
notify: restart grafana
- name: "enable grafana"
systemd:
name: grafana-server
state: restarted
daemon_reload: yes
enabled: yes
roles / grafana / templates / grafana.ini.j2 template configuration file
# The http port to use
http_port = {{ http_port }}
roles / grafana / vars / main.yml 文件
http_port: 8000
Roles / grafana / handlers / main.yml 文件
- name: restart grafana
systemd:
name: grafana-server
state: restarted
daemon_reload: yes
3.4, hosts file
/etc/ansible/hosts
[node2]
192.168.143.131
[node3]
192.168.143.132
3.5, the implementation of deploy.yml
ansible-playbook deploy.yml
Reference: Marco ansible video