Ask Your Question

How to call hiera in a class definition?

asked 2016-07-17 13:17:18 -0600

karthick gravatar image

updated 2016-07-17 23:11:55 -0600

I am facing an issue in calling a hiera key with in a class.. I am playing with hiera to learn how it works and so the idea is to create set of users and groups. My class looks like the following.

class musers
( $users = hiera("in_users", $groups = hiera("in_groups"))
  create_resources(user, $users)
  create_resources(group, $groups)

where inusers and ingroups are the key holding the value and my hiera data looks like the following.

    ensure: present
    home: /home/in/user1
    managehome: true
    shell: /bin/bash
    uid: 10001
    gid: 10001
    password: $1$bM0KFO/z$usCU/t3/NiaL2YBRkoSTM.
    ensure: present
    home: /home/in/user2
    managehome: true
    shell: /bin/bash
    uid: 10002
    gid: 10002
    password: $1$bM0KFO/z$usCU/t3/NiaL2YBRkoSTM.
    ensure: present
    gid: 601
    ensure: present
    gid: 602
    ensure: present
    gid: 603

Issue:- I have assigned the musers class to the agent via PE Console and did 'puppet agent --test' and it is giving out the following error.

[root@agent1 ~]# puppet agent --test
Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Syntax error at '{' at /etc/puppetlabs/code/environments/production/modules/musers/manifests/init.pp:4:1 on node agent
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-07-18 02:26:40 -0600

lupin gravatar image

You have wrong syntax. Here's the corrected one.

class musers ( 
   $users = hiera("in_users"),
   $groups = hiera("in_groups"),
) {
  create_resources(user, $users)
  create_resources(group, $groups)
edit flag offensive delete link more


Thank you lupin, that helped.. Is there a better way to call the hiera ? or ths is the standard way of doing it? Also how do we mention the dependency's? From d above class only user creation is displayed and the result doesn't look like d groups are created. But still the group is getting created.

karthick gravatar imagekarthick ( 2016-07-18 05:24:35 -0600 )edit

answered 2016-07-18 00:26:24 -0600

kadal gravatar image

Hi Karthick,

your code is correct but having syntax error.Add "" ) " after hiera("in_users").

class musers ( $users = hiera("inusers"), $groups = hiera("ingroups")) { createresources(user, $users) createresources(group, $groups) }

your hiera.yaml file should have your .yaml file name

:backends: - yaml :hierarchy: - defaults - "%{clientcert}" - "%{environment}" - global - hierausers :yaml:

datadir is empty here, so hiera uses its defaults:

- /var/lib/hiera on *nix

- %CommonAppData%\PuppetLabs\hiera\var on Windows

When specifying a datadir, make sure the directory exists.

:datadir: /etc/puppetlabs/puppet/hieradata

edit flag offensive delete link more


How can i define the dependencies when i use create_resources ? for example, i want to ensure there is a directory is present inside /home/in before the users are getting created.. So how can i prioritize which should be executed first?

karthick gravatar imagekarthick ( 2016-07-18 21:27:01 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-07-17 06:40:37 -0600

Seen: 205 times

Last updated: Jul 18 '16