Avainsanaan ‘templates’ liitetyt artikkelit

Linux keskitty hallinta kurssilla 9.11.2012 tehty template tiedostoa käyttävä Puppet moduli

16.11.2012

Moduli käyttää template tiedostoa, joka käytetään pohjana /tmp/templatetest.txt tiedostolle.

Modulin rakenne:

xubuntu@xubuntu:~/puppet/modules$ tree
.
└── templatetest
    ├── manifests
    │   └── init.pp
    └── templates
        └── templatetest.txt.erb

Modulin init.pp tiedoston sisältö:

class templatetest {
   $banner= "---------------------"

  file { '/tmp/templatetest.txt':
    ensure => file,
    content => template('templatetest/templatetest.txt.erb'), 
  }
}

Tiedoston templatetest.txt.erb sisältö:

Testing Puppet templates
<%=banner %>
System uptime is <%= @uptime%>
<%=DateTime.now.strftime('%D %R') %>

Modulin suorittaminen:

xubuntu@xubuntu:~/puppet$ sudo puppet apply --modulepath modules/ -e 'include templatetest'

Modulin testaaminen:

xubuntu@xubuntu:~/puppet$ sudo puppet apply --modulepath modules/ -e 'include templatetest'
notice: /Stage[main]/Templatetest/File[/tmp/templatetest.txt]/ensure: defined content as '{md5}86f01e6e2ab88b5e6fb152eb0816bc3f'
notice: Finished catalog run in 0.02 seconds

Tiedoston /tmp/template.txt sisältö:

Testing Puppet templates
---------------------
System uptime is 1:19 hours
11/16/12 09:31
About

Tätä dokumenttia saa kopioida ja muokata GNU General Public License (versio 2 tai uudempi) mukaisesti.
http://www.gnu.org/licenses/gpl.html
Pohjana Tero Karvisen Linux-kurssi, www.iki.fi/karvinen

Läksy w45: Muotit, parametrisoidut luokat ja määritellyt tyypit

15.11.2012

Harjoitustehtävän aiheena oli tutustua Puppet palvelinautomaatio järjestelmän Learning Pupppet dokumentaation lukuihin Templates, Parameterized Classes (Modules, Part Two) ja Defined Types sekä awaseroot blogiin. Käytännön harjoituksena tehtiin lyhyitä Puppet ohjelmia edellä mainitun dokumentaation teemoista.

Harjoitusympäristö

Harjoitusta suoritettiin 15.11. opiskelijan kotona käyttäen henkilökohtaista tietokonetta. Internet-yhteytenä oli Elisa Oyj:n tarjoama VDSL tyyppinen 100/10 mbit kiinteä laajakaistayhteys. Harjoituksessa käytettiin Lenovo R60 kannettavaa tietokonetta. Käyttöjärjestelmä ladattiin Xubuntu versio 12.04 32-bittinen Linux live-cd:ltä. Tietokoneen kiintolevylle asennettua käyttöjärjestelmää ei käytetty harjoitustehtävän suorittamiseen.

Lenovo R60 kokoonpano:

  • Suoritin: Intel Core 2 Duo T56000 @1.83 Ghz
  • Keskusmuisti: 4 Gt DDR2
  • Kiintolevy: 100 Gt SATA150 54000rpm
  • Käyttöjärjestelmä: Xubuntu versio 12.04 32-bittinen

Puppet palvelinautomaatio järjestelmän asentaminen

Aloitin harjoitustehtävän tekemisen päivittämällä Ubuntun pakettivarastot komennolla:

$sudo apt-get update

Pakettivarastojen päivitys kesti muutamia sekunteja.

Asensin Puppet järjestelmän komennolla:

$sudo apt-get install puppet

Testasin asennuksen ajamalla komennon puppet komentoriviltä:

$puppet

Komento antoi tulosteen:

See 'puppet help' for help on available puppet subcommands

Totesin puppet asennuksen onnistuneen.

Muottien (templates) käyttäminen Puppet modulissa

Aluksin tein hakemiston puppet ja sen alihakemiston modules:

xubuntu@xubuntu:~$ cd
xubuntu@xubuntu:~$ mkdir puppet
xubuntu@xubuntu:~$ cd puppet/
xubuntu@xubuntu:~/puppet$ mkdir modules
xubuntu@xubuntu:~/puppet$

