Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It doesn't seem that you can access a defined type's attributes. <a href="http://docs.puppetlabs.com/guides/scope_and_puppet.html" rel="nofollow">Possible explanation here</a>. What you can do however, is externalize it via hiera.</p> <p>Hiera is a great way to separate your manifest logic from the data populating it, but it is not difficult to set up.</p> <p><a href="http://docs.puppetlabs.com/hiera/1/installing.html" rel="nofollow">Installing</a></p> <p>In my first try I was trying to access hiera variables by class reference; <code>foo::a</code> for example, but that doesn't work for defined types.</p> <p>Using <a href="http://drewblessing.com/blog/-/blogs/puppet-hiera-implement-defined-resource-types-in-hiera" rel="nofollow">http://drewblessing.com/blog/-/blogs/puppet-hiera-implement-defined-resource-types-in-hiera</a> as a guide, you can put declare all those attributes in hiera with a simple config:</p> <p><a href="http://docs.puppetlabs.com/hiera/1/configuring.html" rel="nofollow">Configuring</a>: </p> <p>hiera.yaml</p> <pre><code>:backends: - yaml :yaml: :datadir: $hiera_dir :hierarchy: - common </code></pre> <p>$hiera_dir/common.yaml</p> <pre><code>foo: a: x: 'oh bar may you reference me' y: 'please' </code></pre> <p>And then in your puppet manifest:</p> <pre><code>define foo ($x, $y) { } define bar($foo) { require create_my_foos $all_foos = hiera('foo') # This is just for proof of concept, to show that the variable can be passed. file { '/tmp/output.txt': content =&gt; $all_foos[$foo]['x'] } } class create_my_foos { $foo_instances = hiera('foo', []) create_resources('foo', $foo_instances) } bar { 'b': foo =&gt; 'a' } </code></pre> <p>Now you can access foo's variables by calling the <code>hiera('foo')</code> function to get an array of <code>foo</code>'s attributes, and do array lookups to get the exact parameter you need.</p> <p>Note that hiera <a href="http://docs.puppetlabs.com/hiera/1/puppet.html#interacting-with-structured-data-from-hiera" rel="nofollow">only looks up <strong>top-level keys</strong></a>, so you can't do <code>hiera('foo'['a']['x])</code>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload