Introduction and use of roles, a new feature of Ansible1.2
I. brief introduction
New features introduced after the Ansible1.2 version to organize playbook hierarchically and structurally. Roles can automatically load variable files, tasks and handlers according to the hierarchical structure. To use roles, you only need to use the include instruction in playbook. Simply put, roles is a mechanism that allows you to easily include variables, files, tasks, templates, and processors in separate directories. Roles are generally used in scenarios where services are built based on hosts, but they can also be used in scenarios such as building daemons
II. Roles directory structure
It is officially recommended to use roles under the / etc/ansible/roles directory, but it is not necessary. The roles directory can be created on its own.
Tasks- contains the main list of tasks to be performed by the role, at least a main.yml file, and other yml files are included through include
Handlers- contains handlers (tasks triggered by notify) that can be used by the role or even anywhere outside that role. There should be at least a main.yml file.
The default variable for the defaults- role, at least a main.yml file
Other variables of the vars- role, at least a main.yml file
Files- contains files that can be deployed through this role.
Templates- contains templates that can be deployed through this role.
Meta- defines some metadata for this role, at least a main.yml file
Take the deployment of grafana+influxdb+telegraf as an example
The directory structure is as follows:
In the project directory, the script is at the same level as the roles directory. The roles directory contains various role directories, and each role directory contains the files,handlers,tasks,templates,vars; used. It is best to exclude the meta and defaults directories that are not used.
3. Roles example
Take the deployment of grafana+influxdb+telegraf as an example
3.1. Demand
Deploy a three-piece suite on node2, deploy influxdb on node3, use variables for some parameters in the configuration file, use templates for the configuration file, restart the service after changing the configuration file
3.2.The deployment script deploy.yml----hosts: node2 remote_user: root roles:-grafana-influxdb-telegraf- hosts: node3 remote_user: root roles:-influxdb3.3, role take grafana as an example
Roles/grafana/tasks/main.yml file
-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 profile
# The http port to usehttp_port = {{http_port}}
Roles/grafana/vars/main.yml file
Http_port: 8000
Roles/grafana/handlers/main.yml file
-name: restart grafana systemd: name: grafana-server state: restarted daemon_reload: yes3.4, hosts file / etc/ansible/hosts [node2] 192.168.143.131 [Node3] 192.168.143.132 3.5, execute deploy.ymlansible-playbook deploy.yml