2015-09-08 71 views
0

我有Hiera與puppet/vagrant結合運行。傀儡清單腳本內的Hiera變量

說我有一個傀儡高管做以下

 exec { create-project-database: 
    command => 'mysql -u root -e "CREATE DATABASE project_db DEFAULT CHARACTER SET = \'utf8\';"', 
    unless => 'mysql -u root information_schema -e "select * from information_schema.schemata;" | grep "project_db"', 
    require => Package[mysql-server], 
    } 

我想用一個變量我裏面有替換的字符串"project_db"我hiera

--- 
machine_message: 'This is the dev machine!' 
codes_path: '/vagrant/code/laravel/' 
project_db: 'project_db' 

我如何去這樣的common.yaml?語法是不明確的我,像這樣的錯誤帶來的:

command => 'mysql -u root -e "CREATE DATABASE 'hiera('project_db)' DEFAULT CHARACTER SET = \'utf8\';"', 

回答

1

你必須在你的'hiera('project_db)'引號(即你沒有括號前關閉,以便hiera('project_db')應該是正確的)

問題

通常我這樣做這樣的事情

$DB_NAME = hiera('project_db') 
exec { create-project-database: 
    command => "mysql -u root -e \"CREATE DATABASE $DB_NAME DEFAULT CHARACTER SET = 'utf8';\"", 
    unless => 'mysql -u root information_schema -e "select * from information_schema.schemata;" | grep "project_db"', 
    require => Package[mysql-server], 
} 
+0

'命令之前宣佈從hiera變量=> '的mysql -u根-e 「CREATE DATABASE $ DB_NAME默認字符集= \ 'UTF8 \';」','這帶來一個錯誤做'vagrant provision'...我認爲'$ DB_NAME'仍然n提供某種引用或者不同的東西。 – Confidence

+1

我在exec中使用幾乎相同,它運行良好。你可以在調試模式下運行puppet provisioning,並在exec塊中添加debug(「」CREATE DATABASE $ DB_NAME「)以查看它打印的內容 –

+0

'==>默認:錯誤:mysql -u root -e」CREATE DATABASE $ dbName DEFAULT CHARACTER SET ='utf8';「返回1而不是[0] ==>默認值:錯誤:/ Stage [main]/Initenv/Exec [create-project-database]/returns:從notrun更改爲0失敗:mysql -u root -e「CREATE DATABASE $ dbName DEFAULT CHARACTER SET ='utf8';」返回1而不是[0]中的一個' – Confidence