设为首页 收藏本站
查看: 1898|回复: 0

[经验分享] Puppet Host资源介绍(二十一)

[复制链接]

尚未签到

发表于 2017-10-31 16:13:26 | 显示全部楼层 |阅读模式
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
[iyunv@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
[iyunv@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
[iyunv@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
[iyunv@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
[iyunv@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的效果.



运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-406340-1-1.html 上篇帖子: Puppet File资源介绍(二十) 下篇帖子: Puppet user资源介绍(二十二)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表