使用 puppet 设置主机名?

使用 puppet 设置主机名?

有没有办法使用 puppet 设置服务器的主机名?

我可以编写自定义类型,但也许有更简单的方法。

谢谢

[编辑] 抱歉,我应该提到我运行的是 puppet masterless,puppet 是先设置好的,然后再设置其他一切。

答案1

请查看我的“重命名”定义以获取想法。它假设 Debian,也可能适用于 Ubuntu。

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",
    }
}

答案2

创建一个 sethostname 模块。以下是init.pp

class sethostname {
  file { "/etc/hostname":
    ensure  => present,
    owner   => root,
    group   => root,
    mode    => '0644',
    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

相关内容