ansible Role Ansilbe new features introduced from version 1.2, for hierarchical, structured organization playbook;roles to automatically load variable files, tasks, and handlers based on hierarchical structure.to use roles, you only need to use the include directive in playbook. simply put, roles is a mechanism by which variables, files, tasks, modules, and processors are placed in separate directories and easily include them. Roles are typically used in scenarios based on host build services, but can also be used in scenarios such as building daemons. #创建role的步骤(1) Create a directory named after the roles, (2) in the roles directory, respectively, create a directory named after each role name, such as webservers, etc., (3) Create files, handlers, meta, tasks, Templates and VARs directories;to the directory can be created as an empty directory, or can not be created;(4) In the Playbook file, call each role;#role内各目录中可用的文件Tasks: You should include at least one file named Main.yml that defines the task list for this role, which you can use include contains other task files that are located in this directory;Files: Storing files that are called by a module such as copy or script;Templates: The template module will automatically look for JINJA2 templates files in this directory;handlers: This directory should contain a MAIN.YML file that defines the handler used for this role; in handlerThe other handler files that are included in the use of include should also be located in this directory;VARs: A main.yml file should be included to define the variables used by this role;Meta: A main.yml file should be included to define the special settings and dependencies of this role;default: Use this directory when setting default variables for the current role; a main.yml file should be included;
Example: Defining a simple rolescreate a related directory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
[Email protected] ~]# mkdir-pvansible_playbooks/roles/{webserver,dbserver}/{tasks,files,templates,meta,handlers, VARs [Email protected] ~]# tree ansible_playbooks/ ansible_playbooks/ └──roles ├──dbserver │├──files │├──handlers │├──meta │├──tasks │├──templates │└──vars └──webserver ├──files ├──handlers ├──meta ├──tasks ├──templates └──vars
Directories, 0 files |
webserver Creating a configuration file
1 |
[Email protected] ~]# cp/etc/httpd/conf/httpd.confansible_playbooks/roles/webserver/files/ |
under the Tasks directory, define YML, where you only need to define the tasks that are performed
<ignore_js_op style= "Word-wrap:break-word; margin:0px; padding:0px; Color:rgb (68, 68, 68); Font-family:tahoma, ' Microsoft Yahei ', SimSun; widows:1; Background-color:rgb (255, 255, 255); " >
under the Handlers directory, define the handlers
<ignore_js_op style= "Word-wrap:break-word; margin:0px; padding:0px; Color:rgb (68, 68, 68); Font-family:tahoma, ' Microsoft Yahei ', SimSun; widows:1; Background-color:rgb (255, 255, 255); " >
in the directory name sibling directory, define roles, just specify the running host and the role that the user identity has been called.
<ignore_js_op style= "Word-wrap:break-word; margin:0px; padding:0px; Color:rgb (68, 68, 68); Font-family:tahoma, ' Microsoft Yahei ', SimSun; widows:1; Background-color:rgb (255, 255, 255); " >
Execution Results
<ignore_js_op style= "Word-wrap:break-word; margin:0px; padding:0px; Color:rgb (68, 68, 68); Font-family:tahoma, ' Microsoft Yahei ', SimSun; widows:1; Background-color:rgb (255, 255, 255); " >
define multiple roles to achieve reuse effectsExample: Configure the Web service in 4.101, configure the database in 4.102, and configure the database in 4.103 and config web;
copy mysql configuration file
1 |
[Email protected] ~]# cp/etc/my.cnfansible_playbooks/roles/dbserver/files/ |
Define Tasks
<ignore_js_op style= "Word-wrap:break-word; margin:0px; padding:0px; Color:rgb (68, 68, 68); Font-family:tahoma, ' Microsoft Yahei ', SimSun; widows:1; Background-color:rgb (255, 255, 255); " >
Define Handlers
<ignore_js_op style= "Word-wrap:break-word; margin:0px; padding:0px; Color:rgb (68, 68, 68); Font-family:tahoma, ' Microsoft Yahei ', SimSun; widows:1; Background-color:rgb (255, 255, 255); " >
define SITE.YML, configure the Web service in 4.101, configure the database in 4.102, and configure the Web in 4.103 and configure the database.
<ignore_js_op style= "Word-wrap:break-word; margin:0px; padding:0px; Color:rgb (68, 68, 68); Font-family:tahoma, ' Microsoft Yahei ', SimSun; widows:1; Background-color:rgb (255, 255, 255); " >
Execution Result:
<ignore_js_op style= "Word-wrap:break-word; margin:0px; padding:0px; Color:rgb (68, 68, 68); Font-family:tahoma, ' Microsoft Yahei ', SimSun; widows:1; Background-color:rgb (255, 255, 255); " >
From for notes (Wiz)
3.3 Playbook Roles