> For the complete documentation index, see [llms.txt](https://ansible.drx.tw/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://ansible.drx.tw/21.how-to-use-the-roles.md).

# 怎麼使用 Roles？

## 21. 怎麼使用 Roles？

上一章「[20. Roles 是什麼？](/20.what-is-the-roles.md)」凍仁簡單介紹了 Roles，接下來重頭戲登場囉！

![automate\_with\_ansible\_roles\_and\_windows-07.jpg](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-0e45b048393e5aa9ac9a7529d23239ff9957a3ef%2Fautomate_with_ansible_roles_and_windows-07.jpg?alt=media)

我們可以透過 [**Galaxy**](https://galaxy.ansible.com/) **(銀河)** 和 `ansible-galaxy` (Terminal) 來使用 Roles。

![automate\_with\_ansible\_roles\_and\_windows-08.jpg](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-e8b250c13970f0e92361bfadcc123e5b1415359f%2Fautomate_with_ansible_roles_and_windows-08.jpg?alt=media)

### 什麼是 Galaxy？

[Galaxy](https://galaxy.ansible.com/) 的全名為 Ansible Galaxy，它是官方維護的 Roles 市集 (marketplace) 網站。我們可以在網站上取得社群成員所維護的 Roles，其 source code 存放於 GitHub。

![automate\_with\_ansible\_roles\_and\_windows-09.jpg](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-956baac3225e5bf9c264fb2777bb6baf2579a323%2Fautomate_with_ansible_roles_and_windows-09.jpg?alt=media)

### ansible-galaxy 是什麼？

`ansible-galaxy` 是管理 Roles 的指令，我們可以在 Terminal 裡用它搜尋 (search)、安裝 (install)、移除 (remove) Roles 等。換句話說它是 Ansible 世界的 `pip`。

![automate\_with\_ansible\_roles\_and\_windows-14.jpg](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-00be359643da9a894c19bc329894fe3a6a6ba07f%2Fautomate_with_ansible_roles_and_windows-14.jpg?alt=media)

想深入了解 `ansible-galaxy` 的話，不妨 man 一下它！

```
$ man ansible-galaxy
```

### 怎麼用 Galaxy 搜尋 Roles？

1. 開啟瀏覽器 (Browsers) 並進入 [https://galaxy.ansible.com](https://galaxy.ansible.com/)。
2. 進到 Galaxy 的網站後，可在右上角的 Browser Roles 找到數不完的 Role。

![automate\_with\_ansible\_roles\_and\_windows-10.jpg](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-eb69dd4f7af5bd654c6ca7f7dbce1a3ae8572da7%2Fautomate_with_ansible_roles_and_windows-10.jpg?alt=media)

1. 在 **Keyword** 一欄輸入關鍵字後，接下 `Enter` 即可搜尋。底下將以 `win_vim` role 為例。

![automate\_with\_ansible\_roles\_and\_windows-11.jpg](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-1977d51598f8713e52e355a4047e80b7552bfc38%2Fautomate_with_ansible_roles_and_windows-11.jpg?alt=media)

1. 在 **Details** 頁籤裡，可以看到關於這個 Role 的版本記錄 (Version History)、支援的平台 (Supported Platforms) 等資訊。

> Ansible 最小支援版本 (Minimum Ansible Version) 的部份也請留意，有些語法在舊版的 Ansible 是不被支援的喔！

![automate\_with\_ansible\_roles\_and\_windows-12.jpg](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-feb1b7145b8967cbcaa1836c8b4ef8b8bb8878e0%2Fautomate_with_ansible_roles_and_windows-12.jpg?alt=media)

1. 在 **README** 頁籤裡，可以看到作者寫的說明文件。一般會在 **Role Variables** 底下列出哪些可以被覆寫的預設變數。

![automate\_with\_ansible\_roles\_and\_windows-13.jpg](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-4212e6ec1b403352e37432ae581a5204a3dca7b8%2Fautomate_with_ansible_roles_and_windows-13.jpg?alt=media)

### 怎麼用 ansible-galaxy 搜尋 Roles？

除了在 Galaxy 網站搜尋 Roles 以外，我們也可以使用 `ansible-galaxy search` + `關鍵字`進行搜尋。

![automate\_with\_ansible\_roles\_and\_windows-15.jpg](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-3144b7ccb0eefe899b9517af0be54c13e2599e79%2Fautomate_with_ansible_roles_and_windows-15.jpg?alt=media)

## 怎麼安裝 Roles？

請使用 `ansible-galaxy install` + `Role 名稱` 來安裝 role，如要自訂存放 Roles 的位置，請使用 `-p` + `路徑`，詳情請參考 [Download Roles | Ansible Galaxy](https://galaxy.ansible.com/intro#download) 一文。

![automate\_with\_ansible\_roles\_and\_windows-16.jpg](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-0cc3c488da3667e7517ede9bc57c0e7e19fde8d6%2Fautomate_with_ansible_roles_and_windows-16.jpg?alt=media)

## 怎麼初始化 Roles？

我們可以用 `ansible-galaxy init` + `Role 名稱` 來建立一個新 role，其檔案結構部份凍仁已在上一章的「[Role 的基本結構](https://ansible.drx.tw/pages/-LpvdGRLmfYJt3r8sBfY#role-的基本結構)」介紹過，這裡就不詳述了。

![automate\_with\_ansible\_roles\_and\_windows-17.jpg](https://734644837-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LpvbhL-2bDPbj7qVDnY%2Fuploads%2Fgit-blob-65288b705c2de36a5a5d19f9047dcad7ee30c11a%2Fautomate_with_ansible_roles_and_windows-17.jpg?alt=media)

以上，您學會怎麼使用 Roles 了嗎？下章凍仁將教大家如何利用 Galaxy 上的 Roles 建置 LNMP 環境。

### 後語

我們除了可以在 Galaxy 找到現成的 Roles 外，更可藉由閱讀前人的 source code 來學習各種知識與技巧。

**倘若覺得別人的 Roles 不好使，您就自己寫一個，然後分享出來吧！**

### 相關連結

* [Ansible Galaxy | Find, reuse, and share the best Ansible content](https://galaxy.ansible.com/)
* [Ansible: Up and Running — an O’Reilly book by Lorin Hochstein](http://www.ansiblebook.com)
* [現代 IT 人一定要知道的 Ansible 自動化組態技巧 Ⅱ - Roles & Windows | 凍仁的筆記](http://note.drx.tw/2016/07/automate-with-ansible-roles-windows.html)
