Ask Your Question

Cannot reassign variable?

asked 2014-08-05 14:02:58 -0600

Red Cricket gravatar image


I am trying to test out some simple puppet code with puppet apply -e 'include::network_setup'.

Here's the code:

[root@ost-dev-00 manifests]# pwd
[root@ost-dev-00 manifests]# cat init.pp
class network_setup (
        $network_name   =       'public-abc',
        $subnet_name    =       'public-sub-abc',
        $segment        =       'abc',
        $cidr           =       '',
        $pool_start     =       '',
        $pool_end       =       '',
        $dns_server     =       ' yyy.yyy.yyy.yyy',
        if str2bool("$is_virtual") {
                $segment_id="--provider:segmentation_id $segment"

        notity { 'create_network':
                message         => "/usr/bin/neutron net-create $network_name --router:external=True $segmnet_id --provider:network_type $network_type --provider:physical_network physnet1 --shared=True",

        notify { 'create_subnet':
                message         => "/usr/bin/neutron subnet-create $network_name $cidr --name $subnet_name --enable_dhcp=True --allocation-pool start=$pool_start,end=$pool_end --dns_nameservers list=true $dns_servers",

When I execute ...

[root@ost-dev-00 manifests]# puppet apply -e 'include network_setup'

... I get this output ...

Error: Cannot reassign variable network_type at /etc/puppetlabs/puppet/modules/network_setup/manifests/init.pp:14 on node ost-dev-00    
Error: Cannot reassign variable network_type at /etc/puppetlabs/puppet/modules/network_setup/manifests/init.pp:14 on node ost-dev-00

... I do not understand why I cannot set the varibale $network_type to flat. Any help is appreciated. Thanks!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2014-08-05 17:27:53 -0600

lavaman gravatar image

updated 2014-08-05 17:28:08 -0600

Simple. The puppet DSL explicitly does not allow variable re-assignment.

edit flag offensive delete link more


can someone explain is there any logic in this? :(

confiq gravatar imageconfiq ( 2015-01-12 09:47:09 -0600 )edit

so... the are not variable. Puppet variables are immutable? Therefore, they should stop calling them variables and call them constants, shouldn't they?

ilvidel gravatar imageilvidel ( 2018-11-26 08:29:31 -0600 )edit

answered 2014-08-09 23:00:19 -0600

Gary Steven gravatar image

Try something like this

 class myclass (
   $network_type = 'vlan'
) inherits myclass::params {

  case $is_virtual {
    true: {
      $_network_type = 'flat'
    default: {
      $_network_type = $network_type
  notify { "Network Type: ${_network_type}": }

Basically once a variable is assigned you cannot reassign it, you can however assign it to a new variable and use that instead inside your code.

edit flag offensive delete link more


So puppet variables are more like constants in other languages. I have to keep reminding myself that puppet is a "declarative" language.

Red Cricket gravatar imageRed Cricket ( 2014-08-10 21:19:44 -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: 2014-08-05 14:02:58 -0600

Seen: 16,388 times

Last updated: Aug 09 '14