Ask Your Question

How to convert an ip to string in puppet

asked 2016-08-25 09:04:45 -0500

Edely gravatar image

updated 2016-09-06 09:00:15 -0500

I am using version 3.8 of puppet and I need to convert an ip address to a string. I tried the String function but I am getting an error.

This is the code I am using:

class resolver::params {
  $ip = String($::ipaddress)
  $octs = split($ip, '.')
  file{ '/tmp/teste.txt':
   content => $octs[0]

Here is the output:

Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Syntax error at '('; expected ')' at /etc/puppet/modules/resolver/manifests/params.pp:2 on node Warning: Not using cache on failed catalog Error: Could not retrieve catalog; skipping run

edit retag flag offensive close merge delete



Don't declare resources in the `params.pp` manifest.

Kai Burghardt gravatar imageKai Burghardt ( 2016-09-03 19:11:06 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2016-08-25 14:00:51 -0500

lupin gravatar image

updated 2016-08-26 11:33:51 -0500

You can create a function similar to this.

[root@ha usecase]# cat lib/puppet/parser/functions/ip_to_str.rb

module Puppet::Parser::Functions
  newfunction(:ip_to_str, :type => :rvalue) do |args|

Usage: [root@ha usecase]# tail -6 manifests/init.pp #

class usecase {

  $oct = ip_to_str()
  notify { "${oct}": }

An example output: [root@ha usecase]# facter ipaddress

[root@ha usecase]# puppet apply tests/init.pp Notice: Compiled catalog for ha.home in environment production in 0.02 seconds Notice: 10

Notice: /Stage[main]/Usecase/Notify[10]/message: defined 'message' as '10'

Notice: Finished catalog run in 0.02 seconds

edit flag offensive delete link more


Thanks. I tried this approach and put the function at resolver/lib/puppet/parser/functions/iptostr.rb And the calling in resolver/manifests/params.pp But now I am getting: Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Unknown function ip_to_str... Could you help, please?

Edely gravatar imageEdely ( 2016-08-26 07:43:21 -0500 )edit

the filename should be 'ip_to_str.rb' - ('ip' underscore 'to' underscore 'str' dot rb).

lupin gravatar imagelupin ( 2016-08-26 11:33:18 -0500 )edit

answered 2016-08-27 09:17:36 -0500

natemccurdy gravatar image

$::ipaddress is already a string, so there's no need to convert it.

Are you just trying to grab a single octet from the IP? If so, you already have the code there to do it...

$octs = split($::ipaddress, '.')
file{ '/tmp/test.txt':
  content => $octs[0]

Also, that error message you're getting is because this line is not valid Puppet code:

$ip = String($::ipaddress) $octs = split($ip, '.')

I'm not sure what you were trying to do there, but In Puppet 3, unless you turn the future parser on, you can't use the uppercase word, String.

edit flag offensive delete link more


She's in a module's `params.pp`. There are newlines inbetween which aren't displayed here since she didn't marked them up as code/pre.

Kai Burghardt gravatar imageKai Burghardt ( 2016-09-03 19:10:10 -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


Asked: 2016-08-25 09:04:45 -0500

Seen: 102 times

Last updated: Sep 06 '16