According to the Ansible docs, a Playbook
is:
…the basis for a really simple configuration management and multi-machine deployment system, unlike any that already exist, and one that is very well suited to deploying complex applications.
And, again, according to those same docs, a Role
are:
…ways of automatically loading certain vars_files, tasks, and handlers based on a known file structure. Grouping content by roles also allows easy sharing of roles with other users.
However the distinction between these and their different use cases is not immediately obvious to me. For instance, if I configure my /etc/ansible/hosts
file to look like:
[databases]
mydb01.example.org
mydb02.example.org
[mail_servers]
mymail01.example.org
mymail_dr.example.org
…then what is this "[databases]
" entry…a role? Or the name of a playbook YAML file somewhere? Or something else?!?
If someone could explain to me the differences on these, my understanding of Ansible would be greatly enhance!
- Playbook vs Role vs
[databases]
and similar entries in/etc/ansible/hosts
- If Playbooks are defined inside of YAML files, then where are Roles defined?
- Aside from the
ansible.cfg
living on the Ansible server, how do I add/configure Ansible with available Playbooks/Roles? For instance, when I runansible-playbook someplaybook.yaml
, how does Ansible know where to find that playbook?
Best Answer
[databases]
is a single name for a group of hosts. It allows you to reference multiple hosts by a single name.Role is a set of tasks and additional files to configure host to serve for a certain role.
Playbook is a mapping between hosts and roles.
Example from documentation describes example project. It contains two things:
site.yml
,webservers.yml
,fooservers.yml
are playbooks.roles/common/
androles/webservers/
contain definitions ofcommon
andwebservers
roles accordingly.Inside playbook (
webservers.yml
) you have something like:They are defined inside
roles/*
directories. Roles are defined mostly using YAML files, but can also contain resources of any types (files/
,templates/
). According to documentation role definition is structured this way:The most important file is
roles/x/tasks/main.yml
, here you define tasks, which will be executed, when role is executed.Will look for a playbook inside current directory.
Will look for a playbook inside
somedir/somedir/
directory.It's your responsibility to put your project with all playbooks and roles on server. Ansible has nothing to do with that.