###前提・実現したいこと
consul-template(GO言語)で集約処理を書きたいです。シェルで言うと sort|uniq 、SQLでいうと、group by みたいなことです。データは JSON の文字列で、 consul の KVS に入っています。複数のデータであるフィールドに同じ値を持っている場合にその値ごとにテンプレートを展開したいです。
いま例として、以下のようなデータを考えます。
キー: www.example.com データ: '{"fqdn":"www.example.com","ip":"10.0.0.10/8"}' キー: portal.example.com データ: '{"fqdn":"portal.example.com","ip":"10.0.0.11/8"}' キー: mail.example.com データ: '{"fqdn":"mail.example.com","ip":"10.0.0.10/8"}' キー: dns.example.com データ: '{"fqdn":"dns.example.com","ip":"10.0.0.11/8"}' キー: dhcp.example.com データ: '{"fqdn":"dhcp.example.com","ip":"10.0.0.12/8"}' キー: file.example.com データ: '{"fqdn":"file.example.com","ip":"10.0.0.10/8"}'
というようなデータから ip フィールドを抜き出して集約して、
shell
1ip addr add 10.0.0.10/8 dev eth0 2ip addr add 10.0.0.11/8 dev eth0 3ip addr add 10.0.0.12/8 dev eth0
というようなシェルスクリプトを生成したいです。(これは、あくまで例ですので、シェルスクリプトで sort | uniq すればいいというツッコミはなしにしてください。他のミドルウェアの設定ファイルなども生成しますので、あくまで consul-template の機能で実現したいです)
正直、 Go 言語のことは全く知らずに見よう見まねコーディングである程度はやりたいことができましたが、ここへ来て困ってしまいました。consul-template のソースコードも少し見ましたが何を言ってるのかさっぱりです。
参考:hashicorp/consul-template
###発生している問題・エラーメッセージ
最新の consul-template_0.18.0-rc1 で搭載された scratch という機能を使えばできるのかなと思い、後述のテストスクリプトを実行してみましたが、以下のエラーが発生してすすみません。
template: template: :5:24: executing "" at <scratch.MapValues>: error calling MapValues: "ipmap" is not a map
###該当のソースコード
以下にテストプログラムとテスト手順を示します。
テンプレート: addvip.sh.tmpl
#!/bin/bash {{range ls "virtualIps"}}{{$vip := .Value | parseJSON}} {{- scratch.MapSet "ipmap" $vip.fqdn $vip.ip}} {{- end}} {{- range $ip := scratch.MapValues "ipmap"}} ip addr add {{$ip}} dev eth0 {{end}}
consul 実行方法
mkdir /tmp/consul ./consul agent -server -bootstrap -data-dir /tmp/consul -advertise 192.168.56.22
上記で端末が専有されるので、以下は別の端末より実行
curl -XPUT -d '{"fqdn":"www.example.com","ip":"10.0.0.10/8"}' http://localhost:8500/v1/kv/virtualIps/www.example.com curl -XPUT -d '{"fqdn":"portal.example.com","ip":"10.0.0.11/8"}' http://localhost:8500/v1/kv/virtualIps/portal.example.com curl -XPUT -d '{"fqdn":"mail.example.com","ip":"10.0.0.10/8"}' http://localhost:8500/v1/kv/virtualIps/mail.example.com curl -XPUT -d '{"fqdn":"dns.example.com","ip":"10.0.0.11/8"}' http://localhost:8500/v1/kv/virtualIps/dns.example.com curl -XPUT -d '{"fqdn":"dhcp.example.com","ip":"10.0.0.12/8"}' http://localhost:8500/v1/kv/virtualIps/dhcp.example.com curl -XPUT -d '{"fqdn":"file.example.com","ip":"10.0.0.10/8"}' http://localhost:8500/v1/kv/virtualIps/file.example.com
この状態で、 consul-template を以下のように実行するとエラーになります。
$ ./consul-template -template=addvip.sh.templ -once -dry Consul Template returned errors: template: template: :5:24: executing "" at <scratch.MapValues>: error calling MapValues: "ipmap" is not a map
###補足情報(言語/FW/ツール等のバージョンなど)
OS: CentOS 7.2.1511
consul: 0.7.1
ダウンロード元:DOWNLOAD CONSUL
consul-template: 0.18.0-rc1
ダウンロード元:consul template 0.18.0-rc1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。