-- Ensure cannot be summarized by the PNG process of the PAC Jinjia experiment.
Roles introduction:
Ansible uses roles to organize playbooks in a hierarchical and structured manner ). Roles can automatically Load Variable files, tasks, and handlers according to the hierarchical structure. Simply put, roles places variables, files, tasks, modules, and processors in separate directories and easily include them, roles is generally used to build services based on hosts, but can also be used to build daemon and other scenarios.
?
To create and use roles in a playbook:
1. Create a directory with the roles command. * Mkdir/etc/ansible/roles/-P # After yum is installed, 2. Create a global variable directory. * Mkdir/etc/ansible/group_vars/-p * Touch/etc/ansible/group_vars/all # file name defined by yourself. Pay attention to 3 When referencing it. in the roles directory, create a directory named by role, such as httpd. * Mkdir/etc/ansible/roles/common-P4. create the directories of files, handlers, tasks, templates, Meta, ults, and vars in the directory of each role command, A directory that is not used can be created as an empty directory, but cannot be created. * Mkdir/etc/ansible/roles/httpd/{files, templates, tasks, handlers, vars, defaults, meta}-p * mkdir/etc/ansible/roles/MySQL/{files, templates, tasks, handlers, vars, defaults, meta}-P5. create main under the handlers, tasks, Meta, defaults, and vars directories of each role. yml files cannot be customized. * Touch/etc/ansible/roles/httpd/{defaults, vars, tasks, Meta, handlers}/main. yml * Touch/etc/ansible/roles/MySQL/{defaults, vars, tasks, Meta, handlers}/main. yml6. in the playbook file, call each role. VI/etc/ansible/site. yml-hosts: webserver remote_user: Root roles:-httpd-MySQL
Meanings of directories in roles:
* Files: used to store files called by the copy module or script module. * Templates: used to store the jinjia2 template. The template module automatically searches for the jinjia2 template file in this directory. * Tasks: This Directory should contain a main. yml file that defines the task list of this role. This file can be used to include other task files located in this directory. Mail. yml default execution Program * handlers: This Directory should contain a main. yml file, used to define the action performed when the trigger conditions in this role. * Vars: This Directory should contain a main. yml file to define the variables used by this role. * Defaults: This Directory should contain a main. yml file to set the default variables for the current role. * Meta: This Directory should contain a main. yml file to define the special settings and dependencies of this role.
Lab project: Use roles to build the lamp Architecture
1. Create the httpd, MySQL, and PHP role name directories, and create files, handlers, tasks, templates, Meta, ults, and vars directories under the directories. Mkdir/etc/ansible/roles/httpd/{files, templates, tasks, handlers, vars, ults, Meta}-pmkdir/etc/ansible/roles/MySQL/{files, templates, tasks, handlers, vars, defaults, Meta}-pmkdir/etc/ansible/roles/PHP/{files, templates, tasks, handlers, vars, ults, meta}-ptouch/etc/ansible/roles/httpd/{defaults, vars, tasks, Meta, handlers}/main. ymltouch/etc/ansible/roles/MySQL/{defaults, vars, tasks, Meta, handlers}/main. ymltouch/etc/ansible/roles/PHP/{defaults, vars, tasks, Meta, handlers}/main. yml
2. compile the httpd module: write a simple tasks/main. yml: * VI/etc/ansible/roles/httpd/tasks/main. yml-Name: Ensure Apache is at the late ST version Yum: PKG ={{ PKG} state = latest definition variable: can be defined in global variables, it can also be defined in the roles role variable, which is generally defined in the role variable * VI/etc/ansible/roles/httpd/vars/main. yml PKG: httpd
3. compile the MySQL module: * VI/etc/ansible/roles/MySQL/tasks/main. yml-Name: Ensure MySQL is at the latest version Yum: PKG ={{ PKG} state = Latest * VI/etc/ansible/roles/MySQL/vars/main. ymlpkg: mariadb *
4. compile the PHP module: * VI/etc/ansible/roles/PHP/tasks/main. yml-Name: Ensure PHP is at the latest version Yum: PKG ={{ PKG} state = Latest * VI/etc/ansible/roles/PHP/vars/main. yml PKG: PHP
5. Compile a roles example: * VI/etc/ansible/lamp. yml --- hosts: webserver remote_user: Root roles:-httpd-mysql-PHP
6. Execute playbook:
7. Compile the PHP test homepage under the httpd site directory on webserver and access it through a browser:
Conclusion: Using ansible's roles function to split all closely connected services can reduce software coupling. This greatly simplifies the deployment and implementation of software projects and facilitates later maintenance, facilitating the promotion and use of projects.
Master ansible role (roles) automated deployment and configuration lamp Architecture