puppet host资源
host资源主要用来管理操作系统的hosts功能,hosts是一个没有扩展名的系统文件,基本作用就是关联ip和域名,当用户打开一个网址会首先从hosts解析,如果没有再去dns查找解析.linux系统存放在/etc/hosts文件中。
使用puppet describe host查看资源帮助信息:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
[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类.
1
2
3
4
5
6
7
8
9
|
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文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
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文件内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
node base {
include admin
}
node
/sh-
(proxy|web)\d+/ inherits base {
case
$::
hostname
{
/sh-proxy
\d+/: {
include nginx
}
"sh-web1"
: {
include
cron
}
}
}
|
说明:下面的更新是没有加"host_aliases"别名的更新,为了区别别名效果.
1
2
3
4
5
6
7
8
9
10
|
[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
|
1
2
3
4
5
6
7
8
9
10
|
[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"别名的更新,为了区别别名效果.
1
2
3
4
5
6
7
8
9
10
|
[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
|
1
2
3
4
5
6
7
8
9
10
|
[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的效果.
本文转自青衫解衣 51CTO博客,原文链接:http://blog.51cto.com/215687833/1971467