Ok, you can write “if” ; “elsif” or “case” sequences in your Puppet manifests. But, in some cases, you can write selectors to enhance your code.

Take this short example :

[root@puppetserver examples]# cat selector_example.pp
# class definition :
class selector_example {
  file {'/etc/motd' :
    ensure  => file,
    owner   => 'root',
    group   => 'root',
    mode    => 'u=rw,go=r',
    content => $::os['name'] ? {
      'OracleLinux' => "Your are running Oracle Linux version ${::os['release']['full']}\n",
      'CentOS'      => "Welcome to CentOS, a RedHat OpenSource alternative!",
      'fedora'      => "Welcome to fedora!\n",
      default       => "Unknown operating system\n",
    },
  }
}

# class execution :
class { '::selector_example': }

I simply wrote this manisfest where it writes “/etc/motd” file content according the operating system name.

Let execute this manifest :

[root@puppetserver examples]# puppet apply selector_example.pp
Notice: Compiled catalog for puppetserver.argonay.wou in environment production in 0.12 seconds
Notice: /Stage[main]/Selector_example/File[/etc/motd]/ensure: defined content as '{md5}48a09e8abe230115a55907fb26366c4e'
Notice: Applied catalog in 0.14 seconds

Take a look to “/etc/motd” file :

[root@puppetserver examples]# cat /etc/motd
Your are running Oracle Linux version 7.2

My virtual machine is running Oracle Linux :

[root@puppetserver examples]# rpm -q oraclelinux-release
oraclelinux-release-7.2-1.0.5.el7.x86_64

 

www.pdf24.org    Send article as PDF   

Leave a Reply

Your email address will not be published. Required fields are marked *


*