Puppet擴展篇2-如何使用虛擬資源解決puppet衝突問題

零基礎學習Puppet自動化配置管理系列文檔node

虛擬資源是一種用來管理多種配置共同依賴同一資源的方法。若是多個類依賴同一個資源時則可避免寫多個資源,也能夠解決資源重定義的錯誤。 虛擬資源常常用於用戶管理中,虛擬資源只會被聲明一次,但能夠運用一次或屢次。linux

要使用虛擬資源是須要在資源聲明開頭加上字符「@」來使資源虛擬化。而後再使用下面兩種方法之一來實例化虛擬資源:shell

  • "飛船"語法<||>vim

  • realize函數bash

1. 定義兩個用戶,puppet和root,並將其虛擬化

注意定義虛擬資源必須在全局做用域或者節點做用域中定義,簡單的理解,如下目錄中site.pp就是全局做用域,包含nodes目錄(site.pp中import了nodes目錄),在節點node下定義的虛擬資源屬於節點做用域,其餘模塊中的都屬於局部做用域。微信

1.1 在全局做用域中建立對應的pp文件

[root@linuxmaster1poc testing]# tree manifests/
manifests/
├── nodes
│   ├── puppetclient.pp
│   ├── virtual_group.pp
│   └── virtual_user.pp
└── site.pp
1 directory, 4 files

1.2 建立虛擬用戶puppet、root、xiaonuo

[root@linuxmaster1poc testing]# vim manifests/nodes/virtual_user.pp 
class alluser{
  include alluser::puppet,alluser::root
}
class alluser::puppet{
  @user { 'puppet':
    ensure => present,
    uid    => '52',
    gid    => '52',
    home   => '/var/lib/puppet',
    shell  => '/sbin/nologin',
  }
}
class alluser::root{
  @user { 'root':
    ensure => present,
    uid    => '0',
    gid    => '0',
    home   => '/root',
    shell  => '/bin/bash',
  }
}

class alluser::xiaonuo{
  @user { 'xiaonuo':
    ensure => present,
    uid    => '600',
    gid    => '600',
    home   => '/home/xiaonuo',
    shell  => '/sbin/nologin',
  }
}

1.3 建立虛擬組puppet、root和xiaonuo

[root@linuxmaster1poc testing]# vim manifests/nodes/virtual_group.pp 
class allgroup{
  include allgroup::puppet,allgroup::root
}
class allgroup::puppet{
  @group { 'puppet':
    ensure    => present,
    name      => 'puppet',
    gid       => '52',
    allowdupe => false,
    members   => 'puppet',
  }
}
class allgroup::root{
  @group { 'root':
    ensure    => present,
    name      => 'root',
    gid       => '0',
    allowdupe => false,
    members   => 'root',
  }
}
class allgroup::xiaonuo{
  @group { 'xiaonuo':
    ensure    => present,
    name      => 'xiaonuo',
    gid       => '600',
    allowdupe => false,
    members   => 'xiaonuo',
  }
}

2. 編寫puppet模塊,將虛擬資源用戶puppet和組puppet實化

2.1 編寫pupppet模塊

[root@linuxmaster1poc testing]# tree environment/modules/puppet
environment/modules/puppet
├── files
├── manifests
│   ├── config.pp
│   ├── init.pp
│   ├── install.pp
│   ├── params.pp
│   └── service.pp
├── README
└── templates
    ├── auth.conf.erb
    ├── namespaceauth.conf.erb
    └── puppet.conf.erb
3 directories, 9 files

2.2 編寫puppet_linux57poc模塊

[root@linuxmaster1poc testing]# tree agents/modules/puppet_linux57poc/
agents/modules/puppet_linux57poc/
├── files
├── manifests
│   └── init.pp
└── templates
    ├── facts.txt.erb
    └── motd.erb
3 directories, 3 files

2.3 實例化虛擬資源

2.3.1 在puppet模塊中實例化app

