Tuesday, September 5, 2017

Puppet: motd deployment on puppet enterprise 2016

motd deployment on puppet enterprise 2016

create class
create module

Create motd class

1. Create an module environment
[root@devpup]# cd /etc/puppetlabs/code/environments/production/modules

2. Create a module
[root@devpup]# mkdir motd; cd motd; pwd
/etc/puppetlabs/code/environments/production/modules/motd
[root@devpup]#

3. Create these main directories
[root@devpup]# ls
[root@devpup]# mkdir {files,manifests,templates,tests}
[root@devpup]# ls
files  manifests  templates  tests
[root@devpup]# cd manifests/
[root@devpup]# ls
[root@devpup]# pwd
/etc/puppetlabs/code/environments/production/modules/motd/manifests

4. Create class motd and declere resources
note: Resource such as package, file, directory
go to docs.puppet.com for detail

=> - hash rocket

a. Create a file init.pp with folliwing contents
inside this class, we have to define resource

[root@devpup]# vi init.pp

class motd {
        file { "/etc/motd":
                ensure => 'file',
                source => "puppet:///modules/motd/motd    # [on puppet master, inside motd dir, named motd dir ]
        }
}

[root@devpup]# pwd
/etc/puppetlabs/code/environments/production/modules/motd/manifests
[root@devpup]# cd ..
[root@devpup]#

b. Go onto to modules directory and go to files and create motd file

[root@devpup]# ls
files  manifests  templates  tests
[root@devpup]# cd files
[root@devpup]# vi motd
[root@devpup]# cat motd
This is a puppet implementation of motd

do not modify this file, change will be
lost once it is run from puppet master.


-TechTeam
[root@devpup]#

5. Now, you completed motd class.
Now, go back to module dir and go to test dir and create init.pp file with following contents
on init.pp file, you just include the class you created.

[root@devpup]# cd ../tests/
# vi init.pp
include motd

save and exit

6. Now, run the command puppet apply --noop # noop will not apply, it mean, do not apply, just test
# puppet apply --noop init.pp

[root@devpup]# puppet apply --noop init.pp
Notice: Compiled catalog for devpup.expanor.local in environment production in 0.15 seconds
Notice: /Stage[main]/Motd/File[/etc/motd]/content: current_value {md5}d41d8cd98f00b204e9800998ecf8427e, should be {md5}2d6311c2aa4f799ee9cb43b2642f3b8d (noop)
Notice: Class[Motd]: Would have triggered 'refresh' from 1 events
Notice: Stage[main]: Would have triggered 'refresh' from 1 events
Notice: Applied catalog in 0.41 seconds

[root@puppetdev ~]# cat /etc/motd
[root@puppetdev ~]# cp -p /etc/motd /etc/motd.origg
[root@puppetdev ~]#

[root@devpup]# ls -l /etc/motd
-rw-r--r--. 1 root root 0 Jun  7  2013 /etc/motd
[root@devpup]# cp -p /etc/motd /etc/motd.origg

7. Once you verify the output, run the command puppet apply followed by the init.pp file.
[root@devpup]# puppet apply init.pp
Notice: Compiled catalog for devpup.expanor.local in environment production in 0.12 seconds
Notice: /Stage[main]/Motd/File[/etc/motd]/content: content changed '{md5}d41d8cd98f00b204e9800998ecf8427e' to '{md5}2d6311c2aa4f799ee9cb43b2642f3b8d'
Notice: Applied catalog in 0.41 seconds
[root@devpup]# cat /etc/motd
This is a puppet implementation of motd

do not modify this file, change will be
lost once it is run from puppet master.


-TechTeam
[root@devpup]#

you successfully tested on puppet server which is also a client. Now, you have to test on node (client).

8. Now, you have to apply this change to client.
What we have to do is the node classification.

Now, go to main manifests directory and edit site.pp file.
[root@devpup]# cd /etc/puppetlabs/code/environments/production/manifests

go all the way down and this is where you define node class.
you can define under new section with particular host [create a new one.] or under default node section which will apply to all.
In this example, we will add entry under default node.

[root@devpup]# vi site.pp
node default {
        class { 'motd': }
        notify {"Just testing here": }
}

9. Once you make change to this file, save it.
Now, go back to agent node and execute the command puppet agent -t command.