Kokeillakseni templates ominaisuuden käyttöä Puppetissa tein modulin, joka kirjoittaa tekstitiedostoon tietoja järjestelmästä käyttäen template pohjaa ja Facterin tietoja. Tein modules hakemistoon alihakemiston sysinfo Puppet modulia varten.

Modulin rakenne:

xubuntu@xubuntu:~/puppet/modules/sysinfo$ tree
.
├── manifests
│   └── init.pp
└── templates
    └── sysinfo.txt.erb

2 directories, 2 files

Modulin init.pp tiedoston sisältö:

class sysinfo {
  $banner= "\nSystem information\n---------------------\n"

  file { '/tmp/sysinfo.txt':
    ensure => file,
    content => template('sysinfo/sysinfo.txt.erb'), 
  }
}

Tiedoston sysinfo.txt.erb sisältö:

<%=banner %>
Current time is <%=DateTime.now.strftime('%D %R') %>
System uptime is <%= @uptime%>

System architecture: <%= @architecture%>
Computer manufacurer: <%= @manufacturer%>
Computer model: <%= @productname%>
Computer serialnumber: <%= @serialnumber%>

Total memory: <%= @memorytotal%>

Modulin suorittaminen:

xubuntu@xubuntu:~/puppet$ sudo puppet apply --modulepath modules/ -e 'include sysinfo'

Modulin testaaminen:

xubuntu@xubuntu:~/puppet$ sudo puppet apply --modulepath modules/ -e 'include sysinfo'
warning: Could not retrieve fact fqdn
notice: /Stage[main]/Sysinfo/File[/tmp/sysinfo.txt]/ensure: defined content as '{md5}97d638409b07e5c232298ec3b025efac'
notice: Finished catalog run in 0.06 seconds

Modulin tekemän raporttitiedoston /tmp/sysinfo.txt sisältö:

xubuntu@xubuntu:~/puppet$ cat /tmp/sysinfo.txt 

System information
---------------------

Current time is 11/15/12 18:52
System uptime is 1:39 hours

System architecture: i386
Computer manufacurer: LENOVO
Computer model: 9461DXG
Computer serialnumber: L3BC786

Total memory: 2.95 GB

Parametrisoitujen luokkien käyttäminen Puppet modulissa

Kokeillakseni parametrisoitujen luokkien käyttöä Puppetissa, tein SSH palvelimen asentavan modulin, jolle voidaan antaa parametriksi SSH porttinumero sekä SSH paketin sekä palvelun tila. Oletusarvoisesti SSH portti on 22, SSH paketti on asennettuna ja SSH taustaprosessi on käynnissä.Tein modules hakemistoon alihakemiston sshport Puppet modulia varten.

Modulin rakenne:

xubuntu@xubuntu:~/puppet/modules/sshport$ tree
.
└── manifests
    └── init.pp

1 directory, 1 file

Modulin init.pp tiedoston sisältö:

class sshport ($port='22', $enable = true, $ensure = running) {

  package { 'openssh-server':
    ensure => present,
     allowcdrom => true,
  }

  file { '/etc/ssh/sshd_config':
    ensure => file,
    require => Package['openssh-server'],
  }

  augeas{"set_port":
    context => "/files/etc/ssh/sshd_config",
    changes => "set Port $port",
    require => File['etc/ssh/sshd_config'],
   }

  service { 'ssh':
    ensure    => $running,
    enable    => $enable,
    hasstatus => true,
    subscribe => [Package['openssh-server'], File['/etc/ssh/sshd_config']],
    require   => File['/etc/ssh/sshd_config'],
  }
}

Modulin suorittaminen:

xubuntu@xubuntu:~/puppet$ sudo puppet apply --modulepath modules/ -e 'include sshport'

Modulin testaaminen:

xubuntu@xubuntu:~/puppet$ sudo puppet apply --modulepath modules/ -e 'include sshport'
warning: Could not retrieve fact fqdn
notice: /Stage[main]/Sshport/Package[openssh-server]/ensure: ensure changed 'purged' to 'present'
notice: /Stage[main]/Sshport/Service[ssh]/enable: enable changed 'false' to 'true'
notice: /Stage[main]/Sshport/Service[ssh]: Triggered 'refresh' from 1 events
notice: Finished catalog run in 21.38 seconds

