Ask Puppet: Puppet DevOps Q&A Community - RSS feedhttps://ask.puppet.com/questions/Questions and Answers about the Puppet ecosystemenPuppet, 2012-2018Fri, 10 Jun 2016 02:50:37 -0500Get variable from module A into module Bhttps://ask.puppet.com/question/26510/get-variable-from-module-a-into-module-b/Hi,
I'm trying to get a variable value from one class into another class (different modules) but run into issues.
From what I have read in the docs you are supposed to be able to include a class multiple times without running into "already declared..." ?!
What I'm trying to do:
Module A (class "abc")
----------------------
abcvar = 'something'
Module B (class "xyz")
----------------------
include abc
notify {"######my var is: ${$abc::abcvar}": }
site.pp
-------
node 'my-test-node' {
class {'abc':
abcvar => 'somethingelse',
}
include xyz
}
Result
------
Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Evaluation Error: Error while evaluating a Resource Statement, Duplicate declaration: Class[abc] is already declared; cannot redeclare at /etc/puppetlabs/code/environments/puppet_test/manifests/site.pp:51 at /etc/puppetlabs/code/environments/puppet_test/manifests/site.pp:51:3 on node my-test-node
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run
Would this be possible in any other way or am I doing it totally wrong?
Many thanks for your feedback
Regards
TomasThu, 09 Jun 2016 04:33:33 -0500https://ask.puppet.com/question/26510/get-variable-from-module-a-into-module-b/Answer by Alex Harvey for <p>Hi,
I'm trying to get a variable value from one class into another class (different modules) but run into issues.
From what I have read in the docs you are supposed to be able to include a class multiple times without running into "already declared..." ?!</p>
<p>What I'm trying to do:</p>
<h2>Module A (class "abc")</h2>
<pre><code>abcvar = 'something'
</code></pre>
<h2>Module B (class "xyz")</h2>
<pre><code> include abc
notify {"######my var is: ${$abc::abcvar}": }
</code></pre>
<h2>site.pp</h2>
<pre><code> node 'my-test-node' {
class {'abc':
abcvar =&gt; 'somethingelse',
}
include xyz
}
</code></pre>
<p>Result</p>
<hr>
<p>Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Evaluation Error: Error while evaluating a Resource Statement, Duplicate declaration: Class[abc] is already declared; cannot redeclare at /etc/puppetlabs/code/environments/puppet<em>test/manifests/site.pp:51 at /etc/puppetlabs/code/environments/puppet</em>test/manifests/site.pp:51:3 on node my-test-node
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run</p>
<p>Would this be possible in any other way or am I doing it totally wrong?
Many thanks for your feedback</p>
<p>Regards
Tomas</p>
https://ask.puppet.com/question/26510/get-variable-from-module-a-into-module-b/?answer=26514#post-id-26514 The issue is that while you can `include foo` using the `include` function as many times as you like, you can't use the resource-like declarations `class { 'foo': abcvar => 'something' }`. What can you do, however, is use the Automatic Parameter Lookup in conjunction with Hiera.Thu, 09 Jun 2016 07:43:55 -0500https://ask.puppet.com/question/26510/get-variable-from-module-a-into-module-b/?answer=26514#post-id-26514Comment by tomasl for <p>The issue is that while you can <code>include foo</code> using the <code>include</code> function as many times as you like, you can't use the resource-like declarations <code>class { 'foo': abcvar =&gt; 'something' }</code>. What can you do, however, is use the Automatic Parameter Lookup in conjunction with Hiera.</p>
https://ask.puppet.com/question/26510/get-variable-from-module-a-into-module-b/?comment=26515#post-id-26515Thanks Alex!
So that would mean I'd have to redo module A to use Hiera in some way?
I tried in module B with:
$local = lookup({"name" => "abc::abcvar"})
But it did not find any value.
Unfortunately I'm not familiar with HieraThu, 09 Jun 2016 08:38:04 -0500https://ask.puppet.com/question/26510/get-variable-from-module-a-into-module-b/?comment=26515#post-id-26515Comment by Alex Harvey for <p>The issue is that while you can <code>include foo</code> using the <code>include</code> function as many times as you like, you can't use the resource-like declarations <code>class { 'foo': abcvar =&gt; 'something' }</code>. What can you do, however, is use the Automatic Parameter Lookup in conjunction with Hiera.</p>
https://ask.puppet.com/question/26510/get-variable-from-module-a-into-module-b/?comment=26531#post-id-26531According to your example code, your site.pp is making the resource-like declaration of the class. So instead of passing the parameter in there, you'd have ::abc::abcvar: 'something' in one of your Hiera files. Feel free to open another question if you get stuck with Hiera.Fri, 10 Jun 2016 02:28:56 -0500https://ask.puppet.com/question/26510/get-variable-from-module-a-into-module-b/?comment=26531#post-id-26531Comment by tomasl for <p>The issue is that while you can <code>include foo</code> using the <code>include</code> function as many times as you like, you can't use the resource-like declarations <code>class { 'foo': abcvar =&gt; 'something' }</code>. What can you do, however, is use the Automatic Parameter Lookup in conjunction with Hiera.</p>
https://ask.puppet.com/question/26510/get-variable-from-module-a-into-module-b/?comment=26533#post-id-26533Ok, many thanks for your input!Fri, 10 Jun 2016 02:50:37 -0500https://ask.puppet.com/question/26510/get-variable-from-module-a-into-module-b/?comment=26533#post-id-26533