Ansible Playbooks vs Roles Ask Question

Ansible Playbooks vs Roles Ask Question

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 run ansible-playbook someplaybook.yaml, how does Ansible know where to find that playbook?

ベストアンサー1

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

[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:

  • Playbooks. site.yml, webservers.yml, fooservers.yml are playbooks.
  • 役割:roles/common/および には、それに応じた役割roles/webservers/の定義が含まれます。commonwebservers

プレイブック ( webservers.yml) 内には次の内容が含まれます。

---
- 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

Playbook が YAML ファイル内で定義されている場合、ロールはどこで定義されますか?

それらはディレクトリ内で定義されますroles/*。ロールは主にYAMLファイルを使用して定義されますが、任意のタイプのリソース(files/templates/)を含めることもできます。ドキュメンテーション役割の定義は次のように構成されます。

  • roles/x/tasks/main.ymlが存在する場合、そこにリストされているタスクがプレイに追加されます。
  • roles/x/handlers/main.ymlが存在する場合、そこにリストされているハンドラーがプレイに追加されます。
  • roles/x/vars/main.ymlが存在する場合、そこにリストされている変数がプレイに追加されます。
  • roles/x/meta/main.yml が存在する場合、そこにリストされているロール依存関係はロールのリストに追加されます (1.3 以降)
  • どのコピータスクでも、roles/x/files/ 内のファイルを相対パスまたは絶対パスで指定せずに参照できます。
  • スクリプトタスクは、相対パスまたは絶対パスを指定せずに、roles/x/files/ 内のスクリプトを参照できます。
  • テンプレートタスクは、roles/x/templates/ 内のファイルを相対パスまたは絶対パスで指定しなくても参照できます。
  • すべてのインクルードタスクは、roles/x/tasks/ 内のファイルを相対パスまたは絶対パスで指定することなく参照できます。

最も重要なファイルは ですroles/x/tasks/main.yml。ここでは、ロールが実行されたときに実行されるタスクを定義します。

Ansible サーバー上にある ansible.cfg 以外に、利用可能な Playbook/Roles を使用して Ansible を追加/構成するにはどうすればよいですか? たとえば、ansible-playbook someplaybook.yaml を実行すると、Ansible はその Playbook の場所をどのようにして知るのでしょうか?

$ ansible-playbook someplaybook.yaml

現在のディレクトリ内でプレイブックを検索します。

$ ansible-playbook somedir/somedir/someplaybook.yaml

somedir/somedir/ディレクトリ内でプレイブックを検索します。

すべてのプレイブックとロールを含むプロジェクトをサーバーに配置するのはあなたの責任です。Ansible はそれとは何の関係もありません。

おすすめ記事