puppet

IP address returning `1` instead of valid ip address. Puppet


I am currently running into an issue where the IP address is returning 1 instead of the IP address that I am passing in using FACTER. I have simplified the script I am running down to pass the output into a file. The IP address i am passing in via FACTER is resolving correctly when running puppet in debug mode (--debug). The command that I am running on the ubuntu 22.04 server is:

FACTER_host_server_public_fqdn=name.company.com FACTER_host_server_private_fqdn=name.random.company.com FACTER_puppet_server_fqdn=puppet.random.company.com FACTER_puppet_server_ips=173.0.1.256 /opt/puppetlabs/bin/puppet apply --modulepath=/etc/puppetlabs/code/environments/production/modules:$(pwd)/modules puppet-server-ips-test.pp 

The file that is being ran looks like this:

### $puppet_server_fqdn       = 'puppet.random.company.com'
### $puppet_server_ips        = '173.0.1.256' 

# Calculated variables
$puppet_server_name         = split($puppet_server_fqdn, '\.')[0]
$puppet_server_ip_addresses = split($puppet_server_ips, ',')
$puppet_server_is_localhost = '127.0.0.1' in $puppet_server_ip_addresses

file { '/tmp/puppet-test':
  ensure  => file,
  content => "
    name         => ${puppet_server_name},
    comment      => 'Puppet Server',
    host_aliases => ${puppet_server_fqdn},
    ip           => ${puppet_server_ips[0]},
  ",
}

The output into /tmp/puppet-test is the following:

name         => puppet,
comment      => 'Puppet Server',
host_aliases => puppet.random.company.com,
ip           => 1, 

As you can see it is out putting 1 for the ip rather than 173.0.1.256.

I have tried to work though why or where it is getting the 1 from but so far I have come up empty handed. Any help on this issue would be greatly appreciated.

Thank you in advance.

Please note that some of the values have been redacted / changed.


Solution

  • You are accessing the 0th element of $puppet_server_ips. That variable is of type String, and therefore would resolve to the very first character 1. You wanted to access the 0th element of the Array[String] returned by split($puppet_server_ips, ',') which is assigned to $puppet_server_ip_addresses:

    file { '/tmp/puppet-test':
      ensure  => file,
      content => "
        name         => ${puppet_server_name},
        comment      => 'Puppet Server',
        host_aliases => ${puppet_server_fqdn},
        ip           => ${puppet_server_ip_addresses[0]},
      ",
    }