レシピを書く部分は前と同じなんだろうなと。
レシピを書く
recipes/hello.rb をつくる。やはり、Hello, World でしょう。
file 'hello.txt' do content "Hello, World\n" end
実際に動かしたときにレシピはあるけど、それを実行させる必要があるのでランリストをつくります。
これは .kitchen.yml に書いてやります。現状では、suites: の中の name が default であるものの run_list は recipe[cheftest::default] が設定されています。ということで run_list に追加しますが、さっきのレシピは recipe[cheftest::hello] と表記するみたいです。recipe[ ということなんでしょうか。ということで追加したファイルはこんな感じです。
---
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
suites:
- name: default
run_list:
- recipe[cheftest::default]
- recipe[cheftest::hello]
verifier:
inspec_tests:
- test/smoke/default
attributes:
動かしてみる
ここで、一連の操作をおこないます。kitchen コマンドに converge サブコマンドを与えて実行です。
$ kitchen converge
-----> Starting Kitchen (v1.16.0)
-----> Converging <default-ubuntu-1604>...
Preparing files for transfer
Preparing dna.json
Resolving cookbook dependencies with Berkshelf 6.2.0...
[2017-07-12T12:02:10+09:00] ERROR: Error connecting to https://supermarket.chef.io/universe, retry 1/5
Removing non-cookbook files before transfer
Preparing validation.pem
Preparing client.rb
-----> Chef Omnibus installation detected (install only if missing)
Transferring files to <default-ubuntu-1604>
Starting Chef Client, version 13.2.20
resolving cookbooks for run list: ["cheftest::default", "cheftest::hello"]
Synchronizing Cookbooks:
- cheftest (0.1.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 1 resources
Recipe: cheftest::hello
* file[hello.txt] action create
- create new file hello.txt
- update content in file hello.txt from none to 8663ba
--- hello.txt 2017-07-12 03:02:21.399391003 +0000
+++ ./.chef-hello20170712-2629-1u4keb0.txt 2017-07-12 03:02:21.399391003 +0000
@@ -1 +1,2 @@
+Hello, World
Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 01 seconds
Finished converging <default-ubuntu-1604> (0m11.22s).
-----> Kitchen is finished. (0m13.01s)
ということで、ERROR を吐いているのが気になりますが、ファイルは生成されているようです。
$ kitchen login default-ubuntu-1604 Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-81-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage 0 packages can be updated. 0 updates are security updates. Last login: Wed Jul 12 03:02:18 2017 from 10.0.2.2 vagrant@default-ubuntu-1604:~$ ls -l total 0
一瞬無いかと思いましたが、ファイルのパス指定の問題です。
vagrant@default-ubuntu-1604:~$ ls -l / total 89 drwxr-xr-x 2 root root 4096 Jun 26 12:17 bin drwxr-xr-x 4 root root 1024 Jun 26 12:28 boot drwxr-xr-x 19 root root 3840 Jul 12 01:55 dev drwxr-xr-x 93 root root 4096 Jul 12 01:55 etc -rw-r--r-- 1 root root 13 Jul 12 03:02 hello.txt drwxr-xr-x 3 root root 4096 Jun 26 12:12 home lrwxrwxrwx 1 root root 32 Jun 26 12:20 initrd.img -> boot/initrd.img-4.4.0-81-generic drwxr-xr-x 21 root root 4096 Jun 26 12:25 lib drwxr-xr-x 2 root root 4096 Jun 26 12:09 lib64 drwx------ 2 root root 16384 Jun 26 12:02 lost+found drwxr-xr-x 3 root root 4096 Jun 26 12:03 media drwxr-xr-x 2 root root 4096 Jul 19 2016 mnt drwxr-xr-x 4 root root 4096 Jul 12 01:55 opt dr-xr-xr-x 111 root root 0 Jul 12 01:55 proc drwx------ 2 root root 4096 Jun 26 12:02 root drwxr-xr-x 26 root root 960 Jul 12 03:05 run drwxr-xr-x 2 root root 12288 Jun 26 12:24 sbin drwxr-xr-x 2 root root 4096 Jun 29 2016 snap drwxr-xr-x 2 root root 4096 Jul 19 2016 srv dr-xr-xr-x 13 root root 0 Jul 12 01:55 sys drwxrwxrwt 9 root root 4096 Jul 12 03:07 tmp drwxr-xr-x 10 root root 4096 Jun 26 12:03 usr drwxr-xr-x 13 root root 4096 Jun 26 12:08 var lrwxrwxrwx 1 root root 29 Jun 26 12:20 vmlinuz -> boot/vmlinuz-4.4.0-81-generic vagrant@default-ubuntu-1604:~$ cat /hello.txt Hello, World
ありました。では、recipe/hello.rb を修正してみます。
file 'hello.txt' do content "Hello, Kitchen\n" end
再度実行してみます。
$ kitchen converge
-----> Starting Kitchen (v1.16.0)
-----> Converging <default-ubuntu-1604>...
Preparing files for transfer
Preparing dna.json
Resolving cookbook dependencies with Berkshelf 6.2.0...
[2017-07-12T12:12:54+09:00] ERROR: Error connecting to https://supermarket.chef.io/universe, retry 1/5
Removing non-cookbook files before transfer
Preparing validation.pem
Preparing client.rb
-----> Chef Omnibus installation detected (install only if missing)
Transferring files to <default-ubuntu-1604>
Starting Chef Client, version 13.2.20
resolving cookbooks for run list: ["cheftest::default", "cheftest::hello"]
Synchronizing Cookbooks:
- cheftest (0.1.0)
Installing Cookbook Gems:
Compiling Cookbooks...
Converging 1 resources
Recipe: cheftest::hello
* file[hello.txt] action create
- update content in file hello.txt from 8663ba to adeeb2
--- hello.txt 2017-07-12 03:02:21.399391003 +0000
+++ ./.chef-hello20170712-3005-1d9ghzq.txt 2017-07-12 03:13:05.786008277 +0000
@@ -1,2 +1,2 @@
-Hello, World
+Hello, Kitchen
Running handlers:
Running handlers complete
Chef Client finished, 1/1 resources updated in 01 seconds
Finished converging <default-ubuntu-1604> (0m11.20s).
-----> Kitchen is finished. (0m13.01s)
$ kitchen login default-ubuntu-1604
Welcome to Ubuntu 16.04.2 LTS (GNU/Linux 4.4.0-81-generic x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
0 packages can be updated.
0 updates are security updates.
Last login: Wed Jul 12 03:13:03 2017 from 10.0.2.2
vagrant@default-ubuntu-1604:~$ cat /hello.txt
Hello, Kitchen
書き換わっています。さすが Chef だ。
エラーの原因
さて、kitchen converge したときのエラー「ERROR: Error connecting to https://supermarket.chef.io/universe, retry 1/5」ですが、名前が引けていません。
ubuntu 16.04 で DNS masquerade があると名前解決の一発目で失敗することが頻発します。/etc/NetworkManager/NetworkManager.conf を修正します。dns=dnsmasq の行をコメントアウト(#dns=dnsmasq)します。そして、sudo service network-manager restart しておきます。

コメント