When a single playbook file becomes larger, we need to reorganize the playbooks. We can split a large playbook into several small playbook files, and then include them in the main configuration file, which is called the inclusion of a playbook. We can also follow certain rules to carry out a certain type of task in a directory, and in this directory again to the playbook according to Tasks,handlers,files,templates,vars and other types into several files, The corresponding file is stored in the corresponding directory, this organization is called Playbook's roles.
First, the inclusion of playbook
The inclusion of playbook is actually the use of the Include keyword
1. Tasks include
Example 1:
A task file Foo.yml example is as follows:
# possibly saved as Tasks/foo.yml
-Name:placeholder foo
Command:/bin/foo
-Name:placeholder Bar
Command:/bin/bar
Include foo.yml in another task file Bar.yml:
Tasks
-Include:tasks/foo.yml
You can also bring variables into the include:
Tasks
-Include:wordpress.yml User=timmy
-Include:wordpress.yml User=alice
-Include:wordpress.yml User=bob
The variables are brought into the following ways:
Tasks
-{include:wordpress.yml, user:timmy, Ssh_keys: [' keys/one.txt ', ' Keys/two.txt ']}
Give one more example:
Tasks:-Include:wordpress.yml vars:remote_user:timmy some_list_variable:-alpha- Beta-gamma
2. Handlers contains
Handlers contains broadly similar to the inclusion of tasks, giving examples directly:
HANDLERS1.YML content is as follows:
# This might is in a file like Handlers/handlers.yml
-Name:restart Apache
Service:name=apache state=restarted
HANDLERS.YML contains handlers1.yml examples:
Handlers:
-Include:handlers/handlers.yml
3. Mixed inclusions
The include can also be used to import one playbook into another playbook:
-Name:this is a play on the top level of a file
Hosts:all
Remote_user:root
Tasks
-Name:say Hi
Tags:foo
Shell:echo "Hi ..."
-Include:load_balancers.yml
-Include:webservers.yml
-Include:dbservers.yml
Ii. Role (roles)
1. Create role
The steps to create a role are as follows:
1) Create a directory with the roles command
2) Create a directory named for the role name in the roles directory, such as Websrvs, etc.
3) files, handlers, meta, tasks, teamplates, and VARs directories are created in each role-named directory, and directories that are not available can be created as empty directories or created.
4) in the Playbook file, call each role
Roles file organization Structure example:
group_vas/
Site.yml
Webservers.yml
roles/
common/
files/
templates/
tasks/
handlers/
vars/
defaults/
meta/
webservers/
files/
templates/
tasks/
handlers/
vars/
defaults/
meta/
Roles the role of each directory and the files available:
Files: Storing a file that is called by a module such as copy or script
Tempaltes:jinja2 template file
Tasks: You should include at least one file named Main.yml that defines the task list for this role, which can use include to include other task files located in this directory
Handlers: Contains at least one main.yml file that defines the various handler used by this role, and the other handler files included with the include in handler should also be located in this directory
VARs: You should include a main.yml file that defines the variables used by this role
Meta: A main.yml file should be included to define the special settings and dependencies for this role
Default: Use some directories when you set defaults for the current role, including a main.yml file
2. Quoting roles
Basic Reference methods:
-Hosts:webservers
Roles
-Common
-Webserver
You can also bring in variables when referenced in the following ways:
-Hosts:webservers
Roles
-Common
-{role:foo_app_instance, dir: '/opt/a ', port:5000}
-{role:foo_app_instance, dir: '/opt/b ', port:5001}
You can also use conditional statements when referencing:
-Hosts:webservers
Roles
-{role:some_role, when: "ansible_os_family = = ' RedHat '"}
Here is an example of a variable that is brought in:
-Hosts:webservers
Roles
-Role:database
database_name: {{db_name}}
Database_user: {{Db_pass}}
-Role:webserver
Live_hostname:web1
Domains
-example.com
-www.example.com
3, Pre_tasks and Post_tasks
If you need to perform certain tasks before or after a role is executed, we can use Pre_tasks and post_tasks to declare it. Pre_tasks is executed before role, while Post_tasks executes after role:
-Name:deply Webservers
Host:webservers
Vars_files:
-Secrets.yml
Pre_tasks:
-Name:update Yum Cache
Yum:update_cache=yes
Roles
-Role:apache
Database_host: {{hostvars.db.ansible_eth0.ipv4.address}}
Domains
-Exampel.com
-www.example.com
Post_tasks:
-Name:print Something
Shell:echo "The roles has been updated!"
4. Role dependence
If the current role needs to rely on another role before execution, we can define a role dependency in main.yml in the Roles meta directory.
Example 1:
#roles/webservers/meta/main.yml
Dependencies
-{Role:common, some_parameter:3}
-{role:apache, port:80}
-{role:postgres, Dbname:blarg, other_parameter:12}
Example 2:
Dependencies
-{ROLE:NTP, ntp_server=ntp.ubuntu.com}
-{Role:web}
-{role:memcached}
5. Ansible Galaxy
Ansible-galaxy is a tool that we can use to quickly create a standard roles directory structure, and use it to download the roles that others have written on the https:/galaxy.ansible.com.
Initialize a roles directory structure by Ansible-galaxy, by using the following method:
Ansible-galaxy Init/etc/ansible/roles/websrvs
Install roles written by someone else:
Ansible-galaxy install-p/etc/ansible/roles Bennojoy.mysql
List the installed roles:
Ansible-galaxy List
To view installed roles information:
Ansible-galaxy Info Bennojoy.mysql
Uninstall roles:
Ansible-galaxy Remove Bennojoy.mysql
This article is from the "Nobody" blog, please be sure to keep this source http://breezey.blog.51cto.com/2400275/1757832
The role of Ansible10:playbook and the inclusion of "go"