Troubled books versus roles

According to Ansible's docs, the Playbook is:

... the basis for a truly simple configuration management system and multiprocess deployment system, unlike the existing one, and one that is very suitable for deploying complex applications.

And again, according to the same documents, Roles are:

... ways to automatically load specific vars_files, tasks, and handlers based on a known file structure. Grouping content by role also makes it easy to distribute roles with other users.

However, the difference between these and their various uses is not immediately apparent to me. For example, if I configure my /etc/ansible/hosts to look like this:

 [databases] mydb01.example.org mydb02.example.org [mail_servers] mymail01.example.org mymail_dr.example.org 

... then what is this " [databases] " record ... role? Or a YAML file name for a Playbook somewhere? Or something else?!?

If someone can explain the differences in them to me, my understanding of Ansible will improve significantly!

  • Playbook vs Role vs [databases] and similar entries in /etc/ansible/hosts
  • If books are defined in YAML files, then where are the roles defined?
  • Besides ansible.cfg , which lives on Ansible server, how can I add / configure Ansible with available Playbooks / Roles? For example, when I run ansible-playbook someplaybook.yaml , how does Ansible know where to find this book?
+73
ansible ansible-playbook
Aug 19 '15 at 16:29
source share
5 answers

Playbook vs Role vs [databases] and similar entries in / etc / ansible / hosts

[databases] is one name for a group of hosts. It allows you to refer to multiple hosts by the same name.

A role is a set of tasks and additional files for configuring a host to perform a specific role.

Playbook is a mapping between hosts and roles.

An example from the documentation describes an example project. It contains two things:

  • Playbooks site.yml , webservers.yml , fooservers.yml are players.
  • Roles: roles/common/ and roles/webservers/ contain definitions of common and webservers roles, respectively.

Inside the playbook ( webservers.yml ) you have something like:

 --- - hosts: webservers <- this group of hosts defined in /etc/ansible/hosts, databases and mail_servers in example from your question roles: <- this is list of roles to assign to these hosts - common - webservers 

If books are defined in YAML files, then where are the roles defined?

