Thursday, 3 November 2016

Run puppet manifests and modules in different environments - 3/4

In our previous posts, we have seen on how to install and configure puppetmaster/puppetclients we shall now see on how to create the manifests on the different environments. Additionally, I have added the puppet configuration folder to be controlled using version controller 'git', where I shall explain in my next final post.

Puppet version 3.8.7, by default has no environmentpath enabled and it needs to be mentioned in config files.

Place these 3 lines below [main] section of /etc/puppet/puppet.conf, save and close.
Defined the directory and have asked puppet to first check environment path while on its execution.
    confdir = /etc/puppet
    environmentpath = $confdir/environments
    basemodulepath  = $confdir/modules:/usr/share/puppet/modules
[root@puppetmaster puppet]#service puppetmaster restart

Production Environment:

[root@puppetmaster puppet]# mkdir -p environments/{production,testing}

[root@puppetmaster ~]# cd /etc/puppet/environments/production
[root@puppetmaster production]# mkdir manifests modules

Define your environment.conf in the each of the folder.

[root@puppetmaster production]# cat environments.conf
modulepath = $confdir/environments/production/modules:$condfir/modules:/usr/share/puppet/modules
[root@puppetmaster production]#

[root@puppetmaster production]#mkdir -p modules/prodtest/{files,manifests}

Finally, once after you create your directories, the tree structure should be as below :

[root@puppetmaster production]# tree -F .
.
├── environments.conf
├── manifests/
│   └── node.pp
└── modules/
    └── prodtest/
        ├── files/
        │   └── prodtest.conf
        └── manifests/
            └── init.pp

5 directories, 4 files
[root@puppetmaster production]#

Create files accordingly..

[root@puppetmaster production]# cat modules/prodtest/manifests/init.pp
class prodtest {
file {'/tmp/production':
       path => '/tmp/production',
       ensure => present,
       mode => 640,
       source  => 'puppet:///modules/prodtest/prodtest.conf',
       }
}
[root@puppetmaster production]#

[root@puppetmaster production]# cat modules/prodtest/files/prodtest.conf
I am executing from the prodcution environment....
[root@puppetmaster production]#

[root@puppetmaster production]# cat manifests/node.pp
    include prodtest
}
[root@puppetmaster production]#

From the client, execute the below to retrive info..

[root@puppetclient ~]# puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for puppetclient.example.com
Info: Applying configuration version '1478153895'
Notice: /Stage[main]/Prodtest/File[/tmp/production]/ensure: defined content as '{md5}467d4f8c0dff1ec2799ee98637aff019'
Notice: Finished catalog run in 0.11 seconds
[root@puppetclient ~]#

[root@puppetclient ~]# ls -l /tmp/production
-rw-r----- 1 root root 51 Nov  3 06:18 /tmp/production

[root@puppetclient ~]# cat /tmp/production
I am executing from the prodcution environment....
[root@puppetclient ~]#

Now you could write all your classes from the module directory and start executing those manifests. 
punch "puppet manifests examples" in google and you will get lot of pages. I would leave it as an exercise for the reader. 

testing environment:

Just as in the production, I have create a separate environment for 'testing' ,create the directories and files as the same as what you did for production environment.

[root@puppetmaster environments]# tree -F testing/
testing/
├── environments.conf
├── manifests/
│   └── nodes.pp
└── modules/
    └── testing/
        ├── files/
        │   └── testing.conf
        └── manifests/
            └── init.pp

5 directories, 4 files
[root@puppetmaster environments]#

[root@puppetmaster testing]# cat environments.conf
modulepath = $confdir/environments/testing/modules:$condfir/modules:/usr/share/puppet/modules
[root@puppetmaster testing]#

[root@puppetmaster testing]# cat modules/testing/manifests/init.pp
class testing {
file { '/tmp/testing':
  ensure  => present,
  owner   => 'root',
  group   => 'root',
  mode    => '0777',
  source  => 'puppet:///modules/testing/testing.conf',
   }
}
[root@puppetmaster testing]#

[root@puppetmaster testing]# cat modules/testing/files/testing.conf
creating from testing environment....
Creating the test file for Puppet demonstration
[root@puppetmaster testing]#
 [root@puppetmaster testing]# cat manifests/nodes.pp
    include testing
}
[root@puppetmaster testing]#

On the client, execute mentioning the environment which puppet should pick, else by default it will pick from 'production'

[root@puppetclient ~]# puppet agent -t --environment testing
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for puppetclient.example.com
Info: Applying configuration version '1478154384'
Notice: /Stage[main]/Testing/File[/tmp/testing]/ensure: defined content as '{md5}8fffaf0c4ae55d51df4e926284eef521'
Notice: Finished catalog run in 0.10 seconds
[root@puppetclient ~]#

[root@puppetclient ~]#ls -l
-rwxrwxrwx 1 root root 86 Nov  3 06:26 /tmp/testing
[root@puppetclient ~]#

[root@puppetclient ~]# cat /tmp/testing
creating from testing environment....
Creating the test file for Puppet demonstration
[root@puppetclient ~]#


we shall see in our next post, on how to add '/etc/puppet' to version controlling and create branch for testing puppet and later merging so that it can be used for many in the development/testing teams.

Thanks for re-sharing !