AnsibleのroleテストはTravisCIが主流だというので試してみようじゃないという趣旨の記事です。
やること 以下のような感じで適当にリポジトリを作成しておく 作成したリポジトリをClone こんな感じのディレクトリ構成となる TravisCIへログインを行う Travis CI - Test and Deploy Your Code with Confidence 右上のアカウントアイコンをクリックし、「Setting」を選択し、対象のリポジトリを有効にする TravisCI上でテストが走ることを確認 そして正常に完了することを確認 矢印部分をクリックし、Markdownを選択するとなんかでてくるのでそれをコピーして こんな感じで記載してGitHubへPushする GitHubで以下のように表示されていることを確認 おわりに 超簡単にですがこれでAnsible roleの自動テストができるようになったぞ! ただ、TravisCIはUbuntuかMacにしか対応しておらず、そのほかのディストリビューションでやろうとした場合、Dockerを使ったりする必要があるのでそれはまたの機会に! 今回のリポジトリはこちら!
GitHubにリポジトリを作成

ansible-galaxyコマンドでroleのひな型を作成$ git clone https://github.com/Tocyuki/ansible-role-travisci-handson.git
roleのひな型を作成する$ ansible-galaxy init -f ansible-role-travisci-handson
- ansible-role-travisci-handson was created successfully
$ tree ansible-role-travisci-handson
ansible-role-travisci-handson
├── LICENSE
├── README.md
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
8 directories, 9 files
.travis.ymlを作成tasks/main.ymlに適当に記述しておく---
# tasks file for ansible-role-travisci-handson
- name: Debug Message
debug:
msg: 'Hello World!!!'
.travis.ymlに以下のように記載---
language: python
python: 2.7
sudo: false
addons:
apt:
packages:
- python
install:
- pip install ansible
- ansible --version
- printf '[defaults]\nroles_path=../' >ansible.cfg
script:
- ansible-playbook tests/test.yml -i tests/inventory --syntax-check
- ansible-playbook tests/test.yml -i tests/inventory -C
- ansible-playbook tests/test.yml -i tests/inventory
TravisCI側で実行対象のリポジトリを設定する


GitHubへプッシュしTravisCIでテストされることを確認
tasks/main.ymlと.travis.ymlをgit addする$ git add tasks/main.yml .travis.yml
git commitを行う$ git commit -m "Add tasks/main.yml .travis.yml"
[master c367d3a] Add tasks/main.yml .travis.yml
2 files changed, 23 insertions(+), 1 deletion(-)
create mode 100644 .travis.yml
git pushを実行$ git push
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (5/5), 655 bytes | 0 bytes/s, done.
Total 5 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/Tocyuki/ansible-role-travisci-handson.git
9edd540..c367d3a master -> master
tests/test.ymlにconnection: localを入れわすれてビルドに失敗したので修正後、再度git push実行- hosts: localhost
connection: local
remote_user: root
roles:
- ansible-role-travisci-handson
$ gaa ; gcmsg "Fix tests/test.yml"; gp
[master 222b373] Fix tests/test.yml
1 file changed, 2 insertions(+), 1 deletion(-)
Counting objects: 4, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 452 bytes | 0 bytes/s, done.
Total 4 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
To https://github.com/Tocyuki/ansible-role-travisci-handson.git
c367d3a..222b373 master -> master


READMEにテスト結果のバッチをつける
README.mdに張り付ける
git diff README.md
diff --git a/README.md b/README.md
index 225dd44..044d484 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,8 @@
Role Name
=========
+[](https://travis-ci.org/Tocyuki/ansible-role-travisci-handson)
+
A brief description of the role goes here.
Requirements
