0
0

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.

Chef templateリソースでincompatible character encodings: ASCII-8BIT and UTF-8エラー

Posted at

備忘

環境

test-kitchen(vagrant + virtualbox + centos + chef-zero)環境

Chef Development Kit Version: 3.1.0
chef-client version: 14.2.0
kitchen version: 1.22.0
Vagrant 2.1.2
bento/centos-7 (virtualbox, 201808.24.0) : CentOS Linux release 7.5.1804 (Core)

事象

Chefのtemplateリソースを使用していた際にincompatible character encodings: ASCII-8BIT and UTF-8エラーとなった。
erbファイルが日本語を含み、かつ<%= %>内で日本語を返すとエラーとなることがある様子。
erbファイルやレシピのコード指定は効果が見られない。

recipes/default.rb
# encoding: UTF-8
node.default['foo'] = 'あいうえお'

template '/tmp/test.txt' do
  source 'test.txt.erb'
end
templates/test.txt.erb
<%# encoding: UTF-8 %>
テスト
<%= node['foo'] %>
...
         * template[/tmp/test.txt] action create
           
           ================================================================================
           Error executing action `create` on resource 'template[/tmp/test.txt]'
           ================================================================================
           
           Chef::Mixin::Template::TemplateError
           ------------------------------------
           incompatible character encodings: ASCII-8BIT and UTF-8
...

<%= node['foo'].encoding %>とするとUTF-8となるので、erb側がASCII-8BITなのか。
コード指定では対応できなかった。

とりあえず回避

erbファイル本体から日本語を抜いた結果、<%= %>にて日本語を埋め込んでも正しく動作できているが。。

templates/test.txt.erb
<%# encoding: UTF-8 %>
<%= node['foo'] %>
         * template[/tmp/test.txt] action create
           - create new file /tmp/test.txt
           - update content in file /tmp/test.txt from none to 5cf25d
           --- /tmp/test.txt	2018-10-16 06:53:47.343099574 +0000
           +++ /tmp/.chef-test20181016-4788-y84050.txt	2018-10-16 06:53:47.343099574 +0000
           @@ -1 +1,2 @@
           +あいうえお
           - restore selinux security context
0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?