使用p设置主机名?


13

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

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

谢谢

[编辑]抱歉,我应该提到我无主运行puppet,puppet首先安装,然后再设置其他所有内容。


可能有一个,但我不知道一个。我怀疑不是因为服务器签署了包含主机名的客户端证书。通常,主机名是在部署期间设置的,然后安装p并链接到服务器。我不知道您如何在安装后通过puppet自动设置它。
Sirex

抱歉,我应该提到我无主运行puppet,puppet首先安装,然后再设置其他所有内容。
Andrei Serdeliuc 2012年

Answers:


10

看看我对想法的“重新定义”定义。它假定为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",
    }
}

我基本上做同样的事情,除了我还有/etc/resolv.conf的文件资源,设置我的域。我也无主。
弗朗索瓦博索莱伊

1

创建一个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


注意,该$fqdn值必须正确。您也可以删除$rsyslog行。
confiq 2015年
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.