Oletuksena avattu ssh portti netstat listauksessa:

xubuntu@xubuntu:~/puppet/modules/sshport$ netstat -l -t -n
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State      
tcp        0      0 127.0.0.1:53            0.0.0.0:*               LISTEN     
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:631           0.0.0.0:*               LISTEN     
tcp        0      0 127.0.0.1:34264         0.0.0.0:*               LISTEN     
tcp6       0      0 :::22                   :::*                    LISTEN     
tcp6       0      0 ::1:631                 :::*                    LISTEN 

SSH yhteyden kokeileminen:

xubuntu@xubuntu:~/puppet/modules/sshport$ ssh xubuntu@localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is dc:25:50:0c:e1:01:0e:01:d6:2d:e2:d6:68:fc:c9:79.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (ECDSA) to the list of known hosts.
xubuntu@localhost's password: 
Permission denied, please try again.
xubuntu@localhost's password:

Määriteltyjen tyyppien käyttäminen Puppet modulissa

Kokeillakseni määriteltyjen tyypppien käyttöä Puppetissa, tein uuden version aikaisemman harjoitustehtävän uusien  käyttäjätunnuksen luomiseen tarkoitetusta Puppet modulista. Uusi versio käyttää määriteltyä tyypppiä human::account, jolla voidaan luoda uusi käyttäjätunnus ja parametriksi voidaan antaa haluttu komentotulkki ja selväkielinen salasana. Salasanasta luodaan omatekoisella funktiolla hashpw Linuxin /etc/shadow tiedoston formaattiin sopiva SHA-512 tiiviste. Tein modules hakemistoon alihakemiston human Puppet modulia varten.

Modulin rakenne:

xubuntu@xubuntu:~/puppet/modules/human$ tree
.
├── lib
│   └── puppet
│       └── parser
│           └── functions
│               └── hashpw.rb
└── manifests
    ├── account.pp
    └── init.pp

5 directories, 3 files

Modulin hashpw.rb tiedoston sisältö:

module Puppet::Parser::Functions
  newfunction(:hashpw, :type => :rvalue) do |args|
    passwd = args[0]
    hashtype = '$6$'
    salt = rand(36**8).to_s(36)
    hash = passwd.crypt(hashtype + salt)
  end 
end

Modulin init.pp tiedoston sisältö:

class human  {

  human::account { 'humanuser1':
     shell => '/bin/zsh',
  } 

}

Modulin account.pp tiedoston sisältö:

define human::account($shell = '/bin/bash',$password='pA$$w0rd') {
    $username = $title
    $shadowpw = hashpw($password)
    user { $username:
      ensure     => present,
      gid        => $username,
      shell      => $shell,
      password   => $shadowpw,
      home       => "/home/${username}",
      managehome => true,
      require    => Group["$username"],
    }

    group { "$username":
        ensure    => present,
        allowdupe => false,
    }

    file { "/home/${username}":
      ensure  => directory,
      owner   => $username,
      group   => $username,
      recurse => true,
      replace => true,
      force   => true,
      require   => User["${username}"],
    }

    file { "/home/${username}/.bash_history":
        mode => 600,
        owner   => $username,
        group   => $username,
        require => File["/home/${username}"],
    }

    file { "/home/${username}/.ssh":
        ensure  => directory,
        owner   => $username,
        group   => $username,
        mode    => 700,
        require => File["/home/${username}"],
    }

}

Modulin suorittaminen:

xubuntu@xubuntu:~/puppet$ sudo puppet apply --modulepath modules/ -e 'include human'

Modulin testaaminen:

xubuntu@xubuntu:~/puppet$ sudo puppet apply --modulepath modules/ -e 'include human'
warning: Could not retrieve fact fqdn
notice: /Stage[main]/Human/Human::Account[humanuser1]/Group[humanuser1]/ensure: created
notice: /Stage[main]/Human/Human::Account[humanuser1]/User[humanuser1]/ensure: created
notice: /Stage[main]/Human/Human::Account[humanuser1]/File[/home/humanuser1/.ssh]/ensure: created
notice: Finished catalog run in 0.29 seconds

