puppet host資源
host資源主要用來管理操作系統的hosts功能,hosts是一個沒有擴展名的系統文件,基本作用就是關聯ip和域名,當用戶打開一個網址會首先從hosts解析,如果沒有再去dns查找解析.linux系統存放在/etc/hosts文件中。
使用puppet describe host查看資源幫助信息:
[root@sh-proxy2 local]# puppet describe host host ==== Installs and manages host entries. For most systems, these entries will just be in `/etc/hosts`, but some systems (notably OS X) will have different solutions. Parameters ---------- - **comment** A comment that will be attached to the line with a # character. - **ensure** The basic property that the resource should be in. Valid values are `present`, `absent`. - **host_aliases** Any aliases the host might have. Multiple values must be specified as an array. - **ip** The host's IP address, IPv4 or IPv6. - **name** The host name. - **target** The file in which to store service information. Only used by those providers that write to disk. On most systems this defaults to `/etc/hosts`. Providers --------- parsed
host資源常用的屬性:
host {"資源標題":
ensure
ip
name
host_aliases
target
}
資源參數意義:
ensure:確定該主機是否啓用,值爲present即啓用,值爲absent即關閉.
ip:主機的ip,支持ipv6.
name:主機名.
target:指定自定義host文件位置.
依舊使用old 三臺機器演示:
192.168.30.134 puppet
192.168.30.131 sh-web1(web用途)
192.168.30.132 sh-proxy2(proxy)
之前文章寫過定義admin模塊爲基礎模塊,所以將host資源加入admin模塊即可使用。
說明:admin模塊的init.pp文件聲明admin::hosts類.
class admin { include admin::hosts exec {"selinux": command => "sed -i '/^SELINUX=/s/=.*/=disabled/g' /etc/sysconfig/selinux", path => ["/bin/","/sbin/","/usr/bin/","/usr/sbin/"], user => root, group => root, } }
說明:admin的manifests資源目錄下新建hosts.pp文件:
class admin::hosts { host {"puppet": ensure => present, ip => "192.168.30.134", } host {"sh-web1": ensure => present, ip => "192.168.30.131", } host {"sh-proxy2": ensure => present, ip => "192.168.30.132", } host {"test.example.com": ensure => present, host_aliases => ["db","web"], ip => "192.168.30.137", } }
puppet 入口文件nodes.pp所有主機都繼承admin.
說明:nodes.pp文件內容:
node base { include admin } node /sh-(proxy|web)\d+/ inherits base { case $::hostname { /sh-proxy\d+/: { include nginx } "sh-web1": { include cron } } }
說明:下面的更新是沒有加"host_aliases"別名的更新,爲了區別別名效果.
[root@sh-proxy2 local]# puppet agent -t Info: Retrieving pluginfacts Info: Retrieving plugin Info: Loading facts Info: Caching catalog for sh-proxy2.localdomain Info: Applying configuration version '1507646562' Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully Notice: /Stage[main]/Admin::Hosts/Host[test.example.com]/ensure: created Info: Computing checksum on file /etc/hosts Notice: Finished catalog run in 0.23 seconds
[root@sh-proxy2 local]# cat /etc/hosts # HEADER: This file was autogenerated at Tue Oct 10 22:42:50 +0800 2017 # HEADER: by puppet. While it can still be managed manually, it # HEADER: is definitely not recommended. 127.0.0.1localhostlocalhost.localdomain localhost4 localhost4.localdomain4 ::1localhostlocalhost.localdomain localhost6 localhost6.localdomain6 192.168.30.134puppet 192.168.30.131sh-web1 192.168.30.132sh-proxy2 192.168.30.137test.example.com
說明:下面的更新是加了"host_aliases"別名的更新,爲了區別別名效果.
[root@sh-proxy2 local]# puppet agent -t Info: Retrieving pluginfacts Info: Retrieving plugin Info: Loading facts Info: Caching catalog for sh-proxy2.localdomain Info: Applying configuration version '1507646705' Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully Notice: /Stage[main]/Admin::Hosts/Host[test.example.com]/host_aliases: defined 'host_aliases' as 'db web' Info: Computing checksum on file /etc/hosts Notice: Finished catalog run in 0.23 seconds
[root@sh-proxy2 local]# cat /etc/hosts # HEADER: This file was autogenerated at Tue Oct 10 22:45:07 +0800 2017 # HEADER: by puppet. While it can still be managed manually, it # HEADER: is definitely not recommended. 127.0.0.1localhostlocalhost.localdomain localhost4 localhost4.localdomain4 ::1localhostlocalhost.localdomain localhost6 localhost6.localdomain6 192.168.30.134puppet 192.168.30.131sh-web1 192.168.30.132sh-proxy2 192.168.30.137test.example.comdb web
總結:其實方法不止這一種,hosts文件上篇寫了file資源用法,可以直接寫一個/etc/hosts file文件,所有主機下發這個文件也可以起到配置/etc/hosts的效果.