Managing Puppet Environment with r10k

Managing Environment with r10k

r10k is a code management tool that allows you to manage your environment configurations (such as production, testing, and development) in a source control repository. Based on the code in your control repo branches, r10k creates environments on your master and installs and updates the modules you want in each environment.

[root@www ~]# vim /etc/puppetlabs/puppet/r10k.yaml
sources:
operations:
remote: ‘git@github.com:rafi494/environments.git’
basedir: ‘/etc/puppetlabs/code/environments’
prefix: false

Create repository in github and also add ssh-key at github so that we can push the code as well from remote node.

[root@www ~]# cd /opt/

[root@www opt]# git clone git@github.com:rafi494/environments.git
Cloning into ‘environments’…
The authenticity of host ‘github.com (192.30.253.112)’ can’t be established.
RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘github.com,192.30.253.112’ (RSA) to the list of known hosts.
warning: You appear to have cloned an empty repository.

[root@www opt]# ll
total 0
drwxr-xr-x 3 root root 18 Aug 23 13:12 environments
drwxr-xr-x 8 root root 95 Aug 15 10:40 puppetlabs

[root@www opt]# cd environments/

[root@www environments]# librarian-puppet init
create Puppetfile

[root@www environments]# cat Puppetfile
#!/usr/bin/env ruby
#^syntax detection

forge “https://forgeapi.puppetlabs.com”

mod ‘puppetlabs-stdlib’

mod ‘puppetlabs-ntp’,
:git => ‘git://github.com/puppetlabs/puppetlabs-ntp.git’

mod ‘puppetlabs-apt’,
:git => ‘https://github.com/puppetlabs/puppetlabs-apt.git’,
:ref => ‘1.4.x’

[root@www environments]# mkdir manifests
[root@www environments]# vim manifests/site.pp
node /agent/ {
include testrepo
}

[root@www environments]# vim environments.conf
modulepath = site:modules:$basemodulepath
manifest = manifests/site.pp

[root@www environments]# ls -l
total 8
-rw-r–r– 1 root root 71 Aug 23 13:40 environments.conf
drwxr-xr-x 2 root root 21 Aug 23 13:37 manifests
-rw-r–r– 1 root root 292 Aug 23 13:19 Puppetfile

[root@www environments]# cd /opt/

[root@www opt]# git clone git@github.com:rafi494/testrepo.git
Cloning into ‘testrepo’…
Warning: Permanently added the RSA host key for IP address ‘192.30.255.112’ to the list of known hosts.
warning: You appear to have cloned an empty repository.

[root@www opt]# cd testrepo/
[root@www testrepo]# mkdir manifests
[root@www testrepo]# touch manifests/init.pp
[root@www testrepo]# vim manifests/init.pp
class testrepo {
if $environment == ‘production’ {
notify { ‘default-message’ :
message => “This is the production environment”
}
} else {
notify { ‘default-message’ :
message => “This is not production”
}
}
}

[root@www testrepo]# git add -A
[root@www testrepo]# git commit -m “Initial commit”
[master (root-commit) 921b85e] Initial commit
Committer: root <root@www.server.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

git config –global user.name “Your Name”
git config –global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

git commit –amend –reset-author

1 file changed, 11 insertions(+)
create mode 100644 manifests/init.pp
[root@www testrepo]# git push origin master
Warning: Permanently added the RSA host key for IP address ‘192.30.253.113’ to the list of known hosts.
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (4/4), 376 bytes | 0 bytes/s, done.
Total 4 (delta 0), reused 0 (delta 0)
To git@github.com:rafi494/testrepo.git
* [new branch] master -> master
[root@www testrepo]#

[root@www testrepo]# vim ../environments/Puppetfile
#!/usr/bin/env ruby
#^syntax detection

forge “https://forgeapi.puppetlabs.com&#8221;

mod ‘puppetlabs-stdlib’

mod ‘puppetlabs-ntp’,
:git => ‘git://github.com/puppetlabs/puppetlabs-ntp.git’

mod ‘puppetlabs-apt’,
:git => ‘https://github.com/puppetlabs/puppetlabs-apt.git&#8217;,
:ref => ‘1.4.x’

mod ‘testrepo’,
:git => ‘https://github.com/rafi494/testrepo.git&#8217;,
:branch => ‘master’

[root@www testrepo]# cd ../environments/
[root@www environments]# git add -A
[root@www environments]# git commit -m “adding env files”
[master (root-commit) 875c19c] adding env files
Committer: root <root@www.server.com>
Your name and email address were configured automatically based
on your username and hostname. Please check that they are accurate.
You can suppress this message by setting them explicitly:

git config –global user.name “Your Name”
git config –global user.email you@example.com

After doing this, you may fix the identity used for this commit with:

git commit –amend –reset-author

4 files changed, 26 insertions(+)
create mode 100644 .gitignore
create mode 100644 Puppetfile
create mode 100644 environments.conf
create mode 100644 manifests/site.pp
[root@www environments]# git push origin master
Counting objects: 7, done.
Compressing objects: 100% (4/4), done.
Writing objects: 100% (7/7), 682 bytes | 0 bytes/s, done.
Total 7 (delta 0), reused 0 (delta 0)
To git@github.com:rafi494/environments.git
* [new branch] master -> master

