📘
現代 IT 人一定要知道的 Ansible 自動化組態技巧
  • README
  • 前言
  • Ansible 是什麼?
  • 怎麼部署 Ansible 環境?
  • 怎麼用 Vagrant 練習 Ansible?
  • 怎麼用 Docker 練習 Ansible?
  • 怎麼操作 Ansible?
  • 怎麼用 Jupyter 操控 Ansible?(localhost)
  • 怎麼用 Jupyter 操控 Ansible?(Managed node)
  • 怎麼用 Docker Compose 練習 Ansible?
  • Ansible 的開發工具有哪些?
  • 怎麼看 Ansible Modules 文件?
  • 常用的 Ansible Module 有哪些?
  • 怎麼使用 setup 取得 Managed node 的 facts?
  • 怎麼使用 Ansible 的 Template 系統?
  • 怎麼在 Playbooks 裡使用 Handlers?
  • Ansible 如何發送 notification 到 Slack?
  • Ansible 如何發送 notification 到 HipChat?
  • 如何在 Playbooks 使用 loops?
  • 如何維護大型的 Playbooks?
  • Roles 是什麼?
  • 怎麼使用 Roles?
  • 怎麼用 Roles 部署 LNMP 網頁應用程式?(上)
  • 怎麼用 Roles 部署 LNMP 網頁應用程式?(下)
  • 怎麼用 Roles 部署 TestLink?
  • 怎麼在 Ansible Galaxy 分享 Roles?(上)
  • 怎麼在 Ansible Galaxy 分享 Roles?(下)
  • 怎麼用 Travis CI 測試 Roles?
  • 怎麼混用 Ansible 建置 Docker image?
  • 怎麼在 Docker Hub 混用 Ansible 自動建置 Docker image?
  • 怎麼用 Vault 管理敏感資料?
  • 總結
Powered by GitBook
On this page
  • 27. 怎麼用 Travis CI 測試 Roles?
  • 後語
  • 相關連結

Was this helpful?

Edit on GitHub

怎麼用 Travis CI 測試 Roles?

Previous怎麼在 Ansible Galaxy 分享 Roles?(下)Next怎麼混用 Ansible 建置 Docker image?

Last updated 5 years ago

Was this helpful?

27. 怎麼用 Travis CI 測試 Roles?

在「」一節裡,凍仁曾提過好的 Roles 應導入 整合測試,有了持續整合 (CI) 才能確保每次提交的品質。

接下來將以 這個簡單的 role 為例,讓凍仁帶大家串起 , 和 Travis CI 三個服務。

  1. 將左方的開關切換至 on。

  2. 於 Git Repository 裡新增 .travis.yml。

  3. 使用 git push 即會觸發 Travis CI。

  4. chusiang.helloworld/.travis.yml 的內容如下。

---
language: python
python: "2.7"

# Use the new container infrastructure
sudo: false

# Install ansible
addons:
  apt:
    packages:
    - python-pip

install:
  # Install ansible
  - pip install ansible

  # Check ansible version
  - ansible --version

  # Create ansible.cfg with correct roles_path
  - printf '[defaults]\nroles_path=../' >ansible.cfg

script:
  # Basic role syntax check
  - ansible-playbook tests/test.yml -i tests/inventory --syntax-check

notifications:
  webhooks: https://galaxy.ansible.com/api/v1/notifications/
  • 此例用了 --syntax-check 參數進行基本的語法檢測,較進階的作法則是藉由 Docker 進行測試。

  • 使用 notifications 和 webhooks 發送通訊給 Galaxy。

以上,恭喜您學會了在 Roles 加入 Travis CI 自動化測試的技能。

凍仁提醒您:「當 build state badge 顯示 fail 時,記得回頭修一下 Roles 喔!」

後語

仔細回想,要是當初沒這麼做,誰又可以確保半年前寫的 code 現在還可以正常運作呢!

相關連結

撰寫一個 Role,並上傳至 GitHub。 https://github.com/chusiang/helloworld.ansible.role

於 Galaxy 網站進行匯入,詳請請參考「」一節。 https://galaxy.ansible.com/chusiang/helloworld/

進入 網站。

點擊左上角的 ✚ 整合新 Repository。

只需三個步驟即可整合 Travis CI。

將範例 Role 左方的開關切至 on 以整合 Travis CI。

建立完畢,等待觸發,這時的 build state 會是 unknown。

使用 git push 到 GitHub 後,Travis CI 會自動觸發。

若想把 build state 的 badge 放入 README.md,可點擊該 badge 獲得各式語法。

回到 Galaxy 網站,會發現多了 build state 的 badge。

此文發佈不到一天,凍仁就收到熱心網友回報的 。百忙之中上了 hotfix 修正,並確認 Travis CI 測試無誤。

bug issue
Automated Testing | Ansible Galaxy
CI 從入門到入坑 系列 by Miles
Travis CI
chusiang.helloworld
GitHub
Galaxy
Travis CI
automate_with_ansible_practice-30.jpg
2016-12-27-test-role-with-travis-1.png
2016-12-27-test-role-with-travis-3.png
2016-12-27-test-role-with-travis-4.png
2016-12-27-test-role-with-travis-5.png
2016-12-27-test-role-with-travis-6.gif
2016-12-27-test-role-with-travis-7.png
2016-12-27-test-role-with-travis-8.png
2016-12-27-test-role-with-travis-10.png
2016-12-27-test-role-with-travis-9.png
2016-12-27-test-role-with-travis-2.png
怎麼在 Galaxy 匯入 Roles?
好的 Roles 應具備什麼?