Escape codes in collection types

658 views
Skip to first unread message

Joshua Schaeffer

unread,
Feb 1, 2017, 4:04:01 AM2/1/17
to Puppet Users
I'm new to Puppet and learning the program and language so this question might seem novice. I'm confused why I can assign an escape character to a variable but not inside any collection types (arrays or hashes). I was doing the simply motd puppet module and I created this:

$red = "^[[0;31m"
$green
= "^[[0;32m"
$white
= "^[[0;33m"
$bold
= "^[[1m"
$unbold
= "^[[22m"
...

Those "^[" characters are actual escape characters (a.k.a. echo -e \033[0;31m...). When I define my motd variable (which has the contents of /etc/motd) I can do this:

$motd = @("END")
You are connecting to: ${bold}${facts[hostname]}.${facts[networking]['domain']}${unbold}
...
...
END

This works when I apply my manifest. Well, I thought having all those variables is stupid so I wanted to put them in a hash and just reference them by key:

color = {
 
'red' => "^[[0;31m",
 
'green' => "^[[0;32m",
 
'white' => "^[[0;33m",
 
'bold' => "^[[1m",
 
'unbold' => "^[[22m",
}
...
$motd
= @("END")
You are connecting to: ${color['bold']}${facts[hostname]}.${facts[networking]['domain']}${color['unbold']}
...
...
END


However this produces an error

$puppet apply --environment sandbox ./manifests/
Error: Illegal attempt to assign to 'a Name'. Not an assignable reference at /etc/puppetlabs/code/environments/sandbox/modules/motd/manifests/init.pp:53:2 on node fenix.harmonywave.com

I get the same issue if I try using an array as well. I'm fine using a variable for each color, I'm just curious why they can't be assigned to collection types.

Thanks,
Joshua

Lowe Schmidt

unread,
Feb 1, 2017, 5:44:07 AM2/1/17
to puppet...@googlegroups.com
Did you omit the "$" here for the $color hash or is it missing in the source ?

--
Lowe Schmidt | +46 723 867 157

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/12bb88f5-07be-4ae8-9bfb-c4b044d26dd5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Henrik Lindberg

unread,
Feb 1, 2017, 8:45:59 AM2/1/17
to puppet...@googlegroups.com
On 01/02/17 00:20, Joshua Schaeffer wrote:
> I'm new to Puppet and learning the program and language so this question
> might seem novice. I'm confused why I can assign an escape character to
> a variable but not inside any collection types (arrays or hashes). I was
> doing the simply motd puppet module and I created this:
>
> |
> $red ="^[[0;31m"
> $green ="^[[0;32m"
> $white ="^[[0;33m"
> $bold ="^[[1m"
> $unbold ="^[[22m"
> ...
> |
>
> Those "^[" characters are actual escape characters (a.k.a. echo -e
> \033[0;31m...). When I define my motd variable (which has the contents
> of /etc/motd) I can do this:
>
You are probably much better of encoding the UTF-8 characters using \u
as that will not screw up things if you cat the file etc.

> |
> $motd =@("END")
> Youare connecting
> to:*$**{bold}*${facts[hostname]}.${facts[networking]['domain']}*${unbold}

The *{bold} seems to miss a $ to interpolate the variale.

> ...
> ...
> END
> *
> |
>
> This works when I apply my manifest. Well, I thought having all those
> variables is stupid so I wanted to put them in a hash and just reference
> them by key:
>
> |
> color ={

That should be $color =

> 'red'=>"^[[0;31m",
> 'green'=>"^[[0;32m",
> 'white'=>"^[[0;33m",
> 'bold'=>"^[[1m",
> 'unbold'=>"^[[22m",
> }
> ...
> $motd =@("END")
> Youare connecting
> to:*$**{color['bold']}*${facts[hostname]}.${facts[networking]['domain']}${color['unbold']}*
The *{color...} seems to miss a $ to interpolate the variable.
> ...
> ...
> END*
> |
>
> However this produces an error
>
> |
> $puppet apply --environment sandbox ./manifests/
> Error:Illegalattempt to assign to 'a Name'.Notan assignable reference at
> /etc/puppetlabs/code/environments/sandbox/modules/motd/manifests/init.pp:53:2on
> node fenix.harmonywave.com

The 53:2 should point at where the assignment to color (without $) is
done. The "color" is a NAME when not preceded by a $.

- henrik
--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Joshua Schaeffer

unread,
Feb 1, 2017, 12:25:53 PM2/1/17
to Puppet Users

That should be $color =


Wow, I knew it was something simple. I swear I looked at that for 10 minutes straight and couldn't find the syntax error. Thanks for pointing it out. I corrected the syntax error and it's all working correctly now.

Thanks,
Joshua 

Rob Nelson

unread,
Feb 1, 2017, 6:54:28 PM2/1/17
to puppet...@googlegroups.com
It might be a good idea to add `puppet parser validate` and puppet-lint to your pre commit hooks, they should help catch most similar issues.

--
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...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/dd919198-940a-41b7-8028-003227488d73%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
--
Rob Nelson

Christopher Wood

unread,
Feb 2, 2017, 3:25:55 PM2/2/17
to puppet...@googlegroups.com
This thing is nice that way:

https://github.com/drwahl/puppet-git-hooks

I recommend using the pre-receive hook on the server side. That way you can skip awkward conversations about how people should go the extra mile and exceed expectations by installing git hooks locally. Instead you get to have less awkward conversations about how people shouldn't push garbage to git repositories.

On Wed, Feb 01, 2017 at 11:54:04PM +0000, Rob Nelson wrote:
> It might be a good idea to add `puppet parser validate` and puppet-lint to
> your pre commit hooks, they should help catch most similar issues.
> On Wed, Feb 1, 2017 at 12:26 PM Joshua Schaeffer
> <[1]jschaef...@gmail.com> wrote:
>
> That should be $color =
>
> Wow, I knew it was something simple. I swear I looked at that for 10
> minutes straight and couldn't find the syntax error. Thanks for pointing
> it out. I corrected the syntax error and it's all working correctly now.
> Thanks,
> Joshua 
>
> --
> 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 [2]puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> [3]https://groups.google.com/d/msgid/puppet-users/dd919198-940a-41b7-8028-003227488d73%40googlegroups.com.
> For more options, visit [4]https://groups.google.com/d/optout.
>
> --
> Rob Nelson
>
> --
> 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 [5]puppet-users...@googlegroups.com.
> To view this discussion on the web visit
> [6]https://groups.google.com/d/msgid/puppet-users/CAC76iT9x6A4gdfNO-3UAKz7xeq%3DPi7kJF3eDT38nU49fh2%3D7BQ%40mail.gmail.com.
> For more options, visit [7]https://groups.google.com/d/optout.
>
> References
>
> Visible links
> 1. mailto:jschaef...@gmail.com
> 2. mailto:puppet-users...@googlegroups.com
> 3. https://groups.google.com/d/msgid/puppet-users/dd919198-940a-41b7-8028-003227488d73%40googlegroups.com?utm_medium=email&utm_source=footer
> 4. https://groups.google.com/d/optout
> 5. mailto:puppet-users...@googlegroups.com
> 6. https://groups.google.com/d/msgid/puppet-users/CAC76iT9x6A4gdfNO-3UAKz7xeq%3DPi7kJF3eDT38nU49fh2%3D7BQ%40mail.gmail.com?utm_medium=email&utm_source=footer
> 7. https://groups.google.com/d/optout
Reply all
Reply to author
Forward
0 new messages