Ansible 怎么使用 Roles

2018-07-25 10:53 更新

上一章「Ansible 什么是Roles」凍仁簡(jiǎn)單介紹了 Roles,接下來(lái)重頭戲登場(chǎng)咯!

automate_with_ansible_roles_and_windows-07.jpg

我們可以通過(guò) Galaxy (銀河) 和 ansible-galaxy (Terminal) 來(lái)使用 Roles。

automate_with_ansible_roles_and_windows-08.jpg

什么是 Galaxy?

Galaxy 的全名為 Ansible Galaxy,它是官方維護(hù)的 Roles 市集 (marketplace) 網(wǎng)站。我們可以在網(wǎng)站上取得社群成員所維護(hù)的 Roles,其 source code 存放于 GitHub。

automate_with_ansible_roles_and_windows-09.jpg

ansible-galaxy 是什么?

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

automate_with_ansible_roles_and_windows-14.jpg

想深入了解 ansible-galaxy 的話,不妨 man 一下它!

$ man ansible-galaxy

怎么用 Galaxy 搜尋 Roles?

  1. 開(kāi)啟瀏覽器 (Browsers) 并進(jìn)入 https://galaxy.ansible.com
  2. 進(jìn)到 Galaxy 的網(wǎng)站后,可在右上角的 Browser Roles 找到數(shù)不完的 Role。

    automate_with_ansible_roles_and_windows-10.jpg

  3. 在 Keyword 一欄輸入關(guān)鍵字后,接下 Enter 即可搜尋。底下將以 win_vim role 為例。

    automate_with_ansible_roles_and_windows-11.jpg

  4. 在 Details 頁(yè)簽里,可以看到關(guān)于這個(gè) Role 的版本記錄 (Version History)、支持的平臺(tái) (Supported Platforms) 等資訊。

    Ansible 最小支持版本 (Minimum Ansible Version) 的部份也請(qǐng)留意,有些語(yǔ)法在舊版的 Ansible 是不被支持的喔!

    automate_with_ansible_roles_and_windows-12.jpg

  5. 在 README 頁(yè)簽里,可以看到作者寫的說(shuō)明文件。一般會(huì)在 Role Variables 底下列出哪些可以被覆寫的預(yù)設(shè)變數(shù)。

    automate_with_ansible_roles_and_windows-13.jpg

怎么用 ansible-galaxy 搜尋 Roles?

除了在 Galaxy 網(wǎng)站搜尋 Roles 以外,我們也可以使用 ansible-galaxy search + 關(guān)鍵字進(jìn)行搜尋。

automate_with_ansible_roles_and_windows-15.jpg

怎么安裝 Roles?

請(qǐng)使用 ansible-galaxy install + Role 名稱 來(lái)安裝 role,如要自定存放 Roles 的位置,請(qǐng)使用 -p + 路徑,詳情請(qǐng)參考 Download Roles | Ansible Galaxy 一文。

automate_with_ansible_roles_and_windows-16.jpg

怎么初始化 Roles?

我們可以用 ansible-galaxy init + Role 名稱 來(lái)建立一個(gè)新 role,其檔案結(jié)構(gòu)部份凍仁已在上一章的「Role 的基本結(jié)構(gòu)」介紹過(guò),這里就不詳述了。

automate_with_ansible_roles_and_windows-17.jpg

以上,您學(xué)會(huì)怎么使用 Roles 了嗎?下章凍仁將教大家如何利用 Galaxy 上的 Roles 建置 LNMP 環(huán)境。

后話

我們除了可以在 Galaxy 找到現(xiàn)成的 Roles 外,更可借由閱讀前人的 source code 來(lái)學(xué)習(xí)各種知識(shí)與技巧。

倘若覺(jué)得別人的 Roles 不好使,您就自己寫一個(gè),然后分享出來(lái)吧!

相關(guān)連結(jié)


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)