Ansible Concepts: Difference between revisions
(→Role) |
(→Role) |
||
Line 71: | Line 71: | ||
<pre> | <pre> | ||
roles | roles | ||
| | | |
Revision as of 17:50, 25 May 2017
External
Internal
Inventory File
Ansible works against multiple systems at the same time. It does this by selecting portions of systems listed in Ansible’s inventory file. The default location of the inventory file is /etc/ansible/hosts.
A different location of the inventory file can be specified on the command line with:
-i <path>
Inventory File Structure
host1.example.com [webservers] web1.example.com web2.example.com [dbservers] db1.example.com db2.example.com
Groups
The headings in brackets are group names, which are used in classifying systems and deciding what systems you are controlling at what times and for what purpose. A host can be part of more than one group.
Default Groups
There are two default groups: "all" and "ungrouped". "all" contains every host. ungrouped contains all hosts that don’t have another group aside from all.
Recursive Groups
Recursive groups are declared with the [<group-name>:children].
[A] host1 host2 [B] host3 host4 [AandB:children] A B [AandB:vars] something=something-else
Playbook
Playbooks exist to run tasks.
Role
Roles are units of organization. Assigning a role to a group of hosts implies that they should implement a specific behavior. A role may include applying certain variable values, certain tasks, and certain handlers. Roles are redistributable units that allow you to share behavior among playbooks. Conventionally, the elements associated with a role are stored in a directory named after the role:
roles | +-- role1 | | . +-- files . +-- handlers . +-- meta +-- tasks +-- templates +-- role2 +-- role3 ...
Action
Module
Task
A task combines an action (a module and its arguments) with a name and optionally some other keywords (like looping directives). Handlers are also tasks, but they do not run unless they are notified by name when a task reports an underlying change on a remote system. Playbooks exist to run tasks.
Handler
Handlers are just like regular tasks in a playbook (see Tasks) but are only run if the task contains a notify directive and also indicates that it changed something.
For example, if a config file is changed, then the task referencing the config file templating operation may notify a service restart handler. This means services can be bounced only if they need to be restarted. Handlers can be used for things other than service restarts, but service restarts are the most common usage.
Variable
Group Variables
Group variables are declared with [<group-name>:vars] in the inventory file. If they are declared this way, the variables apply to an entire group at once.
[group-A] host1 host2 [group-A:vars] something=something-else
Host Variables
Variables that apply to a specific host are declared in the inventory file after the host name:
[group1] host1 http_port=80 maxRequestsPerChild=808 host2 http_port=303 maxRequestsPerChild=909
Fact
Facts are pieces of information about remote nodes. Facts can used in playbooks and templates just like variables, but they are inferred, rather than set, during automatic discovery when running plays, by executing the internal setup module on the remote node.
Directives
notify
Template
A template is a file to be installed on the target system after the declared variables are substituted with actual values. Variable values may come from the inventory file, host variables, group variables, or facts. Templates use the Jinja2 template engine and can also include logical constructs like loops and if statements.