Ansible @ an efficient configuration management tool-ansible configure management-translation (8)

Source: Internet
Author: User
Tags vars
If you do not have written authorization, do not repeat Chapter 4. Use of ansible in large projects

RolesIf your playbooks start expanding beyond what includes can help you solve, or youstart gathering a large number of templates, you may want to use roles. Roles inAnsible allow you to group files together in a defined format. They are essentiallyan extension to includes that handles a few things automatically, and this helps youorganize them inside your repository.Roles allows you to place your variables, files, tasks, templates, and handlers in afolder, and then easily include them. You can also include other roles from withinroles, which effectively creates a tree of dependencies. Similar to task includes, theycan have variables passed to them. Using these features, you should be able to buildself-contained roles that are easy to share with others.Roles are commonly set up to be services provided by machines, but they can also bedaemons, options, or simply characteristics. Things you may want to configure in arole are as follows:?     Webservers, such as Nginx or Apache?     Messages of the day customized for the security level of the machine?     Database servers running PostgreSQL or MySQLTo manage roles in Ansible perform the following steps:1.     Create a folder named roles with your playbooks.2.     In the roles folder, make a folder for each role that you would like.3.     In the folder for each role, make folders named files , handlers , meta ,tasks , templates , and finally vars . If you aren't going to use all these,you can leave the ones you don't need off. Ansible will silently ignore anymissing files or directories when using roles.4.     In your playbooks, add the keyword roles followed by a list of roles that youwould like to apply to the hosts.5.     For example, if you had the common , apache , website1 , and website2 roles,your directory structure would look similar to the following example. Thesite.yml file is for reconfiguring the entire site, and the webservers1.ymland webservers2.yml files are for configuring each web server farm.

Role

If your playbook grows to include, it cannot be solved, or you already have a large number of templates, you may have to use the role. It allows you to group files according to the defined format. In essence, it is an automated feature that can help you organize your database.

The role allows you to put variables, files, tasks, templates, and handlers in a folder and then include them. After establishing a valid dependency, you can also include another role in a role. Like include, you can pass variables to the role. With these features, you can create a self-contained role and easily share it with others.

Roles are commonly set up to be services provided by machines, but they can also be daemons, options, or simply Characteristics)

You can configure a role as follows:

  • Web services, such as nginx or Apache
  • Messages of the day customized for the security level of the machine
  • PostgreSQL or MySQL

Create a role

  1. Create a folder named roles
  2. In the roles folder, create a folder for each role you want to define
  3. In the created role folder, create files, handlers, Meta, tasks, templates, and vars. If you do not need them for the time being, ansible will automatically ignore them.
  4. In playbook, add the roles keyword. Its value is the list of hosts that you want this role to apply.
  5. For example, if you have common, Apache, website1, and website2 roles, your directory should be like the example below. Site, yml is used to configure the entire site, webservers1, yml, and webservers2.yml are used to configure their respective servers.


The following file is what could be in website1.yml . It shows a playbook thatapplies the common , apache , and website1 roles to the website1 group in theinventory. The website1 role is included using a more verbose format thatallows us to pass variables to the role:---- name: Setup servers for website1.example.comhosts: website1roles:- common- apache- { role: website1, port: 80 }For the role named common , Ansible will then try to load roles/common/tasks/main.yml as a task include, roles/common/handlers/main.yml as a handlerinclude, and roles/common/vars/main.yml as a variable file include. If all of thesefiles are missing, Ansible will throw an error; however, if one of the files exists thenthe others, if missing, will be ignored. The following directories are used by a defaultinstall of Ansible. Other directories may be used by different modules:
The following file is website1.yml. It shows how a playbook applies the common. Apache and website1 roles to the website1 group in the device list. The website1 role contains more and more detailed formats, allowing us to pass parameters to the role.

---
-Name: Setup servers for website1.example.com
Hosts: website1
Roles:
-Common
-Apache
-{Role: website1, Port: 80}

When the common role is processed, ansible loads main. yml is included as a task and loaded with roles/common/handlers/main. the yml file is included as handlers and loaded with roles/common/vars/main. yml is included as a variable. However, when some files exist and some files do not exist, non-existing files will be ignored. The following is the default installation directory of ansible, which is commonly used by other modules:

When using roles, the behavior of the copy, the template, and the script modules isslightly altered. Instead of searching for files by looking from the directory in whichthe playbook file is located, Ansible will look for the files in the location of the role.For example, if you are using a role named common , these modules will change to thefollowing behavior:? The copy module will look for files in roles/common/files .? The template module will look for templates in roles/common/templates .? The script module will look for files in roles/common/files .? Other modules may decide to look for their data in other folders insideroles/common/ . The documentation for modules can be retrieved usingansible-doc , as was discussed in the Module help section of Chapter 1, GettingStarted with Ansible.

When a role is used, there are other modules such as copy and template, and their behavior will change. Originally, these modules will search for files in the directory where the playbook file is located, and now they will search for files in the directory where the role is located. For example, you are using a common role:

  • The copy module searches for files in roles/common/files.
  • Template Mo will meet in roles/common/templates to find files
  • Other script modules will find files in roles/common/files.
  • Other modules may search for files in roles/common/. The HELP command of the module is ansible-Doc.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.