Testikäyttäjän “humanuser1″ salasanatiiviste /etc/shadow tiedostossa:

humanuser1:$6$vicoxo86$iLh8A21dSjSRZanv9qRo.jlNp5bSK9KiMmERkMtQLoSE/fiHghD04BxevD.46pqvGRCTYdu8EDk5xVAb/ue1n0:15659:0:99999:7:::

Testikäyttäjän “humanuser1″ tiedot /etc/passwd tiedostossa:

humanuser1:x:1000:1000::/home/humanuser1:/bin/zsh

SSH yhteyden kokeileminen:

xubuntu@xubuntu:~/puppet$ ssh humanuser1@localhost
humanuser1@localhost's password: 
Welcome to Ubuntu 12.04.1 LTS (GNU/Linux 3.2.0-29-generic i686)

 * Documentation:  https://help.ubuntu.com/

The programs included with the Ubuntu system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Ubuntu comes with ABSOLUTELY NO WARRANTY, to the extent permitted by
applicable law.

humanuser1@xubuntu ~ % 

Mount resurssin käyttäminen Puppet modulissa

Kokeillakseni awaseroot blogissa esiteltyä Puppetin mount resurssia, tein määriteltyä tyyppiä mount resurssin yhteydessä käyttävän Puppet modulin. Tein modules hakemistoon alihakemiston mountdev Puppet modulia varten.

Modulin rakenne:

xubuntu@xubuntu:~/puppet/modules/mountdev$ tree
.
└── manifests
    ├── init.pp
    └── mounter.pp

1 directory, 2 files

Modulin init.pp tiedoston sisältö:

class mountdev  {

  mountdev::mounter { '/dev/sr0':
     mountpoint => '/cdrom'',
     fstype => 'iso9660',
     ensure => 'unmounted',
  } 

}

Modulin mounter.pp tiedoston sisältö:

define mountdev::mounter($mountpoint,$ensure,$fstype,$options="defaults") {
    mount { $mountpoint :
        device  => $title,
        fstype  => $fstype,
    ensure  => $ensure,
    options => $options,
    }
}

Modulin suorittaminen:

xubuntu@xubuntu:~/puppet$ sudo puppet apply --modulepath modules/ -e 'include mountdev'

Modulin testaaminen:

xubuntu@xubuntu:~/puppet$ sudo puppet apply --modulepath modules/ -e 'include mountdev'
warning: Could not retrieve fact fqdn
err: /Stage[main]/Mountdev/Mountdev::Mounter[/dev/sr0]/Mount[/cdrom]/ensure: change from ghost to unmounted failed: Execution of '/bin/umount /cdrom' returned 1: umount: /cdrom: device is busy.
        (In some cases useful info about processes that use
         the device is found by lsof(8) or fuser(1))

notice: Finished catalog run in 0.29 seconds

Xubuntun live-cd:n unmounttaaminen ei onnistunut, koska live-cd on käytössä.

Lähteet

Karvinen, Tero 2012. Linux keskitetty hallinta ICT4TN011-2 kurssin kotisivu.
http://terokarvinen.com/2012/aikataulu-linuxin-keskitetty-hallinta-ict4tn011-2-puppet

Ortega, Felipe 2012. [Puppet Users] rand losing its randomness after using fqdn_rand.
https://groups.google.com/forum/#!msg/puppet-users/kc_KJwLwHvo/FjokplF1IuoJ

Pereira, Jude 2012. Use Ruby to Generate your Shadow Password.
http://judepereira.com/blog/use-ruby-to-generate-your-shadow-password/

Puppet Labs 2012. Docs: Type Reference.
http://docs.puppetlabs.com/references/latest/type.html

Puppet Labs 2011. Docs: Learning — Defined Types.
http://docs.puppetlabs.com/learning/definedtypes.html

Puppet Labs 2011. Docs: Learning — Parameterized Classes (Modules, Part Two).
http://docs.puppetlabs.com/learning/modules2.html

Puppet Labs 2011. Docs: Learning — Templates.
http://docs.puppetlabs.com/learning/templates.html

Sobral, Daniel 2010. Module dcsobral/users. Puppet Forge.
http://forge.puppetlabs.com/dcsobral/users