They are defined inside the roles/* directories. Roles are defined mainly using YAML files, but can also contain resources of any type ( files/ , templates/ ). According to the documentation, the definition of a role is structured as follows:

  • If the /x/tasks/main.yml roles exist, the tasks listed in it will be added to the game.
  • If the /x/handlers/main.yml roles exist, the handlers listed in it will be added to the game,
  • If the /x/vars/main.yml roles exist, the variables listed in it will be added to the game.
  • If /x/meta/main.yml roles exist, any role dependencies listed in it will be added to the role list (1.3 and later).
  • Any copy tasks can refer to files in the roles / x / files / without the need to relate them relatively or absolutely
  • Any script tasks can refer to scripts in the roles / x / files / without having to relate them relatively or absolutely
  • Any tasks of the template can refer to files in the roles / x / templates / without the need to relate them relatively or absolutely
  • Any included tasks can refer to files in the roles / x / tasks / without the need to relate them relatively or absolutely

The most important file is roles/x/tasks/main.yml , here you define the tasks that will be executed when the role is executed.

Besides ansible.cfg, which lives on Ansible server, how can I add / configure Ansible with available Playbooks / Roles? For example, when I run the boot file someplaybook.yaml, how does Ansible know where to find this book?

 $ ansible-playbook someplaybook.yaml 

Will look for a tutorial in the current directory.

 $ ansible-playbook somedir/somedir/someplaybook.yaml 

Look at the playbook inside the somedir/somedir/ directory.

It is your responsibility to put your project with all the game books and roles on the server. Ansible has nothing to do with it.

+77
Aug 19 '15 at 16:50
source share

Playbook vs Role vs [databases] and similar entries in / etc / ansible / hosts

Roles are a way to combine tasks together into one container. You may have a role to configure MySQL, another to configure Postfix, etc.

The playbook determines what happens where. This is the place where you define the hosts (host groups, see below) and the roles that will be applied to these nodes.

[databases] , and the other entries in your inventory are host groups. Hostgroups determine the set of hosts on which the game will run.

Playback is a collection of tasks or roles (or both) within a play. In most cases (and examples) in the textbook there will be only one game. But you can have as much as you want. This means that you can have a playbook in which the postfix role in the mail_servers host mail_servers and the mysql role in the databases host group are mail_servers :

 - hosts: mail_servers roles: - postfix - hosts: databases roles: - mysql 

If books are defined in YAML files, then where are the roles defined?

In Ansible, almost everything is defined in YAML, which allows for roles and playbooks.

Besides ansible.cfg, which lives on Ansible server, how can I add / configure Ansible with available Playbooks / Roles? For example, when I run the boot file someplaybook.yaml, how does Ansible know where to find this book?

AFAIK, you must specify the path to the playbook when invoking ansible-playbook . Therefore, ansible-playbook someplaybook.yaml would expect someplaybook.yaml be in your current directory. But you can provide the full path: ansible-playbook /path/to/someplaybook.yaml

+29
Aug 19 '15 at 16:50
source share

This is a terminology / semantic question. This may be subjective, although there is a definition of a baseline.

My opinion is as follows:

Any system management / deployment system has:

  • source data - data used to create the configuration of the target host
  • target data - data used to identify target hosts.
  • config changes - list / set of rules / actions that we apply with source data on the target host based on target data

In inevitable terms:

  • source data are different places where we can put data - group_vars , playbook vars, role vars, etc. These places influence the priority (if a variable with the same name is redefined in different ones, there are very specific rules for what will be the value of the variable at runtime ansible / ansible-playbook
  • target data is inventory (And, it’s also possible to define inventory / host group variables inside inventory!)
  • config changes - ansible has 4 levels of abstraction for it:
    • task - one action
    • task list - action list
    • role - a list of actions (or a list of lists) grouped by the same "subject", usually all goals work in the same group of hosts / hosts
    • playbook - a playlist, each of which works with a possible different group of hosts, using several role s / task s / tasklists (and special tasks such as handlers )

From a β€œsoftware” point of view, the role must be sufficiently general so that it can be reused.

Also, in some (rather large) organizations, "roles" are assigned by group A, while they are used in books maintained by group B.

Summary

All of the above allows you to group similar configurations into a role . grouping related subsystems / components into one playbook . It is also worth noting that 1 YAML element in the playbook (including hosts: and either or tasks , pre_tasks , post_tasks , roles ) is called play

Now for your question:

Yes, at first it is confusing.

Usually you connect source data to the semantics of your role, so when you see that the setup_db role setup_db applied in a game on a related host group (for example, db_hosts ), but play can work on combining several host groups. It is simply a matter of agreement and flexibility.

PS

Please email me if this is added to the confusion or clarified. Thank.

+6
Sep 07 '17 at 21:40
source share

Also keep in mind that a playbook can trigger more than one role if a metafile is used to influence different roles.

Playbook example: dual_role-playbook.yml

 - name: Some Action for two roles hosts: localhost vars_files: - roles/dual_role/meta/main.yml roles: - dual_role/container-1 - dual_role/container-2 

The layout of the roles and files folder will look like this:

 dual_role-playbook.yml -- roles -- dual_role -- meta/main.yml -- container-1 -- tasks/main.yml -- templates/template.j2 -- container-2 -- tasks/main.yml -- templates/template.j2 
0
Aug 16 '17 at 19:19
source share

Simply put:

The playbook is similar to the main program, it contains complete instructions for completing the work. However, for large projects it is undesirable to invest in them all the details. So you need a role.

A role is a routine that typically performs a single task, for example, configuring a database server. You can put it in the rolesfile.yml roles/ directory or download third-party roles, rolesfile.yml URI in rolesfile.yml and rolesfile.yml -galaxy download them for you.

[database] is the host group defined in the inventory file, which lists the hosts belonging to the database group. You can also specify a group of web servers by specifying something like

 [web] web1.example.com web2.example.com 

web group or database can then be used in books or roles to indicate the hosts to which to apply.

Groups can also be used in the ansible to run special commands.

0
Apr 30 '19 at 12:47
source share



All Articles