Roles is similar to the State in the salt-stack, and the State has a certain organizational structure.
Roles is the playbooks directory structure in ansible.
What are the advantages of this division? There are more advantages. If you write everything into playbooks,
This may cause our playbooks to be bloated and should not be read. After modularization, roles has become an organizational structure, easy to read, reusable code, and clear hierarchy.
Let's take a look at a small directory structure. The main user did the test on his own, and the content was all blind. It was totally different from the actual environment.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/37/F7/wKiom1OxY9Wyt1lZAAFEk2CIC6k694.jpg "Title =" mulu.png "alt =" wkiom1oxy9wyt1lzaafek2cic6k694.jpg "/>
The first level directory contains two folders and two files.
Group_vars the group variables stored here are the same as the group variable definition rules in/etc/ansible/group_vars. The salt file under groups_vars. the variables in this file are only valid for the salt group... If the file name is all, it is valid for all groups on all hosts. Compared with roles, the variables here are global.
[email protected]:~/ansible_script/web$ cat group_vars/salt http_port: 80
Hosts stores host and group information.
[email protected]:~/ansible_script/web$ cat hosts [salt]salt-master
MySQL and webservers under the roles directory are obviously two role.
Directories such as MySQL and webservers can contain these directories.
Files: Save the file and drop the file into this directory. ansible will find the file in this directory by default, corresponding to the copy module in the task.
Tasks: it apparently stores tasks.
Handlers: stores handlers
Templates: storage template, corresponding to the module template in the task
Vars: The variable defined here, which only applies to the current role.
Meta: Define the direct dependency between role and role.
Let's first check what is in the webserver folder.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/37/F7/wKioL1OxY1azM6V_AAPOkkFG6oE307.jpg "Title =" apache.png "alt =" wkiol1oxy1azm6v_aapokkfg6oe307.jpg "/>
Let's take a look at the things in the MySQL folder.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/37/F7/wKiom1OxYj7gyjogAAM5pBGUUvM496.jpg "Title =" mysql.png "alt =" wkiom1oxyj7gyjogaam5pbguuvm496.jpg "/>
The site. yml file is the file we want to call.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/37/F7/wKioL1OxZ8OBp0jZAABbCmuYgZ4479.jpg "Title =" site.png "alt =" wkiol1oxz8obp0jzaabbcmuygz4479.jpg "/>
Let's take a look at the execution results.
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M00/37/F8/wKiom1OxaUfhe27WAASnOa5-6mA978.jpg "Title =" jieguo.png "alt =" wKiom1OxaUfhe27WAASnOa5-6mA978.jpg "/>
I think everyone can see the tags label defined in tasks. How can I use tags in roles?
See
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/37/F7/wKioL1Oxae6w9s6jAABqY8N3GCM369.jpg "Title =" site.png "alt =" wkiol1oxae6w9s6jaabqy8n1_cmw..jpg "/>
Run it. There are the following results!
650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/37/F8/wKiom1OxakqQtRy9AARt6etBneU886.jpg "Title =" jieguo.png "alt =" wkiom1oxakqqtry9aart6etbneu886.jpg "/>
OK. I'm going home from work.
This article is from the "West Wind" blog, please be sure to keep this source http://lixcto.blog.51cto.com/4834175/1432722