Movsesjans, Armens 2012. Puppet module for /etc/fstab mounts.
https://awaseroot.wordpress.com/2012/11/03/puppet-module-for-etcfstab-mounts/

About

Tätä dokumenttia saa kopioida ja muokata GNU General Public License (versio 2 tai uudempi) mukaisesti.
http://www.gnu.org/licenses/gpl.html
Pohjana Tero Karvisen Linux-kurssi, www.iki.fi/karvinen

Linux keskitetty hallinta kurssilla 9.11.2012 tehty SSH palvelimen asentava Puppet moduli

9.11.2012

Puppet moduli, joka asentaa SSH palvelimen. Moduli asettaa myös SSH palvelimen asetustiedoston, jossa SSH palvelun porttinumero on vaihdettu 2222:n.

Modulin perusversio

Modulin rakenne:

xubuntu@xubuntu:~/puppet/modules$ tree
.
└── sshport
    ├── files
    │   └── sshd_config
    └── manifests
        └── init.pp

3 directories, 2 files

Modulin init.pp tiedoston sisältö:

class sshport {

   package { 'openssh-server':
     ensure => present,
     allowcdrom => true,
   }

   file { '/etc/ssh/sshd_config':
     ensure => file,
     source =>"puppet:///modules/sshport/sshd_config",
     require => Package['openssh-server'],
   }

   service { 'ssh':
     ensure => running,
     enable => true,
     hasstatus => true,
     subscribe => [Package['openssh-server'], File['/etc/ssh/sshd_config']],
     require => File['/etc/ssh/sshd_config'],
   }
}

Modulin suorittaminen:
xubuntu@xubuntu:~/puppet$ sudo puppet apply --modulepath modules/ -e 'include sshport'

Modulin testaaminen:

xubuntu@xubuntu:~/puppet/modules/sshport$ ssh -p 2222 xubuntu@localhost xubuntu@localhost's password: 
Permission denied, please try again.

Modulin toteutus template tiedostoa käyttämällä

Vaihtoehtoinen toteutus modulista, jossa SSH palvelimen asetustiedosto luodaan käyttämällä template tiedostoa. Template tiedostossa porttimäärityksen sisältävä riville tehdään interpolaatio modulissa käytetystä muuttujasta. SSH portin numero määritetään modulissa muuttujalla $port.

Modulin rakenne:

xubuntu@xubuntu:~/puppet/modules$ tree
.
└── sshport
    ├── files
    │   └── sshd_config
    ├── manifests
    │   └── init.pp
    └── templates
        └── sshd_config.erb

Modulin init.pp tiedoston sisältö:

class sshport {

  $port='2562'

  package { 'openssh-server':
     ensure => present,
     allowcdrom => true,
  }

  file { '/etc/ssh/sshd_config':
    ensure => file,
    content => template('sshport/sshd_config.erb'),
    require => Package['openssh-server'],
  }

  service { 'ssh':
    ensure    => running,
    enable    => true,
    hasstatus => true,
    subscribe => [Package['openssh-server'], File['/etc/ssh/sshd_config']],
    require   => File['/etc/ssh/sshd_config'],
  }
}

Modulin sshd_config.erb tiedosto on samanlainen kuin normaali SSH palvelimen asetustiedosto. Poikkeuksena porttimäärityksen sisältä rivi, jossa on seuraavanlainen muuttuja interpolaatio:

Port <%= port %>

Modulin suorittaminen:

xubuntu@xubuntu:~/puppet$ sudo puppet apply --modulepath modules/ -e 'include sshport'

Modulin testaaminen:

xubuntu@xubuntu:~/puppet$ ssh -p 2562 xubuntu@localhost 
xubuntu@localhost's password: 
Permission denied, please try again.

Lähteet

Vogeraal, Bram 2012. Module attachmentgenie/ssh. Puppet Forge.
http://forge.puppetlabs.com/attachmentgenie/ssh

About

Tätä dokumenttia saa kopioida ja muokata GNU General Public License (versio 2 tai uudempi) mukaisesti.
http://www.gnu.org/licenses/gpl.html
Pohjana Tero Karvisen Linux-kurssi, www.iki.fi/karvinen


Seuraa

Get every new post delivered to your Inbox.