[root@linuxmaster1poc testing]# vim environment/modules/puppet/manifests/config.pp 
class puppet::config{
  include puppet::params
  include puppet::puppet_config,puppet::namespaceauth_config,puppet::auth_config,puppet::user,puppet::group
  include alluser,allgroup #必須將節點做用域中的類包含進來
}
class puppet::puppet_config{
  file { '/etc/puppet/puppet.conf':
    ensure  => present,
    content => template('puppet/puppet.conf.erb'),
    owner   => 'puppet',
    group   => 'puppet',
    mode    => '0644',
    backup  => main,
    require => Class['puppet::install','puppet::user','puppet::group'],
    notify  => Class['puppet::service'],
  }
}
class puppet::auth_config{
  file { '/etc/puppet/auth.conf':
    ensure  => present,
    content => template('puppet/auth.conf.erb'),
    owner   => 'puppet',
    group   => 'puppet',
    mode    => '0644',
    backup  => main,
    require => Class['puppet::install','puppet::user','puppet::group'],
    notify  => Class['puppet::service'],
  }
}
class puppet::namespaceauth_config{
  file { '/etc/puppet/namespaceauth.conf':
    ensure  => present,
    content => template('puppet/namespaceauth.conf.erb'),
    owner   => 'puppet',
    group   => 'puppet',
    mode    => '0644',
    backup  => main,
    require => Class['puppet::install','puppet::user','puppet::group'],
    notify  => Class['puppet::service'],
  }
}
class puppet::user{ #使用飛船語法實化用戶puppet資源
#  realize User['puppet'] 
  User <| title == 'puppet' |>
}
class puppet::group{ #使用realize函數實化組puppet資源
  realize Group['puppet']
#  Group <| title == 'puppet' |>
}

2.3.2 在puppet_linux57poc模塊中實例化ide

[root@linuxmaster1poc testing]# cat agents/modules/puppet_linux57poc/manifests/init.pp 
class puppet_linux57poc{
  include puppet_linux57poc::motd_install,puppet_linux57poc::motd_config,puppet_linux57poc::facts,puppet_linux57poc::user,puppet_linux57poc::group
  include alluser,allgroup #必須將節點做用域中的類包含進來
}
class puppet_linux57poc::motd_install{
  package{ setup:
    ensure => present,
  }
}
class puppet_linux57poc::motd_config{
  file{ "/etc/motd":
    owner   => "xiaonuo",
    group   => "root",
    mode    => 0440,
    content => template("puppet_linux57poc/motd.erb"),
    backup  => 'main',
    require => Class['puppet_linux57poc::motd_install','puppet_linux57poc::user','puppet_linux57poc::group']
  }
}
class puppet_linux57poc::facts{
  file{ "/etc/mcollective/facts.txt":
    owner   => "root",
    group   => "root",
    mode    => 0400,
    content => template("puppet_linux57poc/facts.txt.erb"),
    backup  => 'main',
    require => Class['puppet_linux57poc::motd_install','puppet_linux57poc::user','puppet_linux57poc::group']
  }
}
class puppet_linux57poc::user{  #使用realize函數實化用戶xiaonuo和root資源
  realize( User['xiaonuo'], 
           User['root'] )
}
class puppet_linux57poc::group{ #使用realize函數實化組xiaonuo和root資源
  realize( Group['xiaonuo'],
           Group['root'] )
}

3. 測試

3.1 測試puppet模塊(略)

3.2 測試puppet_linux57poc模塊

3.2.1 查看當前系統是否有xiaonuo用戶和組函數

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

3.2.2 同步puppetmaster學習

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

3.2.3 驗證結果是否正確

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

返回主目錄

交流方式:

微信公衆號:puppet2014,可微信搜索加入,也能夠掃描如下二維碼進行加入

微信公衆號微信公衆號

QQ交流羣:296934942

相關文章
相關標籤/搜索