[root@www environments]# git branch production
[root@www environments]# git branch staging
[root@www environments]# git branch test
[root@www environments]# git checkout production
Switched to branch ‘production’
[root@www environments]# git push origin production
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:rafi494/environments.git
* [new branch] production -> production
[root@www environments]# git checkout staging
Switched to branch ‘staging’
[root@www environments]# git push origin staging
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:rafi494/environments.git
* [new branch] staging -> staging
[root@www environments]# git checkout test
Switched to branch ‘test’
[root@www environments]# git push origin test
Warning: Permanently added the RSA host key for IP address ‘192.30.255.113’ to the list of known hosts.
Total 0 (delta 0), reused 0 (delta 0)
To git@github.com:rafi494/environments.git
* [new branch] test -> test

[root@www environments]# gem install r10k –no-rdoc –no-ri
ERROR: While executing gem … (Gem::DependencyError)
Unable to resolve dependencies: r10k requires semantic_puppet (~> 0.1.0)

[root@www environments]# gem install puppet_forge:2.2.6 r10k
Fetching: semantic_puppet-0.1.4.gem (100%)
Successfully installed semantic_puppet-0.1.4
Fetching: puppet_forge-2.2.6.gem (100%)
Successfully installed puppet_forge-2.2.6
Parsing documentation for semantic_puppet-0.1.4
Installing ri documentation for semantic_puppet-0.1.4
Parsing documentation for puppet_forge-2.2.6
Installing ri documentation for puppet_forge-2.2.6
Fetching: colored-1.2.gem (100%)
Successfully installed colored-1.2
Fetching: cri-2.6.1.gem (100%)
Successfully installed cri-2.6.1
Fetching: log4r-1.1.10.gem (100%)
Successfully installed log4r-1.1.10
Fetching: multi_json-1.12.1.gem (100%)
Successfully installed multi_json-1.12.1
Fetching: r10k-2.5.5.gem (100%)
Successfully installed r10k-2.5.5
Parsing documentation for colored-1.2
Installing ri documentation for colored-1.2
Parsing documentation for cri-2.6.1
Installing ri documentation for cri-2.6.1
Parsing documentation for log4r-1.1.10
Installing ri documentation for log4r-1.1.10
Parsing documentation for multi_json-1.12.1
Installing ri documentation for multi_json-1.12.1
Parsing documentation for r10k-2.5.5
Installing ri documentation for r10k-2.5.5
7 gems installed

-p to process Puppetfile
-c configfile
[root@www environments]# r10k deploy environment -p -c /etc/puppetlabs/puppet/r10k.yaml

[root@www ~]# r10k deploy environment -p -c /etc/puppetlabs/puppet/r10k.yaml -v debug2 –trace

[root@www ~]# ls -l /etc/puppetlabs/code/environments/
total 0
drwxr-xr-x 5 root root 136 Aug 24 00:09 master
drwxr-xr-x 5 root root 136 Aug 24 00:10 production
drwxr-xr-x 5 root root 136 Aug 24 00:12 staging
drwxr-xr-x 5 root root 136 Aug 24 00:13 test
[root@www ~]# ls -l /etc/puppetlabs/code/environments/master/
total 8
-rw-r–r– 1 root root 72 Aug 24 00:07 environments.conf
drwxr-xr-x 2 root root 21 Aug 24 00:07 manifests
drwxr-xr-x 6 root root 58 Aug 24 00:09 modules
-rw-r–r– 1 root root 384 Aug 24 00:07 Puppetfile
[root@www ~]# ls -l /etc/puppetlabs/code/environments/master/modules/
total 8
drwxr-xr-x 7 root root 296 Aug 24 00:09 apt
drwxr-xr-x 11 root root 4096 Aug 24 00:09 ntp
drwxr-xr-x 9 root root 4096 Aug 24 00:08 stdlib
drwxr-xr-x 4 root root 35 Aug 24 00:09 testrepo
[root@www ~]# ls -l /etc/puppetlabs/code/environments/master/manifests/
total 4
-rw-r–r– 1 root root 35 Aug 24 00:07 site.pp

[root@www ~]# puppet agent -t
Info: Using configured environment ‘production
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for agent
Info: Applying configuration version ‘1503514188’
Notice: This is the production environment
Notice: /Stage[main]/Testrepo/Notify[default-message]/message: defined ‘message’ as ‘This is the production environment’
Notice: Applied catalog in 0.22 seconds

[root@www ~]# puppet agent -t –environment staging
Info: Using configured environment ‘staging
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Loading facts
Info: Caching catalog for agent
Info: Applying configuration version ‘1503514258’
Notice: This is not production
Notice: /Stage[main]/Testrepo/Notify[default-message]/message: defined ‘message’ as ‘This is not production’
Notice: Applied catalog in 0.16 seconds

Leave a comment