Facter包含GCE ()元数据详细信息:
$ facter | grep gce
gce => {"instance"=>{"attributes"=>{}, "description"=>"", "disks"=>[{"deviceName"=>"srvpup01", "index"=>0, "mode"=>"READ_WRITE", "type"=>"PERSISTENT"}, {"deviceName"=>"srvpup01-storage01", "index"=>1, "mode"=>"READ_WRITE", "type"=>"PERSISTENT"}], "hostname"=>"srvpup01.c.example.internal", "id"=>12345, "image"=>nil, "licenses"=>[{"id"=>"1000010"}], "machineType"=>"n1-standard-1", "maintenanceEvent"=>"NONE", "networkInterfaces"=>[{"accessConfigs"=>[{"externalIp"=>"", "type"=>"ONE_TO_ONE_NAT"}], "forwardedIps"=>[], "ip"=>"123.456.789.123", "ipAliases"=>[], "mac"=>"00:11:22:33:44:55", "network"=>"example"}], "scheduling"=>{"automaticRestart"=>"TRUE", "onHostMaintenance"=>"MIGRATE", "preemptible"=>"FALSE"}, "serviceAccounts"=>{"12345-compute@developer.gserviceaccount.com"=>{"aliases"=>["default"], "email"=>"12345-compute@developer.gserviceaccount.com", "scopes"=>["xxx"]}, "default"=>{"aliases"=>["default"], "email"=>"12345-compute@developer.gserviceaccount.com", "scopes"=>["xxx"]}}, "tags"=>["no-public-ip"], "zone"=>"europe-west1-d"}, "project"=>{"attributes"=>{"google-compute-default-region"=>"europe-west1", "google-compute-default-zone"=>"europe-west1-d", "sshKeys"=>["...是否有任何简单的方法从木偶模块中访问"zone“属性,或者我必须自己解析该字符串?
像散列一样访问它会失败:
gce is not a hash or array发布于 2016-07-27 13:39:03
错误gce is not a hash or array表示您使用的是Puppe3.x(而不是4.x),它将所有事实视为字符串,因此要访问哈希中的值,需要关闭stringify_facts设置。
可以在puppet.conf中对所有代理执行以下操作:
stringify_facts = false然后,您应该能够使用以下方法访问该值:
$gce["zone"]发布于 2016-07-27 09:39:41
我认为facter命令行不能输出嵌套事实(如gce.zone )的值,因此您可能需要解析它。
请注意以下几点:
facter gce将只打印没有"gce =>“前缀的散列,或者需要输出grep。使用jgrep,您可以:
facter --json gce | jgrep -s gce.zone或者使用YAML和Ruby可以:
facter --yaml gce | ruby -ryaml -e 'p YAML.load(STDIN)["gce"]["zone"]'或者使用YAML和awk:
facter --yaml gce | awk '/zone:/ { print $2 }'https://serverfault.com/questions/792191
复制相似问题