0

I have an exec command to change the hostname. I only want to execute that if the hostname is not what it should be:

exec { 'update hostname':
  command => 'sudo hostname VM01',
  unless  => '[[ "$(hostname)" == "VM01" ]]';
}

Now the [[ "$(hostname)" == "VM01" ]] command seems to work just as expected if I execute it manually but Puppet seems to ignore it.

I also tried:

test "$(hostname)" == "VM01"
test $(hostname) == "VM01"
[ <(hostname) == "VM01" ]
[ hostname == "VM01" ]

Can some one point out my mistake I just don't get it.

HopelessN00b
  • 53,795
  • 33
  • 135
  • 209
Mario
  • 101
  • 1
  • 2

1 Answers1

0

It would have been good if you also posted the error message you got.
This code works for me:

exec { 'update hostname':
    command => 'hostname VM01',
    path => '/usr/bin:/usr/sbin/:/bin:/sbin',
    unless => '[ "$(hostname)" == "VM01" ]',
}

You need to specify a path if you don't use fully qualified paths in the commands you execute.

Also note, since you didn't specify which distribution you use: you probably want to change the hostname in more places, so it boots already with the correct hostname.
But I guess this is just a very short snippet of your full manifest.

faker
  • 17,496
  • 2
  • 60
  • 70