[root@puppetdev ~]# puppet agent -t
Warning: Unable to fetch my node definition, but the agent run will continue:
Warning: getaddrinfo: Temporary failure in name resolution
Info: Retrieving pluginfacts
Error: /File[/opt/puppetlabs/puppet/cache/facts.d]: Failed to generate additional resources using 'eval_generate': getaddrinfo: Temporary failure in name resolution
Error: /File[/opt/puppetlabs/puppet/cache/facts.d]: Could not evaluate: Could not retrieve file metadata for puppet:///pluginfacts: getaddrinfo: Temporary failure in name resolution
Info: Retrieving plugin
Error: /File[/opt/puppetlabs/puppet/cache/lib]: Failed to generate additional resources using 'eval_generate': getaddrinfo: Temporary failure in name resolution
Error: /File[/opt/puppetlabs/puppet/cache/lib]: Could not evaluate: Could not retrieve file metadata for puppet:///plugins: getaddrinfo: Temporary failure in name resolution
Info: Loading facts
Error: Could not retrieve catalog from remote server: getaddrinfo: Temporary failure in name resolution
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run
Error: Could not send report: getaddrinfo: Temporary failure in name resolution
[root@puppetdev ~]# ping devpup.expanor.local
PING devpup.expanor.local (192.168.10.16) 56(84) bytes of data.
64 bytes from devpup.expanor.local (192.168.10.16): icmp_seq=1 ttl=64 time=0.138 ms
64 bytes from devpup.expanor.local (192.168.10.16): icmp_seq=2 ttl=64 time=0.145 ms
^C
--- devpup.expanor.local ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 999ms
rtt min/avg/max/mdev = 0.138/0.141/0.145/0.012 ms
[root@puppetdev ~]# hostname
puppetdev.expanor.local
[root@puppetdev ~]# systemctl status firewalld
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2017-09-02 12:08:46 EDT; 3 days ago
 Main PID: 706 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─706 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

Sep 02 12:08:45 puppetdev.expanor.local systemd[1]: Starting firewalld - dynami...
Sep 02 12:08:46 puppetdev.expanor.local systemd[1]: Started firewalld - dynamic...
Hint: Some lines were ellipsized, use -l to show in full.
[root@puppetdev ~]# systemctl stop firewalld
[root@puppetdev ~]#

run the puppet agent -t command again. looks like firewall is blocking it..

[root@puppetdev ~]# puppet agent -t
Warning: Unable to fetch my node definition, but the agent run will continue:
Warning: getaddrinfo: Temporary failure in name resolution
Info: Retrieving pluginfacts
Error: /File[/opt/puppetlabs/puppet/cache/facts.d]: Failed to generate additional
Error: /File[/opt/puppetlabs/puppet/cache/facts.d]: Could not evaluate: Could not me resolution
Info: Retrieving plugin
Error: /File[/opt/puppetlabs/puppet/cache/lib]: Failed to generate additional reso
Error: /File[/opt/puppetlabs/puppet/cache/lib]: Could not evaluate: Could not retrieve file metadata for puppet:///plugins: getaddrinfo: Temporary failure in name resolution
Info: Loading facts
Error: Could not retrieve catalog from remote server: getaddrinfo: Temporary failure in name resolution
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run
Error: Could not send report: getaddrinfo: Temporary failure in name resolution
[root@puppetdev ~]# grep devpup.expanor.local /etc/hosts
#192.168.10.16   devpup.expanor.local devpup puppet
192.168.10.16   devpup.expanor.local devpup
[root@puppetdev ~]# puppet agent -t --verbose --server devpup.expanor.local
Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for puppetdev.expanor.local
Info: Applying configuration version '1504635836'
Notice: /Stage[main]/Motd/File[/etc/motd]/content:
--- /etc/motd   2013-06-07 10:31:32.000000000 -0400
+++ /tmp/puppet-file20170905-24243-r5tj39       2017-09-05 14:21:41.362462828 -0400
@@ -0,0 +1 @@
+my motd

Notice: /Stage[main]/Motd/File[/etc/motd]/content: content changed '{md5}d41d8cd98f00b204e9800998ecf8427e' to '{md5}2d6311c2aa4f799ee9cb43b2642f3b8d'
Notice: Just testing here
Notice: /Stage[main]/Main/Node[default]/Notify[Just testing here]/message: defined 'message' as 'Just testing here'
Notice: Applied catalog in 0.68 seconds
[root@puppetdev ~]#


------------------
run following on puppet server

[root@devpup]# netstat -an | grep 8140
tcp6       0      0 :::8140                 :::*                    LISTEN
tcp6       0      0 192.168.10.16:8140      192.168.10.25:57707     ESTABLISHED
[root@devpup]# man nscd
[root@devpup]# nscd -i files
bash: nscd: command not found...



