Thank you for your answer.

Le 17/09/2014 17:12, jcbollinger a écrit :

>> class my_module ( 
>>   $var1 = $my_module::params::var1, 
>>   $var2 = $my_module::params::var2, 
>> ) inherits my_module::params { 
>>
>>   include my_module 
>>
> 
> 
> There's no point to including class my_module inside its own definition.  
> Did you mean to include my_module::internal?

Yes exactly. Sorry for the mistake.

>> class my_module::internal { 
>>
>>   file { '/tmp/test_internal.txt': 
>>     content => template('test/test_internal.txt.erb'), 
>>   } 
>>
>>   notify { 'm1': message => "var1 = ${my_module::var1}", } 
>>   notify { 'm2': message => "var2 = ${my_module::var2}", } 
>>   notify { 'm3': message => "foo1 = ${my_module::foo1}", } 
>>   notify { 'm4': message => "foo2 = ${my_module::foo2}", } 
>>
>> } 
>>
>> It's curious because I use no inheritance in my_module::internal, 
>> no require etc. and despite that I can access to the variables of 
>> my_module. Is it normal? 
>>
>>
> 
> It is very poor form to access $my_module::params::foo1 and 
> $my_module::params::foo2 via inheriting class my_module (i.e. as 
> ${my_module::foo1}, etc.). 

But the my_module::internal inherits no class, so doesn't inherit
my_module class? I don't see where is the inheritance in the code
above.

Or, if I understand well, when I have:

class my_module {
    include my_module::internal
}

I can say: "my_module::internal" inherits my_module. Is that correct?


> With that out of the way, it is better to say 
> that the result you observe is among the valid possibilities than to say it 
> is "normal".
> 
> All class variables have global visibility regardless of inheritance or 
> declaration (e.g. via include / require).  

Do you mean that with:

class my_module {
    include my_module::internal
}

In the body of my_module::internal class I can access to the variables
defined in my_module class? With a unqualified name?

> The main purpose of all that *as 
> it pertains to variables* is to ensure that Puppet evaluates classes during 
> catalog compilation in an order that assigns values to those variables 
> before those values are used.  It also serves a documentary purpose, 
> showing clearly that one class has a dependency on another.
> 
>  
> 
>> One thing very curious too: in the my_module::internal class 
>> I can access to $var1 with this syntax : 
>>
>>     ${my_module::var1} 
>>
> 
> 
> No, you can't.  There is no $var1 in class my_module::internal, so you are 
> not accessing such a variable.  You are instead accessing 
> $my_module::params::var1 via an alias (that alias resulting from class 
> my_module inheriting my_module::params).  Don't use that alias, even in 
> class my_module.

Ok.

>> and $var1 is empty. Ok, I think I understand that. But in my 
>> template "test_internal.txt.erb" I can use directly: 
>>
>>     blabla <%= @var1 %> blabla 
>>
>> without the fully qualified name. Here, I don't understand. 
>>
> 
> 
> I don't believe you. 

I can give you a precise and quick (if you have already a Debian Wheezy)
process to reproduct this. I take a Debian Wheezy, updated and cleaned
(it's a simple VM in Virtualbox). And here is the process:

-------------------------------------------------------------------
# Quick install of puppetmaster
root@wheezy-clean:~# wget 
https://apt.puppetlabs.com/puppetlabs-release-$(lsb_release -sc).deb
root@wheezy-clean:~# dpkg -i puppetlabs-release-$(lsb_release -sc).deb
root@wheezy-clean:~# apt-get update
root@wheezy-clean:~# apt-get install puppetmaster

# I use puppet version 3.7.1
root@wheezy-clean:~# puppet -V
3.7.1

# In [main] section, I just:
#    1. comment this line -> #templatedir=$confdir/templates (deprecated)
#    2  add this line     -> environmentpath = $confdir/environments
root@wheezy-clean:~# vim /etc/puppet/puppet.conf 

root@wheezy-clean:~# cd /etc/puppet/environments/
root@wheezy-clean:/etc/puppet/environments# ls -l
total 4
drwxr-xr-x 4 root root 4096 Sep 18 04:35 example_env

# I create my conf (modules etc. in "production" directory)
# It's very basic. You can see my conf here 
http://sisco.laf.free.fr/divers/production.tar.gz
root@wheezy-clean:/etc/puppet/environments# wget 
http://sisco.laf.free.fr/divers/production.tar.gz
root@wheezy-clean:/etc/puppet/environments# tar -xf production.tar.gz 
root@wheezy-clean:/etc/puppet/environments# rm production.tar.gz 
root@wheezy-clean:/etc/puppet/environments# ls -l
total 8
drwxr-xr-x 4 root root 4096 Sep 18 04:35 example_env
drwxr-xr-x 4 1000 1000 4096 Sep 18 03:43 production

root@wheezy-clean:/etc/puppet/environments# apt-get install tree

# In ./manisfests/site.pp you'll can see that I just include
# the profile::test class.
# In ./modules/profiles/manifests/test.pp, I just declare
# the ::test class with one parameter
# And I think the "test" module will very easy to understand.
root@wheezy-clean:/etc/puppet/environments# tree production/
production/
├── manifests
│   └── site.pp
└── modules
    ├── profiles
    │   └── manifests
    │       └── test.pp
    └── test
        ├── manifests
        │   ├── init.pp
        │   ├── internal.pp
        │   └── params.pp
        └── templates
            ├── internal.txt.erb
            └── test.txt.erb

7 directories, 7 files

# I just add this line -> "192.168.0.115 puppet"
# where 192.168.0.115 is the IP address of my puppermaster
# I want to resolve the "puppet" short name.
root@wheezy-clean:/etc/puppet/environments# vim /etc/hosts

# I restart puppetmaster and run the agent.
root@wheezy-clean:/etc/puppet/environments# service puppetmaster restart
root@wheezy-clean:/etc/puppet/environments# puppet agent --test
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for wheezy-clean.chezmoi.priv
Info: Applying configuration version '1411008116'
Notice: module test class test -> foo1 = value_foo1
Notice: /Stage[main]/Test/Notify[n3]/message: defined 'message' as 'module test 
class test -> foo1 = value_foo1'
Notice: /Stage[main]/Test/File[/tmp/test.txt]/ensure: defined content as 
'{md5}f424081c82df8216c6085b29cedf3700'
Notice: module test class test -> foo2 = value_foo2
Notice: /Stage[main]/Test/Notify[n4]/message: defined 'message' as 'module test 
class test -> foo2 = value_foo2'
Notice: module test class test -> var2 = default_value_var2
Notice: /Stage[main]/Test/Notify[n2]/message: defined 'message' as 'module test 
class test -> var2 = default_value_var2'
Notice: module test class test::internal -> var2 = default_value_var2
Notice: /Stage[main]/Test::Internal/Notify[sn2]/message: defined 'message' as 
'module test class test::internal -> var2 = default_value_var2'
Notice: /Stage[main]/Test::Internal/File[/tmp/internal.txt]/ensure: defined 
content as '{md5}77dc0e02e3caa93db00c87d29330ce92'
Notice: module test class test::internal -> var1 = specific_value_var1
Notice: /Stage[main]/Test::Internal/Notify[sn1]/message: defined 'message' as 
'module test class test::internal -> var1 = specific_value_var1'
Notice: module test class test::internal -> foo1 = value_foo1
Notice: /Stage[main]/Test::Internal/Notify[sn3]/message: defined 'message' as 
'module test class test::internal -> foo1 = value_foo1'
Notice: module test class test::internal -> foo2 = value_foo2
Notice: /Stage[main]/Test::Internal/Notify[sn4]/message: defined 'message' as 
'module test class test::internal -> foo2 = value_foo2'
Notice: module test class test -> var1 = specific_value_var1
Notice: /Stage[main]/Test/Notify[n1]/message: defined 'message' as 'module test 
class test -> var1 = specific_value_var1'
Info: Creating state file /var/lib/puppet/state/state.yaml
Notice: Finished catalog run in 0.06 seconds

# And here is the result of my template.
root@wheezy-clean:/etc/puppet/environments# cat /tmp/internal.txt 
# Class test::internal
var1 = specific_value_var1    <---------- Here!!!
var2 = default_value_var2
foo1 = value_foo1
foo2 = value_foo2

root@wheezy-clean:/etc/puppet/environments# cat /tmp/test.txt 
# Class test
var1 = specific_value_var1
var2 = default_value_var2
foo1 = value_foo1
foo2 = value_foo2
-------------------------------------------------------------------

I have:

   var1 = specific_value_var1

in the /tmp/internal.txt file but in my template internal.txt.erb
I use :

    var1 = <%= @var1 %>

Did you have the same result? I don't understand and for me it's
not logical.

Another curious thing: even if I put:

    $var1 = "internal"

in the test::internal class, the /tmp/test.txt don't change.

> That form of reference would work of class 
> my_module::internal were inheriting my_module::params (or class my_module) 
> because of the aliasing mechanism I described.  It would also work if 
> my_module::internal had a parameter or an ordinary variable of its own 
> named "var1".  It will not work with the code you actually presented.

I understand. But in this case, where is my error in the
commands above?

I will continue to think about your long answers (thank you again
for the help). For starters, I would like to known where I'm
wrong in the example above. Maybe I'm very tired and I missed something
simple and stupid.


François Lafont


-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/541A57B1.4000207%40gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to