Ask Your Question
2

Cannot reassign variable?

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

Red Cricket gravatar image

Hi,

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
/etc/puppetlabs/puppet/modules/network_setup/manifests
[root@ost-dev-00 manifests]# cat init.pp
class network_setup (
        $network_name   =       'public-abc',
        $subnet_name    =       'public-sub-abc',
        $segment        =       'abc',
        $cidr           =       '10.xxx.xxx.xxx/26',
        $pool_start     =       '10.xxx.xxx.180',
        $pool_end       =       '10.xxx.xxx.182',
        $dns_server     =       'xxx.xxx.xxx.xxx yyy.yyy.yyy.yyy',
)
{
        $network_type='vlan'
        $segment_id=''
        if str2bool("$is_virtual") {
                $network_type='flat'
                $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
5

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

lavaman gravatar image

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

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

edit flag offensive delete link more

Comments

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

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

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

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

Comments

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 -0500 )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

Stats

Asked: 2014-08-05 14:02:58 -0500

Seen: 10,408 times

Last updated: Aug 09 '14