簡體   English   中英

如何過濾人偶清單中的哈希?

[英]How to filter hash in puppet manifest?

請你幫助我好嗎。 我從人偶清單中的hiera獲取dict,然后嘗試過濾它們並將python腳本作為args傳遞。 但是不知道該怎么做。

我的等級:

myclass::server_conf:
 'first_serv':
   'serv_name': 'testname'
   'serv_hostname': 'testhost'
   'test_url': 'test@url.com'
 'second_serv':
   'serv_name': 'testname2'
   'serv_hostname': 'testhost2'
   'test_url': 'test@url.com2'

我的人偶清單(我正在從hiera中的值獲取哈希值):

 $server_conf = hiera_hash('myclass::server_conf', {})

結果,我有了:

{\"first_serv\"=>{\"serv_name\"=>\"testname\", \"serv_hostname\"=>\"testhost\", \"test_url\"=>\"test@url.com\"}, \"second_serv\"=>{\"serv_name\"=>\"serv2\", \"serv_name\"=>\"testname2\", \"serv_hostname\"=>\"testhost2\", \"test_url\"=>\"test@url.com2\"}}

然后我只想從此列表中選擇值:

'testname' 'testhost' 'test@url.com' 'testname2' 'testhost2' 'test@url.com2'

我正在嘗試使用地圖功能:

$transforrmed_data = map(server_conf) |$key,$value| { $value }

並得到錯誤:

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Could not match |$key,$value| at /manifests/server.pp:26 on node test.node

我怎么解決這個問題? 另外,我還需要轉移到另一個變量'testname2''testhost2''test@url.com2'並將其傳遞給exec命令資源。

謝謝!

看起來在Ask PuppetLabs論壇上有一個很好的例子: 從manifest中的hiera迭代嵌套的哈希

該解決方案利用運行您的exec的已定義類型 然后,只需使用create_resources()自動遍歷哈希即可 ,該哈希會將哈希轉換為一組資源並將其添加到目錄中。 使用此函數可以輕松地一次從Hiera數據源創建許多資源,而不必編寫自己的循環函數。 最好與定義的類型一起使用,因為它們可以在許多不同的時間實現。

我為您的目的改編了他們的示例:

define run_my_exec($serv_name, $serv_hostname, $test_url) {
  notify { "$serv_name": }
}

$server_conf = hiera_hash('myclass::server_conf', {})
create_resources( run_my_exec, $server_conf )

另外,在人偶中使用exec是代碼的味道。 並非總是很糟糕,但是通常這是解決問題的最不優雅的方法。 例如,這位高管正在配置您的服務器嗎? 如果是這樣,也許使用模板來寫配置文件會更好。 這是偽文件中針對該類型的exec的另一種觀點:

警告:使用執行程序集合來管理現有資源類型未涵蓋的資源的趨勢已廣泛存在。 這對於簡單的任務來說效果很好,但是一旦您的執行堆變得足夠復雜,以至於您真的不得不考慮了解正在發生的事情,則應該考慮開發自定義資源類型,因為它將更加可預測和可維護。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM