1

ホスト固有のトークン値を保持するために Hiera ハッシュを使用しています。ハッシュ内のキーは、ハッシュ値を呼び出すプロファイル モジュールで分類されるノードのホスト名/証明書名に対応します。ただし、モジュールを適用すると、ホストのハッシュ キーに対応する値が常に null になります。ここに私が取り組んでいるコードがあります。

hiera-file.yaml で

token_lookup:
  host-name1: 'abcdef123'
  host-name2: 'abbcde456'

そして profile.pp で

$_tokens = hiera_hash('token_lookup', undef, 'hiera-file')
$_specific_token = $_tokens["${::hostname}"]       <== never gets a value

ホスト名がハッシュのキーと一致することは確かです。問題は、ヒエラファイルから値を取得して適切に入力するための正しい構文は何ですか? アドバイスをいただきありがとうございます。

編集: 多くのホスト名がそうであるように、ハッシュ キーにリテラル '-' 文字が含まれている場合に問題を発見したと思います。ダッシュを含むキーを表示するようにハッシュを更新したので、より具体的な質問をする必要があります。二重引用符を使用してハッシュの値に含まれる文字をエスケープする方法についての記事をたくさん見ましたが、何も表示されません。 on yaml.org - キーの一部として表示される場合に文字をエスケープする方法について。この問題に関するヒントはありますか?YAML パーサーは、これが構文的に有効であることを示していますが、「-」をリテラル文字ではなくコレクション マーカーとして扱っていると思います。

4

2 に答える 2

2

Your code is correct, I test it as below, seems it didn't target the right yaml file in your environment. Check the hierarchy setting, and put the token key-value in the right place.

If I put the yaml file to global.yaml(If hiera can't find the key, it will always go to the last one in my hiera.yaml setting)

I rebuilt it with simplest setting:

$ cat /etc/hiera.yaml:

---
:backends:
  - yaml
:hierarchy:
  - defaults
  - "%{clientcert}"
  - "%{environment}"
  - global

:yaml:
# datadir is empty here, so hiera uses its defaults:
# - /var/lib/hiera on *nix
# - %CommonAppData%\PuppetLabs\hiera\var on Windows
# When specifying a datadir, make sure the directory exists.
  :datadir:

$ cat /var/lib/hiera/global.yaml
token_lookup:
  host-name1: 'abcdef123'
  host-name2: 'abbcde456'

$ cat profile.pp   
$_tokens = hiera_hash('token_lookup', undef, 'hiera-file')
notice ("tokens is $_tokens")
$_specific_token = $_tokens["${::hostname}"]
notice ("token is $_specific_token ")

Then I run puppet apply, I can see the result

$ FACTER_hostname='host-name1' puppet apply profile.pp --hiera_config /etc/hiera.yaml
Notice: Scope(Class[main]): tokens is host-name1abcdef123host-name2abbcde456
Notice: Scope(Class[main]): token is abcdef123
Notice: Compiled catalog for host-name1 in environment production in 0.04 seconds
Notice: Finished catalog run in 0.07 seconds

$ FACTER_hostname='host-name2' puppet apply profile.pp --hiera_config /etc/hiera.yaml
Notice: Scope(Class[main]): tokens is host-name1abcdef123host-name2abbcde456
Notice: Scope(Class[main]): token is abbcde456
Notice: Compiled catalog for host-name2 in environment production in 0.04 seconds
Notice: Finished catalog run in 0.02 seconds
root@ac976d6d79fb:~#
于 2015-05-31T03:36:49.577 に答える
0

hiera_hashはあなたが望むものではないと思います。

hiera_hash : ハッシュ マージ ルックアップを使用します。特定のキーの階層内のすべての値がハッシュであると想定し、各ハッシュの最上位のキーを 1 つのハッシュにマージします。ネストされた構造の場合、これは深いマージを行わないことに注意してください。

変化する:

$_tokens = hiera_hash('token_lookup', undef, 'hiera-file')

$_tokens = hiera('token_lookup', {}) #it will create empty hash if it couldn't find 'token_lookup' variable.

次の例も確認してください。

# /etc/puppet/hieradata/appservers.yaml
---
proxies:
  - hostname: lb01.example.com
    ipaddress: 192.168.22.21
  - hostname: lb02.example.com
    ipaddress: 192.168.22.28

# Get the structured data:
$proxies = hiera('proxies')
# Index into the structure:
$use_ip = $proxies[1]['ipaddress'] # will be 192.168.22.28
于 2015-05-29T07:06:38.683 に答える