Search code examples
vagrantpuppetprovisioninghiera

Hiera variables inside a puppet manifest script


I have Hiera running in combination with puppet/vagrant .

Say I have a puppet exec doing following

      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],
  }

I want to replace the string "project_db" with a variable I have inside my common.yaml of hiera

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

How I go about this? The syntax is not clear for me, and something like this brings errors:

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

Solution

  • You have an issue with your quotes in 'hiera('project_db)' (i.e. you're not closing before the parentheses so hiera('project_db') should be correct)

    usually I do declare variables from hiera before so something like this

    $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],
    }