Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Is there any way of setting a server's hostname using puppet?

I could write a custom type, but maybe there's a simpler way.

Thanks

[Edit] Sorry, I should've mentioned I run puppet masterless, puppet is setup first and then it sets up everything else.

share|improve this question
    
There may be one, but i don't know of one. I suspect there isn't because the server signs the client's certificate that includes the hostname. Normally the hostname is set during deployment, then puppet installed and linked to the server. I don't know how you'd automate the setting of it after install time via puppet. –  Sirex Jan 31 '12 at 12:45
    
Sorry, I should've mentioned I run puppet masterless, puppet is setup first and then it sets up everything else. –  Andrei Serdeliuc Jan 31 '12 at 12:47

2 Answers 2

up vote 6 down vote accepted

Take a look at my "renaming" definition for ideas. It assumes Debian, and might work on Ubuntu as well.

define rename() {
    # We only need puppet so we can restart it. In practice, there's
    # little point in renaming a machine through puppet without a
    # running puppet service
    include puppet::conf

    # We only need apt because puppet management of its package
    include apt

    host { "$hostname": ensure => absent }

    host { "$fqdn": ensure => absent }

    $alias = regsubst($name, '^([^.]*).*$', '\1')

    host { "$name":
        ensure => present,
        ip     => $ipaddress,
        alias  => $alias ? {
            "$hostname" => undef,
            default     => $alias
        },
        before => Exec['hostname.sh'],
    }

    file { '/etc/mailname':
        ensure  => present,
        owner   => 'root',
        group   => 'root',
        mode    => 644,
        content => "${name}\n",
    }

    file { '/etc/hostname':
        ensure  => present,
        owner   => 'root',
        group   => 'root',
        mode    => 644,
        content => "${name}\n",
        notify  => Exec['hostname.sh'],
    }

    exec { 'hostname.sh':
        command     => '/etc/init.d/hostname.sh start',
        refreshonly => true,
        notify      => Service['puppet'],
    }
} 

define rename::domain() {
    rename { "${hostname}.${name}": }

    common::line { 'remove_old_domain':
        ensure => absent,
        file   => '/etc/resolv.conf',
        line   => "domain $domain",
    }

    common::line { 'add_new_domain':
        ensure => present,
        file   => '/etc/resolv.conf',
        line   => "domain $name",
    }
}
share|improve this answer
    
I essentially do the same thing, except I also have a file resource for /etc/resolv.conf, setting my domain. I run masterless as well. –  François Beausoleil Feb 1 '12 at 15:49

Create a sethostname module. Here's the init.pp:

class sethostname {
  file { "/etc/hostname":
    ensure => present,
    owner => root,
    group => root,
    mode => 644,
    content => "$::fqdn\n",
    notify => Exec["set-hostname"],
  }
  exec { "set-hostname":
    command => "/bin/hostname -F /etc/hostname",
    unless => "/usr/bin/test `hostname` = `/bin/cat /etc/hostname`",
    notify => Service[$rsyslog::params::service_name],
  }
}

https://gist.github.com/VertigoRay/6024253

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.