3

i got the task to implement hiera puppet script to manage sites_enabled for nginx.

Here is my puppet script:

common.yaml

---
classes:
    - nginx

nginx:
    enabled:
        abc.com
        xyz.com
    disabled:
        test.com
        test2.com

init.pp

class nginx{
    create_resources("site_enabled", hiera("nginx"), {})
}

define site_enabled($name){
    file { '/etc/nginx/sites_enabled/${name}':
       ensure => 'link',
       target => '/etc/nginx/site_available/${name}',
    }
}

But i got the error when puppet executed:

err: Could not retrieve catalog from remote server: Error 400 on SERVER: can't convert String into Integer at /etc/puppet/modules/nginx/manifests/init.pp:7 on node XX

When i tried to query hiera via commandline:

$ hiera nginx

{"enabled"=>["abc.com", "xyz.com"]}

I know i have wrong some where. Please kindly correct me. i don't understand much, how hiera query and process with array data. please point me some useful document if it is possible.

Thanks so much.

Calvin Pham
  • 55
  • 1
  • 4

2 Answers2

3

Your questions is very similar to Problems creating Hiera hashes for create_resources, which has an answer. I'll provide a recap here.

According to the documentation for create_resources, the hash must be in the form {title => {parameters} }. You should edit your hiera data to set the parameters. Since there are none, I think it could just look like this:

common.yaml

---
classes:
    - nginx

nginx::enabled:
    abc.com: {}
    xyz.com: {}
nginx::disabled:
    test.com: {}
    test2.com: {}

Next, you need to actually load the right data from hiera. You want to load nginx::enabled, not all of nginx

init.pp

class nginx{
    create_resources("site_enabled", hiera("nginx::enabled"))
}

define site_enabled($name){
    file { '/etc/nginx/sites_enabled/${name}':
       ensure => 'link',
       target => '/etc/nginx/site_available/${name}',
    }
}
sciurus
  • 12,493
  • 2
  • 30
  • 49
  • Thanks for quick respond, i tried as your suggestion but i did not make it worked. i got the error "err: Could not retrieve catalog from remote server: Error 400 on SERVER: Could not find data item nginx::enabled in any Hiera data file and no default supplied at /etc/puppet/module". When i queried by command line: "hiera nginx {"enabled"=>{"abc.com"=>{}, "xyz.com"=>{}}, "disabled"=>{"test.com"=>{}, "test2.com"=>{}}}. hiera nginx::enabled nil " i use puppet 2.7.11 – Calvin Pham Dec 06 '13 at 03:20
  • Sorry, I made an error in the yaml, it should be corrected now. – sciurus Dec 06 '13 at 15:54
0

Thanks Sciunus for your quick respond.

May i ask for this question? i would like to make my yaml data following this kind of format:

nginx:

enabled:
    abc.com
    xyz.com
disabled:
    test.com
    test2.com

i DO NOT want to make the format like that:

nginx::enabled:

abc.com: {}
xyz.com: {} 

nginx::disabled:

test.com: {}
test2.com: {}

How can i make my puppet script worked as expected. i also read Hiera document, as i know, it is also supported three kinds of lookup data: hiera, hiera_hash and hiera_array. but i could not make it work as my expected following my yaml file format above

Sorry as i am very new with hiera.

Thanks in advanced.

Calvin Pham
  • 55
  • 1
  • 4