Task Include files
The plays and playbooks can be called repeatedly between different tasks. Include files can be used to achieve a purpose
The system uses the include task to perfectly implement the role definition, remembering that play in playbook is ultimately
The goal is to map system clusters into multiple roles
Cat Tasks/foo.yml
---
# possibly saved as Tasks/foo.yml
-Name:placeholder foo
Command:/bin/foo
-Name:placeholder Bar
Command:/bin/bar
The include directive is similar to the following, which can be mixed in playbook like the normal tasks command
Tasks
-Include:tasks/foo.yml
You can also transfer variables to the includes directive, called parameterized include
How to distribute multiple WordPress instances, I can forgive all WordPress commands to a wordpress.yml
File
Tasks
-Include:wordpress.yml Wp_user=timmy
-Include:wordpress.yml Wp_user=alice
-Include:wordpress.yml Wp_user=bob
If you are using ansible1.4 or later, the include syntax simplifies matching roles while allowing
Passing parameter lists and dictionaries:
Tasks
-{include:wordpress.yml,wp_user:timmy,ssh_keys:[' keys/one.txt ', ' keys/two.txt '}
You can also refer to them {{Wp_user}}
Starting with 1.0, Ansible also supports a different way of passing variables to include files-structured variables
Methods are as follows
Tasks
-Include:wordpress.yml
VARs
Wp_user:timmy
Ssh_keys:
-Keys/one.txt
-Keys/two.txt
Playbooks can also include references to other playbooks
Includes functions can also be used in handlers areas, for example, if you want to define how to restart Apache, you only need to define a playbook, and only need to do it once. Edit the handers.yml similar to the following sample:
---
# This might is in a file like Handlers/handlers.yml
-Name:restart Apache
Service:name=apache state=restarted
You can then refer to the play in main playbook as follows:
Handlers:
-Include:handlers/handlers.yml
Includes can also mix references in tasks and handlers files that do not normally contain included. Includes is often used to import commands from one playbook file to another playbook. This allows us to define a top-level playbook (top-level playbook) made up of other playbooks.
-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
This article from "Eight Miles" blog, declined reprint!
Ansible Study notes 9-playbooks include