• Puppet扩展篇2-如何使用虚拟资源解决puppet冲突问题
  • 1. 定义两个用户,puppet和root,并将其虚拟化
    • 1.1 在全局作用域中创建对应的pp文件
    • 1.2 创建虚拟用户puppet、root、xiaonuo
    • 1.3 创建虚拟组puppet、root和xiaonuo
  • 2. 编写puppet模块,将虚拟资源用户puppet和组puppet实化
    • 2.1 编写pupppet模块
    • 2.2 编写puppet_linux57poc模块
    • 2.3 实例化虚拟资源
  • 3. 测试
    • 3.1 测试puppet模块(略)
    • 3.2 测试puppet_linux57poc模块

    Puppet扩展篇2-如何使用虚拟资源解决puppet冲突问题

    虚拟资源是一种用来管理多种配置共同依赖同一资源的方法。如果多个类依赖同一个资源时则可避免写多个资源,也可以解决资源重定义的错误。
    虚拟资源经常用于用户管理中,虚拟资源只会被声明一次,但可以运用一次或多次。

    要使用虚拟资源是需要在资源声明开头加上字符“@”来使资源虚拟化。然后再使用下面两种方法之一来实例化虚拟资源:

    • “飞船”语法<||>
    • realize函数

    1. 定义两个用户,puppet和root,并将其虚拟化

    注意定义虚拟资源必须在全局作用域或者节点作用域中定义,简单的理解,以下目录中site.pp就是全局作用域,包含nodes目录(site.pp中import了nodes目录),在节点node下定义的虚拟资源属于节点作用域,其他模块中的都属于局部作用域。

    1.1 在全局作用域中创建对应的pp文件

    1. [root@linuxmaster1poc testing]# tree manifests/
    2. manifests/
    3. ├── nodes
    4. ├── puppetclient.pp
    5. ├── virtual_group.pp
    6. └── virtual_user.pp
    7. └── site.pp
    8. 1 directory, 4 files

    1.2 创建虚拟用户puppet、root、xiaonuo

    1. [root@linuxmaster1poc testing]# vim manifests/nodes/virtual_user.pp
    2. class alluser{
    3. include alluser::puppet,alluser::root
    4. }
    5. class alluser::puppet{
    6. @user { 'puppet':
    7. ensure => present,
    8. uid => '52',
    9. gid => '52',
    10. home => '/var/lib/puppet',
    11. shell => '/sbin/nologin',
    12. }
    13. }
    14. class alluser::root{
    15. @user { 'root':
    16. ensure => present,
    17. uid => '0',
    18. gid => '0',
    19. home => '/root',
    20. shell => '/bin/bash',
    21. }
    22. }
    23. class alluser::xiaonuo{
    24. @user { 'xiaonuo':
    25. ensure => present,
    26. uid => '600',
    27. gid => '600',
    28. home => '/home/xiaonuo',
    29. shell => '/sbin/nologin',
    30. }
    31. }

    1.3 创建虚拟组puppet、root和xiaonuo

    1. [root@linuxmaster1poc testing]# vim manifests/nodes/virtual_group.pp
    2. class allgroup{
    3. include allgroup::puppet,allgroup::root
    4. }
    5. class allgroup::puppet{
    6. @group { 'puppet':
    7. ensure => present,
    8. name => 'puppet',
    9. gid => '52',
    10. allowdupe => false,
    11. members => 'puppet',
    12. }
    13. }
    14. class allgroup::root{
    15. @group { 'root':
    16. ensure => present,
    17. name => 'root',
    18. gid => '0',
    19. allowdupe => false,
    20. members => 'root',
    21. }
    22. }
    23. class allgroup::xiaonuo{
    24. @group { 'xiaonuo':
    25. ensure => present,
    26. name => 'xiaonuo',
    27. gid => '600',
    28. allowdupe => false,
    29. members => 'xiaonuo',
    30. }
    31. }

    2. 编写puppet模块,将虚拟资源用户puppet和组puppet实化

    2.1 编写pupppet模块

    1. [root@linuxmaster1poc testing]# tree environment/modules/puppet
    2. environment/modules/puppet
    3. ├── files
    4. ├── manifests
    5. ├── config.pp
    6. ├── init.pp
    7. ├── install.pp
    8. ├── params.pp
    9. └── service.pp
    10. ├── README
    11. └── templates
    12. ├── auth.conf.erb
    13. ├── namespaceauth.conf.erb
    14. └── puppet.conf.erb
    15. 3 directories, 9 files

    2.2 编写puppet_linux57poc模块

    1. [root@linuxmaster1poc testing]# tree agents/modules/puppet_linux57poc/
    2. agents/modules/puppet_linux57poc/
    3. ├── files
    4. ├── manifests
    5. └── init.pp
    6. └── templates
    7. ├── facts.txt.erb
    8. └── motd.erb
    9. 3 directories, 3 files

    2.3 实例化虚拟资源

    2.3.1 在puppet模块中实例化

    1. [root@linuxmaster1poc testing]# vim environment/modules/puppet/manifests/config.pp
    2. class puppet::config{
    3. include puppet::params
    4. include puppet::puppet_config,puppet::namespaceauth_config,puppet::auth_config,puppet::user,puppet::group
    5. include alluser,allgroup #必须将节点作用域中的类包含进来
    6. }
    7. class puppet::puppet_config{
    8. file { '/etc/puppet/puppet.conf':
    9. ensure => present,
    10. content => template('puppet/puppet.conf.erb'),
    11. owner => 'puppet',
    12. group => 'puppet',
    13. mode => '0644',
    14. backup => main,
    15. require => Class['puppet::install','puppet::user','puppet::group'],
    16. notify => Class['puppet::service'],
    17. }
    18. }
    19. class puppet::auth_config{
    20. file { '/etc/puppet/auth.conf':
    21. ensure => present,
    22. content => template('puppet/auth.conf.erb'),
    23. owner => 'puppet',
    24. group => 'puppet',
    25. mode => '0644',
    26. backup => main,
    27. require => Class['puppet::install','puppet::user','puppet::group'],
    28. notify => Class['puppet::service'],
    29. }
    30. }
    31. class puppet::namespaceauth_config{
    32. file { '/etc/puppet/namespaceauth.conf':
    33. ensure => present,
    34. content => template('puppet/namespaceauth.conf.erb'),
    35. owner => 'puppet',
    36. group => 'puppet',
    37. mode => '0644',
    38. backup => main,
    39. require => Class['puppet::install','puppet::user','puppet::group'],
    40. notify => Class['puppet::service'],
    41. }
    42. }
    43. class puppet::user{ #使用飞船语法实化用户puppet资源
    44. # realize User['puppet']
    45. User <| title == 'puppet' |>
    46. }
    47. class puppet::group{ #使用realize函数实化组puppet资源
    48. realize Group['puppet']
    49. # Group <| title == 'puppet' |>
    50. }

    2.3.2 在puppet_linux57poc模块中实例化

    1. [root@linuxmaster1poc testing]# cat agents/modules/puppet_linux57poc/manifests/init.pp
    2. class puppet_linux57poc{
    3. include puppet_linux57poc::motd_install,puppet_linux57poc::motd_config,puppet_linux57poc::facts,puppet_linux57poc::user,puppet_linux57poc::group
    4. include alluser,allgroup #必须将节点作用域中的类包含进来
    5. }
    6. class puppet_linux57poc::motd_install{
    7. package{ setup:
    8. ensure => present,
    9. }
    10. }
    11. class puppet_linux57poc::motd_config{
    12. file{ "/etc/motd":
    13. owner => "xiaonuo",
    14. group => "root",
    15. mode => 0440,
    16. content => template("puppet_linux57poc/motd.erb"),
    17. backup => 'main',
    18. require => Class['puppet_linux57poc::motd_install','puppet_linux57poc::user','puppet_linux57poc::group']
    19. }
    20. }
    21. class puppet_linux57poc::facts{
    22. file{ "/etc/mcollective/facts.txt":
    23. owner => "root",
    24. group => "root",
    25. mode => 0400,
    26. content => template("puppet_linux57poc/facts.txt.erb"),
    27. backup => 'main',
    28. require => Class['puppet_linux57poc::motd_install','puppet_linux57poc::user','puppet_linux57poc::group']
    29. }
    30. }
    31. class puppet_linux57poc::user{ #使用realize函数实化用户xiaonuo和root资源
    32. realize( User['xiaonuo'],
    33. User['root'] )
    34. }
    35. class puppet_linux57poc::group{ #使用realize函数实化组xiaonuo和root资源
    36. realize( Group['xiaonuo'],
    37. Group['root'] )
    38. }

    3. 测试

    3.1 测试puppet模块(略)

    3.2 测试puppet_linux57poc模块

    3.2.1 查看当前系统是否有xiaonuo用户和组

    1. [root@linux57poc puppet]# id xiaonuo
    2. id: xiaonuo: No such user
    3. [root@linux57poc puppet]# cat /etc/group | grep xiaonuo
    4. [root@linux57poc puppet]#
    5. [root@linux57poc puppet]# ll /etc/motd
    6. -rwxrwxrwx 1 puppet puppet 313 Jan 2 06:17 /etc/motd

    3.2.2 同步puppetmaster

    1. [root@linux57poc puppet]# puppet agent -t --environment=testing
    2. info: Retrieving plugin
    3. info: Loading facts in /var/lib/puppet/lib/facter/fact_apply.rb
    4. info: Caching catalog for puppet_linux57poc.dev.shanghaigm.com
    5. info: Applying configuration version '1389555288'
    6. notice: /Stage[main]/Allservice::Lm_sensors_service/Service[lm_sensors]/ensure: ensure changed 'running' to 'stopped'
    7. notice: /Group[xiaonuo]/ensure: created
    8. notice: /Stage[main]/Alluser::Xiaonuo/User[xiaonuo]/ensure: created
    9. ...
    10. info: FileBucket adding {md5}b2090646c444c5ddf1533749743ebd71
    11. info: /Stage[main]/Mcollective::Facter/File[/etc/mcollective/facts.yaml]: Filebucketed /etc/mcollective/facts.yaml to main with sum b2090646c444c5ddf1533749743ebd71
    12. notice: /Stage[main]/Sysctl::Exec/Exec[sysctl -p >/dev/null &]/returns: executed successfully
    13. notice: /Stage[main]/Puppet_linux57poc::Motd_config/File[/etc/motd]/owner: owner changed 'puppet' to 'xiaonuo'
    14. notice: /Stage[main]/Puppet_linux57poc::Motd_config/File[/etc/motd]/group: group changed 'puppet' to 'root'
    15. notice: /Stage[main]/Puppet_linux57poc::Motd_config/File[/etc/motd]/mode: mode changed '0777' to '0440'
    16. notice: /Stage[main]/Allservice::Bluetooth_service/Service[bluetooth]/ensure: ensure changed 'running' to 'stopped'
    17. notice: Finished catalog run in 4.54 seconds

    3.2.3 验证结果是否正确

    1. [root@linux57poc puppet]# id xiaonuo
    2. uid=600(xiaonuo) gid=600(xiaonuo) groups=600(xiaonuo)
    3. [root@linux57poc puppet]# cat /etc/group | grep xiaonuo
    4. xiaonuo:x:600:
    5. [root@linux57poc puppet]# ll /etc/motd
    6. -r--r----- 1 xiaonuo root 313 Jan 2 06:17 /etc/motd
    7. [root@linux57poc puppet]#