- Puppet is an open source configuration management tool that helps you to manage the configurations of hundreds of client systems from the central location.
- It can work on multiple operating systems based on Unix as well as on Microsoft Windows
- Puppet is Infrastructure management platform that allows automation of repeated tasks and it also includes its own declarative language to describe system configuration, the Puppet Language that is based on Ruby under released License of Apache.
- Puppet is arranged in a master – agent architecture. The master serves the manifests and files, and the agents poll the master at specific intervals of time to get their configuration. The master does not push anything into the client.
- Agents identify with the master using SSL, so the first time an agent tries to connect to the master, the agent certificate needs to be approved (in the default configuration), and that’s usually a source of problems.
- Puppet Master: The master server that controls configuration on the nodes
- Puppet Agent Node: A node controlled by a Puppet Master
- Manifest: a file that contains a set of instructions to be executed
- Resource: a portion of code that declares an element of the system and how its state should be changed. For instance, to install a package we need to define a package resource and ensure its state is set to “installed”
- Module: a collection of manifests and other related files organized in a pre-defined way to facilitate sharing and reusing parts of a provisioning
- Class: just like with regular programming languages, classes are used in Puppet to better organize the provisioning and make it easier to reuse portions of the code
- Facts: global variables containing information about the system, like network interfaces and operating system
- Services: used to trigger service status changes, like restarting or stopping a service
Here, we will configure a puppet in Server/agent architecture.
Puppet Master:
Host Name: vvm01
IP Address: 192.168.2.121
Operating System: CentOS 7.6
Puppet client:
Host Name: vvm02
IP Address: 192.168.2.122
Operating System: CentOS 7.6
Host Name: vvm03
IP Address: 192.168.2.123
Operating System: Redhat 7.6
Host Name: vvm04
IP Address: 192.168.2.124
Operating System: Ubuntu 18.04
IP Address: 192.168.2.125
Operating System: Window 2016
Prerequisites
Install NTP:
Timings of the master and client nodes should be accurately in sync with upstream time servers because the Puppet master server master will be acting as the certificate authority.
(If the time is wrong, it might mistakenly issue agent certificates from the distant past or future date, which other nodes will treat as expired.)
Install the NTP package and perform the time sync with upstream NTP servers.
[root@vvm01 ~]# yum -y install ntpdate
[root@vvm01 ~]# ntpdate 0.centos.pool.ntp.org
DNS
Puppet agent uses the hostname to communicate with the Puppet Server. So, make sure agent nodes can resolve the hostname of Puppet Server with the help of /etc/hosts file or DNS server.
[root@vvm01 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
192.168.2.121 vvm01
192.168.2.122 vvm02
192.168.2.123 vvm03
192.168.2.124 vvm04
[root@vvm01 ~]#
To install the Puppet Server/Agents, we would need to add puppet repository on all the nodes.
[root@vvm01 ~]# rpm -Uvh https://yum.puppet.com/puppet6-release-el-7.noarch.rpm
Install Puppet Server
Puppet Server is the server software that runs on the puppet master node. Puppet master pushes the configurations to managed nodes (puppet-agent).
Puppet server is now installed, do not start the puppet server service yet. Configure Puppet Server
Memory Allocation By default, Puppet Server JVM is configured to use 2GB of memory. You can change it, depends on how much memory available on your master node; ensure that it is enough for managing all the nodes connected to it. To change the value of memory allocation, edit the below file.
[root@vvm01 ~]# vi /etc/sysconfig/puppetserver
Change the value.
From:
JAVA_ARGS="-Xms2g -Xmx2g -Djruby.logger.class=com.puppetlabs.jruby_utils.jruby.Slf4jLogger"To: For 512MB, use below settings.
JAVA_ARGS="-Xms512m -Xmx512m -Djruby.logger.class=com.puppetlabs.jruby_utils.jruby.Slf4jLogger"
Embedded Web Server
Puppet Server uses a Jetty-based web server embedded in the service’s JVM process. You do not need to configure or enable the web server; it works out of the box. It performs well under production-level loads.
The embedded web server’s settings can be modified in webserver.conf. You might need to edit this file if you are using an external CA or running Puppet Server on a non-standard port.
Puppet Configuration
Puppet Server does not require any configuration. You can just start the Puppet server service. It will use the default settings. If you want to change puppet master hostname, follow the below procedure.
Advanced Configurations (optional) Here, I am going to modify the Puppet Master settings for our requirement.
[root@vvm01 ~]# vi /etc/puppetlabs/puppet/puppet.conf
Place the below lines. Modify it according to your environment.
dns_alt_names = vvm01
[main]
certname = vvm01
server = vvm01
environment = production
runinterval = 1h
Generate a root and intermediate signing CA for Puppet Server.
[root@vvm01 ~]# /opt/puppetlabs/bin/puppetserver ca setup
Output:
Generation succeeded. Find your files in /etc/puppetlabs/puppet/ssl/ca
Start and enable the Puppet Server.
[root@vvm01 ~]#systemctl start puppetserver
[root@vvm01 ~]#systemctl enable puppetserver
Firewall
The Puppet Master listens on port 8140, so configure the firewall in such way that managed nodes can connect to the master.
[root@vvm01 ~]# firewall-cmd --permanent --add-port=8140/tcp
[root@vvm01 ~]#firewall-cmd --reload
[root@vvm01 ~]# firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: eth0
sources:
services: ssh dhcpv6-client http
ports: 8140/tcp
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
[root@vvm01 ~]#
Install Puppet Agent
Install the puppet agent on your client using the below command.
[root@vvm02 ~]# rpm -Uvh https://yum.puppet.com/puppet6-release-el-7.noarch.rpm
[root@vvm02 ~]# yum install -y puppet-agent
Puppet agent also uses some of the default settings to connect to the master node. However, we need to edit the puppet configuration file and set puppet master information.
Set server value as per your master node name. In my case, the server is vvm01, and certname is my client hostname vvm02, vvm03 & vvm04.
[root@vvm02 ~]# vi /etc/puppetlabs/puppet/puppet.conf
[main]
certname = vvm02
server = vvm01
environment = production
runinterval = 1h
You can change the value of runinterval depends on the requirement. You can set the value in seconds (10 or 10s), minutes (10m) and hours (1h). This setting controls how long the agent should wait between the two catalog requests.
Start puppet agent on the node and make it start automatically on system boot.
[root@vvm02 ~]# /opt/puppetlabs/bin/puppet resource service puppet ensure=running enable=true
Output:
Notice: /Service[puppet]/ensure: ensure changed 'stopped' to 'running'
service { 'puppet':
ensure => 'running',
enable => 'true',
}
Repeat the procedure for vvm03 and vvm04.
In an agent/master deployment, an admin must approve a certificate request coming from each node so that they can fetch the configurations. Agent nodes will request certificates for the first time if they attempt to run.
Log into the puppet master server and run below command to view outstanding requests.
[root@vvm01 ~]# puppetserver ca list
Output:
Requested Certificates:
vvm02 (SHA256) EF:BE:B3:75:00:CE:ED:99:FA:6E:FB:FB:A1:94:4B:34:B4:EF:01:B0:BE:41:74:0D:3D:2A:73:EA:15:14:FE:69
vvm03 (SHA256) 78:E8:66:A5:6D:48:27:8C:36:99:28:8A:C0:8E:2C:45:FE:D3:5D:F1:FC:EF:4C:B3:A2:06:AE:84:70:41:77:21
vvm04 (SHA256) 5B:27:6B:CD:DC:4C:64:D0:F0:86:AE:12:A9:FD:F4:3B:83:1F:D4:D3:FF:08:AC:1C:1D:F4:38:A2:2D:6C:B0:7F
Run puppet cert sign command to sign a request.
[root@vvm01 ~]#puppetserver ca sign --certname vvm02
Output:
The puppet master can now communicate to the client machine and control the node.
If you have multiple signing requests from nodes, you can sign all the requests in one command.
[root@vvm01 ~]# puppetserver ca sign --all
Successfully signed certificate request for vvm02
Successfully signed certificate request for vvm03
Successfully signed certificate request for vvm04
[root@vvm01 ~]#
Sometimes, you may need to revoke the certificate of a particular node to read them back.
Replace the <hostname> with your client hostname.
[root@vvm01 ~]# puppetserver ca revoke --certname vvm02
[root@vvm01 ~]# puppetserver ca clean --certname vvm02
After removing the certificate, if you want to reconnect to vvm02 to vvm01(puppet master) then follow the below steps.
1. Move the files from the below locations & rerun the test.
[root@vvm02 ~]# mv /etc/puppetlabs/puppet/ssl/private_keys/ /root/
[root@vvm02 ~]# mv /etc/puppetlabs/puppet/ssl/certs/ /root
[root@vvm02 ~]# puppet agent --test
2. Reinstall the puppet agent.
You can list all of the signed and unsigned requests with the below command.
[root@vvm01 ~]# puppetserver ca list --all
Output:
Signed Certificates:
vvm01 (SHA256) F3:6D:6A:85:84:20:90:78:C5:66:94:10:0C:10:6D:F1:6D:13:FD:99:CA:58:64:B3:68:C0:44:74:70:17:2C:C4 alt names: ["DNS:vvm01", "DNS:vvm01"]
vvm02 (SHA256) 73:99:23:4F:F0:0C:30:CA:F3:5B:7B:BE:85:8F:15:B3:F4:C7:EA:AF:E2:C4:16:82:07:CF:E5:64:F7:1B:55:D4
vvm03 (SHA256) 5E:AC:C8:BF:4F:C1:B9:1B:5E:BD:9E:90:A2:A5:D1:31:1E:B0:70:BA:01:7B:8E:0A:7D:52:2C:23:69:1B:37:AB
vvm04 (SHA256) 34:10:B0:B9:44:5D:3B:39:80:05:2D:39:0B:63:D3:E4:BC:5B:2B:D2:0C:56:6C:58:3F:49:6B:17:E5:4D:03:78
Verify Puppet Client
Once the Puppet Server has signed your client certificate, run the following command on the client machine to test it.
[root@vvm02 ~]# puppet agent --test
Output:
Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Retrieving locales
Info: Caching catalog for vvm04
Info: Applying configuration version '1583641427'
Notice: Applied catalog in 0.02 seconds
[root@vvm02 ~]#
Manifest is a data file which contains client configuration’s, written in Puppet’s declarative language or a Ruby DSL and saved with an extension of .pp file. (pp = puppet program)
Puppet manifest file is located at /etc/puppetlabs/code/environments/production/manifests/
Task-1: Create /etc/motd and /etc/issue on all the servers.
[root@vvm01 ]#vi file.pp
file {'motd': #Creation of /etc/motd file
path => '/etc/motd',
content => '###############################################################
# Welcome to CentOS-Linux
# All connections are monitored and recorded
# Disconnect IMMEDIATELY if you are not an authorized user!
###############################################################
',
}
file { '/etc/issue': ### Creation of /etc/issue file
ensure => present,
content => "\S
Kernel \r on an \m
Today is \d \t @ \n",
mode => '0644',
owner => 'root',
group => 'root',
path => $issue_file_path,
}
Perform the below activity on vvm01 node.
[root@vvm01 ]# groupadd localadmin
[root@vvm01 ]# useradd -g localadmin admin
[root@vvm01 ]# passwd admin
[root@vvm01 ]# puppet resource user admin
[root@vvm01 ]# puppet resource group localadmin
group { 'localadmin':
ensure => 'present',
provider => 'groupadd',
}
user { 'admin':
ensure => 'present',
groups => ['localadmin'],
home => '/home/admin',
password => '$6$OzsxT9l/$lN/jt59efODN79LlzMjhRHBxsGK78iGUjHM.kfJuATNcAq01ogHV8In5Gx6UqL1xNDfFwolNwNJXtoyMlue6O0',
password_max_age => 99999,
password_min_age => 0,
password_warn_days => 7,
provider => 'useradd',
shell => '/bin/bash',
managehome => true,
}
augeas { "sudo-localadmin":
context => "/files/etc/sudoers",
changes => [
"set spec[user = 'admin']/user admin",
"set spec[user = 'admin']/host_group/host ALL",
"set spec[user = 'admin']/host_group/command ALL",
"set spec[user = 'admin']/host_group/command/runas_user ALL",
],
}
[root@vvm01 ]# vi ssh_login.pp
augeas { "sshd_config":
changes => [
"set /files/etc/ssh/sshd_config/PermitRootLogin no",
"set /files/etc/ssh/sshd_config/LoginGraceTime 2m",
],
}
exec { 'sshd':
command => '/usr/bin/systemctl restart sshd'
}
Task-4: Execute command using puppet manifest.
[root@vvm01 ]# vi command.pp
exec { 'date':
command => '/usr/bin/date >> /root/abc'
}
$packages = ['ftp', 'elinks', 'curl' ,'nfs-utils',]
package { $packages:
ensure => "installed"
}
Task-6: Create condition statement using "IF" and "CASE"
[root@vvm01 ]# vi condition.pp
$my_variable = 'three'
if $my_variable == 'one' {
notify {'The value of our variable is one': }
}
elsif $my_variable == 'two' {
notify {'The value of our variable is two': }
}
else {
notify {"The value of our variable is $my_variable": }
}
case $my_variable {
'one': {
notify {'The value of our variable is one by case': }
}
default : {
notify {"The value of our variable is ${my_variable} by case": }
}
}
CentOS
[root@vvm01 ~]# vi facter.pp
if $::operatingsystem =='CentOS' {
notify {" Installing on CentOS" :}
}
Note: Facter command provide complete inventory of system.
Task-8: Create directory nfs_disk and mount nfs partition on all servers.
ensure => 'directory',
group => root,
owner => root,
}
mount { '/nfs_disk':
ensure => 'mounted',
atboot => false,
device => 'vm01:/nfs_share',
fstype => 'nfs',
options => 'defaults',
pass => 0,
}
command => '/bin/firewall-cmd --add-service http',
command => '/bin/firewall-cmd --add-service http --permanent',
}
exec { "test":
command => '/bin/echo apache2 is installed on vvm02 > /nfs_disk/vvm02_status.txt',
onlyif => '/bin/which httpd',
}
class ntpconfig {
service { 'ntpd': # Start NTP Service
ensure => running,
require => [ # Required ntp packages and configuration file
Package['ntp'],
File['/etc/ntp.conf'],
],
}
package { 'ntp':
ensure => present,
before => Service['ntpd'], # Before starting the server check ntp package.
}
file { '/etc/ntp.conf': # It will copy the ntp.conf file to client server.
ensure => file,
mode => '0600',
source => 'puppet:///modules/ntpconfig/ntp.conf',
before => Service['ntpd'],
}
}
if $::osfamily =='windows' {
host { 'ans01.darole.org' :
ensure => present,
host_aliases => [ 'ans01.darole.org' ],
ip => '172.16.1.223'
}
}
if $::osfamily =='windows' {
service { 'TermService':
ensure=> running,
enable => true,
}
service { 'Zabbix Agent':
ensure=> running,
enable => true,
}
}
[root@pup01 manifests]#
if $::osfamily =='windows' {
file { 'c:\puppet-file.txt':
ensure => file,
owner => 'Administrator',
group => 'Users',
mode => '0644',
content =>
'################################################################
# Welcome to Darole.org #
# All Connection are monitored and recorded #
# Disconnect IMMEDIATELY if you are not an authorized user !!! #
################################################################ ',
}
}
[root@pup01 manifests]#
[root@vvm01 ]# puppet config print all
Puppet store all the report on below location
[root@vvm01 ]# puppet config print report
true
[root@vvm01 ]# puppet config print reportdir
/opt/puppetlabs/puppet/cache/reports
Puppet store all the yaml file on below location
[root@vvm01 ~]# tail /opt/puppetlabs/server/data/puppetserver/reports/vvm04/202003141641.yaml
changed: false
out_of_sync: false
skipped: false
change_count: 0
out_of_sync_count: 0
events: []
corrective_change: false
corrective_change: false
catalog_uuid: 821d02db-fe75-4062-b3da-ea7be54449f3
cached_catalog_status: not_used
[root@vvm01 ~]#
Run puppet agent in foreground and debug mode:
#puppet agent --test --debug
Run a dry-run puppet without making any change to the system:
#puppet agent --test --noop
Run puppet using an environment different from the default one:
#puppet agent --test --environment testing
Wait for certificate approval (by default 120 seconds) in the first Puppet run (useful during automated first time installation if PuppetMaster's autosign is false):
#puppet agent --test --waitforcert 120
Resource
This command provides simple facilities for converting current system state into Puppet code, along with some ability to modify the current state using Puppet's RAL
https://puppet.com/docs/puppet/latest/type.html
[root@vvm01 ~]# /opt/puppetlabs/bin/puppet resource --types
cron
exec
file
filebucket
group
host
mount
notify
package
resources
schedule
scheduled_task
selboolean
selmodule
service
ssh_authorized_key
sshkey
stage
tidy
user
whit
yumrepo
zfs
zone
zpool
[root@vvm01 ~]#
The below command will provide you detail of user vdarole exist on server vvm02
[root@vvm02 ~]# puppet resource user vdarole
user { 'vdarole':
ensure => 'present',
gid => 1000,
home => '/home/vdarole',
password => '$6$PPrVOJSt$ELwTKHKzLnV7YFDHqs4ZxPWo1lDvrDRmmU92JfV326qWiyqtjEI/gXBFUdLi/jTkV3tpD79BqHfNERgP3DVy2/',
password_max_age => 99999,
password_min_age => 0,
password_warn_days => 7,
provider => 'useradd',
shell => '/bin/bash',
uid => 1000,
}
The below command will provide you detail of configuration file exist on server vvm02
[root@vvm02 ~]# puppet resource file /etc/httpd/conf/httpd.conf
file { '/etc/httpd/conf/httpd.conf':
ensure => 'file',
content => '{md5}f5e7449c0f17bc856e86011cb5d152ba',
ctime => '2020-03-10 21:53:38 +0530',
group => 0,
mode => '0644',
mtime => '2019-08-06 19:14:34 +0530',
owner => 0,
provider => 'posix',
selrange => 's0',
selrole => 'object_r',
seltype => 'httpd_config_t',
seluser => 'system_u',
type => 'file',
}
The below command will provide you detail of service running on server vvm02
[root@vvm02 ~]# puppet resource service httpd
service { 'httpd':
ensure => 'running',
enable => 'false',
provider => 'systemd',
}
[root@vvm02 ~]#
The below command will provide you detail of package http on server vvm02
[root@vvm02 ~]# puppet resource package httpd
package { 'httpd':
ensure => '2.4.6-90.el7.centos',
provider => 'yum',
}
The below video is for how to Installation and Configuration Puppet 6 Master and Client on CentOS7 with multiple examples manifests.
In the below links are for how to Installation and Configuration Puppet agent on Ubuntu and Windows
[root@ans01 ]# useradd --uid 4001 ansible
Set password to ansible user.
[root@ans01 ]# passwd ansible
Copy the password of ansible user from shadow file.
[root@ans01 ]# cat /etc/shadow | grep ansible
Generate authorized key for ansible user.
[ansible@ans01 ~]$ ssh-keygen
Create module for authorized_keys (below file stucture) in module folder (/etc/puppetlabs/code/environments/production/module)
[root@pup01 ansfile]# tree
.
├── files
│ └── authorized_keys
└── manifests
└── init.pp
Copy the authorized_keys to below location
[root@pup01 modules]# cat ansfile/files/authorized_keys
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDkUVQI3Agc/zbgkeU6MhdtboeuJc3jdU9Y9ONWHa8oOuMGjR/DP5MrQ+Ce5noZBpr1xLvMSIQIA0TeDnJHe+Kg8afi5P1kdfYD11II6YZqDZcBmbEtY7lHl6zCsZJiublFwW2bidHZp0e1sifNceV9MJYXQy9jpzUswyRyLFUq6uYizGkYxcKJoegBT18pEO0638KkJ2EaXbJwhl9Edc5K17fE0r1oqgzzBgeqeREO+mkpskPxy8aCp1xivyPu6ZArzfd0Ri5toLAsnADohJEi0Ku313UbvgjsOjxjr1z783xgBpu3qcBFq0cL3GpH/ZYaCvQogs3yfJHFj6WoBkR/ ansible@ans01.darole.org
Write below manifest for coping the authorized_keys in module directory
[root@pup01 modules]# cat ansfile/manifests/init.pp
class ansfile {
file {'/home/ansible/.ssh/authorized_keys':
ensure => file,
source => 'puppet:///modules/ansfile/authorized_keys',
owner => 'ansible',
group => 'ansible',
mode => '0600',
}
}
[root@pup01 modules]#
Write the main manifests in to create ansible user with password-less login on all Linux servers
[root@pup01 manifests]# cat user-creation.pp
if $::kernel =='Linux' {
user { 'ansible':
ensure => 'present',
home => '/home/ansible',
password => '$6$Xqu9Qn5Q$Srxo5qVd7NxVtA.U/WtXaktHYXRB5YZIlctuho8L85DkX0bTzL/KdB0GfessD4QCsjfUfZFFFbGadRjtlokWt0',
password_max_age => 99999,
password_min_age => 0,
password_warn_days => 7,
provider => 'useradd',
shell => '/bin/bash',
uid => 4001,
managehome => true
}
augeas { "sudo-ansible":
context => "/files/etc/sudoers",
changes => [
"set spec[user = 'ansible']/user ansible",
"set spec[user = 'ansible']/host_group/host ALL",
"set spec[user = 'ansible']/host_group/command ALL",
"set spec[user = 'ansible']/host_group/command/runas_user ALL",
"set spec[user = 'ansible']/host_group/command/tag NOPASSWD",
],
}
file { '/home/ansible/.ssh/':
ensure => 'directory',
group => 4001,
owner => 4001,
mode => '0600',
}
include ansfile
}
No comments:
Post a Comment