Ansible Playbooks vs Roles

后端 未结 5 1222
暖寄归人
暖寄归人 2021-01-29 20:38

According to the Ansible docs, a Playbook is:

...the basis for a really simple configuration management and multi-machine deployment system, unlike any that

5条回答
  •  既然无缘
    2021-01-29 21:11

    It's a terminology/semantic question. It can be subjective, even though there is a baseline definition.

    My view is as follows:

    Any configuration management/deployment system has:

    1. source data - data used to create target host's configuration
    2. target data - data used to identify target hosts
    3. config changes - list/set of rules/actions we apply with source data over target host based on target data

    In Ansible terms:

    1. source data - is the various places we can put data - group_vars, playbook vars, role vars, etc., These places affect precedence (if a variable named the same is re-defined in different locations, there are very specific rules of what would be the value of the variable during ansible/ansible-playbook execution
    2. target data - is the inventory (And, It's also possible to define inventory/hostgroup variables inside inventory!)
    3. config changes - ansible has 4 levels of abstraction for it:
      1. task - single action
      2. task list - list of actions
      3. role - list of actions (or list of lists) grouped by the same 'subject', usually all targets are operating on the same host/hostgroup
      4. playbook - list of plays, each operating on possibly different hostgroup, applying several roles/tasks/tasklists (and special tasks like handlers)

    From 'software' aspect - role should be generic enough to be reused.

    Also in some (rather big) organizations, 'roles' are shipped by group A, while used in playbooks maintained by group B.

    summary

    All the above allows grouping of similar configurations - into a role. grouping related subsystems/components into one playbook. Also, worth mentioning, 1 YAML item in a playbook (including hosts: and either or tasks, pre_tasks, post_tasks, roles) is called a play

    Now for your question:

    Yes, it is confusing at first.

    You usually connect your source data to your role's semantics, so when you see that role setup_db is applied in a play onto related hostgroup (e.g. db_hosts) But a play can be running over a union of several hostgroups. It's just a matter of convention vs flexibility.

    P.S.

    Please write me back whether this added to the confusion, or clarified. Thanks.

提交回复
热议问题