0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

test-kitchen において個人的に使う driver, provisioner の組み合わせと.kitchen.yml

Posted at

ChefDK導入後にchef generate cookbookコマンドを用いてcookbookを作成した場合に構成されるkitchenCIの設定ファイル .kitchen.yml は、以下の組み合わせをサポートする。

  • ローカルのVirtualBox上 ubuntu, centos
  • Chef
  • Inspec

しかし、test-kitchenは様々な組み合わせで使用する事ができる。

対象:

  • ローカルのVirtualBox上 ubuntu, centos
  • ローカルのdocker 上 ubuntu, centos
  • 既に稼働している既存のサーバー

デプロイツール

  • chef (対象にchef-clientを導入してchefzeroモードで適用)
  • ansible (ローカルに導入されたansibleからリモート適用)
  • ansible (対象に導入してローカルモードで適用)

個人的には以下の組み合わせで使う事が多い。

VirtualBox docker 既存のサーバー
Chef Chefレシピ開発 軽いChef動作確認 Chefサーバー無しでの既存サーバー管理
Ansible(リモート反映) Ansibleプレイブック開発 軽いAnsible動作確認

テストが必要な場合にはInspecを使用
Ansible(リモート反映)を既存のサーバーに反映させるケースは、ansible-playbookコマンドを生で使えば対応できるので、メリットは若干薄れる。
Ansible(ローカル反映)ケースは個人的には使用していないが、Ansibleのバージョンを変更してテストを行う必要がある場合には有用と思われる。

Chef + VirtualBox

driver: vagrant
provisioner: chef_zero

chefDKにより提供されるchefコマンドを用いてcookbookの雛形を作ると設定されている標準の組み合わせ。

$ chef generate cookbook mybook
Generating cookbook mybook
- Ensuring correct cookbook file content
- Committing cookbook files to git
- Ensuring delivery configuration
- Ensuring correct delivery build cookbook content
- Adding delivery configuration to feature branch
- Adding build cookbook to feature branch
- Merging delivery content feature branch to master

Your cookbook is ready. Type `cd mybook` to enter it.

There are several commands you can run to get started locally developing and testing your cookbook.
Type `delivery local --help` to see a full list.

Why not start by writing a test? Tests for the default recipe are stored at:

test/integration/default/default_test.rb

If you'd prefer to dive right in, the default recipe can be found at:

recipes/default.rb
mybook/.kitchen.yml
---
driver:
  name: vagrant

provisioner:
  name: chef_zero
  # You may wish to disable always updating cookbooks in CI or other testing environments.
  # For example:
  #   always_update_cookbooks: <%= !ENV['CI'] %>
  always_update_cookbooks: true

verifier:
  name: inspec

platforms:
  - name: ubuntu-16.04
  - name: centos-7

suites:
  - name: default
    run_list:
      - recipe[mybook::default]
    verifier:
      inspec_tests:
        - test/integration/default
    attributes:

前提として、ローカルマシンに VirtualBox, Vagrant が導入されている必要がある。

Ansible(リモート反映) + VirtualBox

driver: vagrant
provisioner: ansible_push

.kitchen.yml
---
driver:
  name: vagrant

provisioner:
  name: ansible_push    # https://github.com/ahelal/kitchen-ansiblepush
  sudo: true
  chef_bootstrap_url: nil
  use_instance_name: true

verifier:
  name: inspec

platforms:
  - name: centos-7

suites:
  - name: ansiblepush
    provisioner:
      playbook:  site.yml
    verifier:
      inspec_tests:
        - test/integration/default
    attributes:

Ansible 関係の provisioner としては、ansible と ansible_push が存在するが、主にローカルマシンの Ansible を使用する ansible_push を使用する。

参考:

Chef + Docker

driver: dokken
provisioner: dokken(chef)

.kitchen.yml
<% cookbook = %x( awk '$1 == "name" {print $2}' metadata.rb ).chomp.gsub(/['"]/, '') %>
---
driver:
  name: dokken
  chef_version: latest

transport:
  name: dokken

provisioner:
  name: dokken

verifier:
  name: inspec
  inspec_tests:
    - test/integration/default

platforms:
  - name: centos-7
    driver:
      image: dokken/centos-7

suites:
  - name: default
    run_list:
    - recipe[<%= cookbook %>::default]

前提:
ローカルにChefDKとDockerが導入されていること。
kitchen-dokkenはChefDKに含まれる。

参考:

Ansible(リモート反映) + Docker

driver: docker
provisioner: ansible_push

.kitchen.yml
---
driver:
  name: docker

provisioner:
  name: ansible_push
  chef_bootstrap_url: nil
  become: true

verifier:
  name: inspec

platforms:
  #- name: ubuntu-16.04
  - name: centos-7

suites:
  - name: ansiblepush
    provisioner:
      playbook: site.yml
    verifier:
      inspec_tests:
        - test/integration/default

前提としてkitchen-docker gemが必要
$ gem install kitchen-docker

参考:

Chef + 既存サーバー

driver: proxy
provisioner: chef_zero

ssh接続可能な既存のサーバーに対して Chef による変更を行う。

.kitchen.yml
<% hosts = ENV['HOSTS'].split(',') %>
<% cookbook = %x( awk '$1 == "name" {print $2}' metadata.rb ).chomp.gsub(/['"]/, '') %>
---
provisioner:
  name: chef_zero
  always_update_cookbooks: true
  sudo: false
  product_name: chef
  install_strategy: skip
  data_path: data

verifier:
  name: inspec
  sudo: false
  inspec_tests:
    - test/integration/default

platforms:
  - name: unix
    run_list:
      - recipe[<%= cookbook %>::default]

suites:
<% 
  hosts.each do |host| 
%>
  - name: <%= host %>
    driver:
      name: proxy
      host: <%= host %>
      sudo: false
      reset_command: 'exit 0'
      ssh_key: ~/.ssh/id_rsa
<% end %>

環境変数 HOSTS に接続先を設定して kitchenCIを呼び出す。
-pをつけるとパラレル実行が可能。

$ env HOSTS=node1,node2 kitchen test

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?