[root@devpup]# puppet resource service puppetserve ensure=running
Error: Execution of 'journalctl -n 50 --since '5 minutes ago' -u puppetserve --no-pager' ret                                                                            urned 1: -- Logs begin at Sat 2017-09-02 08:18:50 EDT, end at Tue 2017-09-05 15:45:01 EDT. -                                                                            -
Error: /Service[puppetserve]/ensure: change from stopped to running failed: Execution of 'jo                                                                            urnalctl -n 50 --since '5 minutes ago' -u puppetserve --no-pager' returned 1: -- Logs begin                                                                             at Sat 2017-09-02 08:18:50 EDT, end at Tue 2017-09-05 15:45:01 EDT. --
service { 'puppetserve':
  ensure => 'stopped',
}
[root@devpup]# puppet resource service puppetserver enable=true
Error: Could not unmask puppetserver:
Error: /Service[puppetserver]/enable: change from false to true failed: Could not unmask pup                                                                            petserver:
service { 'puppetserver':
  enable => 'false',
}
[root@devpup]#

--------------------


Go to the client node and run it again,

[root@puppetdev Packages]# puppet agent -t
Warning: Unable to fetch my node definition, but the agent run will continue:
Warning: getaddrinfo: Temporary failure in name resolution
Info: Retrieving pluginfacts
Error: /File[/opt/puppetlabs/puppet/cache/facts.d]: Failed to generate additional resources using 'eval_generate': getaddrinfo: Temporary failure in name resolution
Error: /File[/opt/puppetlabs/puppet/cache/facts.d]: Could not evaluate: Could not retrieve file metadata for puppet:///pluginfacts: getaddrinfo: Temporary failure in name resolution
Info: Retrieving plugin
Error: /File[/opt/puppetlabs/puppet/cache/lib]: Failed to generate additional resources using 'eval_generate': getaddrinfo: Temporary failure in name resolution
Error: /File[/opt/puppetlabs/puppet/cache/lib]: Could not evaluate: Could not retrieve file metadata for puppet:///plugins: getaddrinfo: Temporary failure in name resolution
Info: Loading facts
Error: Could not retrieve catalog from remote server: getaddrinfo: Temporary failure in name resolution
Warning: Not using cache on failed catalog
Error: Could not retrieve catalog; skipping run
Error: Could not send report: getaddrinfo: Temporary failure in name resolution

it was not working but after a while, I think the default push time is 30 minutes, the motd file was updated.

[root@puppetdev Packages]# cat /etc/motd
This is a puppet implementation of motd

do not modify this file, change will be
lost once it is run from puppet master.


-TechTeam
[root@puppetdev Packages]#



define node

[root@devpup]# pwd
/etc/puppetlabs/code/environments/production/manifests
[root@devpup]# vi site.pp

node "puppetdev.expanor.local" {
        notify { 'This is a test notify': }
}


[root@devpup]# pwd
/etc/puppetlabs/code/environments/production
[root@devpup]# ls
environment.conf  hieradata  manifests  modules
[root@devpup]# ls -lR /etc/puppetlabs/code/environments/production
/etc/puppetlabs/code/environments/production:
total 4
-rw-r--r--. 1 pe-puppet pe-puppet 879 Oct 11  2016 environment.conf
drwxr-xr-x. 2 pe-puppet pe-puppet   6 Oct 11  2016 hieradata
drwxr-xr-x. 2 pe-puppet pe-puppet  40 Sep  5 17:38 manifests
drwxr-xr-x. 3 pe-puppet pe-puppet  17 Sep  5 13:44 modules

/etc/puppetlabs/code/environments/production/hieradata:
total 0

/etc/puppetlabs/code/environments/production/manifests:
total 8
-rw-r-----. 1 pe-puppet pe-puppet 1538 Sep  5 17:38 site.pp
-rw-r-----. 1 root      root      1309 Sep  2 13:27 site.pp.origg

/etc/puppetlabs/code/environments/production/modules:
total 0
drwxr-xr-x. 6 root root 62 Sep  5 13:46 motd

/etc/puppetlabs/code/environments/production/modules/motd:
total 0
drwxr-xr-x. 2 root root 17 Sep  5 17:39 files
drwxr-xr-x. 2 root root 20 Sep  5 13:56 manifests
drwxr-xr-x. 2 root root  6 Sep  5 13:46 templates
drwxr-xr-x. 2 root root 20 Sep  5 14:01 tests

/etc/puppetlabs/code/environments/production/modules/motd/files:
total 4
-rw-r--r--. 1 root root 144 Sep  5 17:39 motd

/etc/puppetlabs/code/environments/production/modules/motd/manifests:
total 4
-rw-r--r--. 1 root root 103 Sep  5 13:56 init.pp

/etc/puppetlabs/code/environments/production/modules/motd/templates:
total 0

/etc/puppetlabs/code/environments/production/modules/motd/tests:
total 4
-rw-r--r--. 1 root root 13 Sep  5 14:01 init.pp
[root@devpup]#