Skip to content

Commit

Permalink
Puppet master (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkn3rd authored Oct 10, 2024
1 parent 8cea009 commit f54c12f
Show file tree
Hide file tree
Showing 10 changed files with 295 additions and 15 deletions.
49 changes: 34 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,54 @@ These are code related to tutorials created.

### General

#### **Containerization**

* Docker
* Docker Command vs Ansible - https://medium.com/@Joachim8675309/docker-using-shell-or-docker-compose-4ab8ea8ae801
* Docker Command vs. Docker Compose - https://medium.com/@Joachim8675309/docker-using-shell-or-ansible-7cdceb646d3

#### **Virtualization**

* Virtual Box
* Windows
* Windows 8.1 - https://medium.com/@Joachim8675309/virtualbox-and-friends-on-windows-8-1-3c691460698f
* macOS (Mac OS X)
* macOS 10.13.5 - https://medium.com/@Joachim8675309/virtualbox-and-friends-on-macos-fd0b78c71a32
* Linux
* Fedora 28 - https://medium.com/@Joachim8675309/vagrant-and-friends-on-fedora-28-37b8cbc47e47
* Vagrant
* Provisioners
* **Vagrant**: manage dev environments through automatic provisioning and configuration of virtual machines or containers.
* **Providers**
* Virtualbox
* [Vagrant with Macbook (intel)](https://joachim8675309.medium.com/vagrant-with-macbook-intel-dfc8efffe148) on May 7, 2024 - example of using Virtualbox to run Windows, macOS, FreeBSD, ArchLinux, FreeBSD on Intel Macbook.
* QEMU (`vagrant-qemu`)
* [Vagrant with Macbook Mx (arm64)](https://joachim8675309.medium.com/vagrant-with-macbook-mx-arm64-0f590fd7e48a) on March 14, 2024: demonstrate how to run arm64 based VMs (Hyperkit) and Intel based VMs (Q35 emulator) using `vagrant-qemu` plugin on arm64-based Macbook M-Series.
* **Provisioners**
* Shell (`shell`) - https://medium.com/@Joachim8675309/vagrant-provisioning-e4c9fd57968d
* Ansible (`ansible_local`) - https://medium.com/@Joachim8675309/vagrant-provisioning-with-ansible-6dba6bca6290
* Docker (`docker`) - https://medium.com/@Joachim8675309/vagrant-provisioning-with-docker-3621df12092a
* Chef (`chef_zero`) - https://medium.com/@Joachim8675309/vagrant-provisioning-with-chef-90a2bf724f
* Puppet (`puppet`) - https://medium.com/@Joachim8675309/vagrant-provisioning-with-puppet-553a59f0c48e
* Salt Stack (`salt`) - https://medium.com/@Joachim8675309/vagrant-provisioning-with-saltstack-50dab12ce6c7
* Kitchen (Test Kitchen)
* Chef, Busser/ServerSpec, InSpec
* https://medium.com/@Joachim8675309/testkitchen-with-chef-and-serverspec-2ac0cd938e5
* **Test Kitchen** - automates the process of creating, configuring, and testing systems
* Chef, ServerSpec, Busser/ServerSpec
* [TestKitchen with Chef](https://joachim8675309.medium.com/testkitchen-with-chef-and-serverspec-2ac0cd938e5) on Aug 28, 2018
* Ansible, Busser/TestInfra, Shell/TestInfra
* https://medium.com/@Joachim8675309/testkitchen-with-ansible-and-testinfra-e3fc4320ced
* Chef
* Overview: https://medium.com/@Joachim8675309/testing-on-the-chef-platform-overview-8e29b4f050ca
* Integration w/ InSpec: https://medium.com/@Joachim8675309/testing-chef-cookbooks-with-inspec-c18ec1119c94
* Salt Stack
* developing formulas (`vagrant`) - https://joachim8675309.medium.com/building-gke-with-terraform-869df1cd3f41
* using external formulas (`vagrant`) - https://joachim8675309.medium.com/salt-devkit-with-external-formulas-9e38d8b90cd7
* [TestKitchen with Ansible and TestInfra](https://joachim8675309.medium.com/testkitchen-with-ansible-and-testinfra-e3fc4320ced) on Sep 6, 2018

#### **Change Configuration**

Aautomate the management/deployment of infrastructure by defining the desired state of systems through code

* **Chef**
* [Vagrant Provisioning with Chef](https://joachim8675309.medium.com/vagrant-provisioning-with-chef-90a2bf724f) on Aug 10, 2018 - `chef-zero`
* [Testing on the Chef Platform: Overview](https://joachim8675309.medium.com/testing-on-the-chef-platform-overview-8e29b4f050ca) on Aug 23, 2018
* [Testing Chef Cookbooks with InSpec](https://joachim8675309.medium.com/testing-chef-cookbooks-with-inspec-c18ec1119c94) on Aug 31, 2018
* **Puppet**
* [Vagrant Provisioning with Puppet](https://joachim8675309.medium.com/vagrant-provisioning-with-puppet-553a59f0c48e) on Aug 12, 2018 - Puppet 5, Provisioner: `puppet`
* [Master of Puppets with Vagrant](https://joachim8675309.medium.com/master-of-puppets-with-vagrant-b29be7858b28) on Sep 2024 - Puppet 8, Provisioner: `puppet_server`
* [Puppetry with Vagrant](https://joachim8675309.medium.com/puppetry-with-vagrant-1b5e0613672a) on Sep 2024 - Puppet 8, Provisioner: `puppet`
* **Salt Stack (Salt Project)**
* [Vagrant Provisioning with SaltStack](https://joachim8675309.medium.com/vagrant-provisioning-with-saltstack-50dab12ce6c7) on Aug 12, 2018
* [Salt DevKit with External Formulas](https://joachim8675309.medium.com/salt-devkit-with-external-formulas-9e38d8b90cd7) on Feb 6, 2020
* [Salt DevKit: Developing Formulas](https://joachim8675309.medium.com/salt-devkit-developing-formulas-e8b500d6b970) on Feb 10, 2020
* [Adding Salt to Vagrant](https://medium.com/@joachim8675309/adding-salt-to-vagrant-43c162e67008) on Aug 6, 2024 - `salt 3007.1`

### Language Platforms

Expand Down
4 changes: 4 additions & 0 deletions vagrant/puppet_server/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Puppet Server Provisioner

Articles about using the `puppet_server` provisioner where code must work with a Puppet Server.

65 changes: 65 additions & 0 deletions vagrant/puppet_server/hello_web_proj/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Hello Web Project

## Instructions

1. Setup `bash setup.sh`
2. Launch Guests
```bash
vagrant up --no-provision
```
3. Install Server
```bash
vagrant provision puppetserver01 --provision-with "bootstrap"
vagrant ssh puppetserver01 \
--command "sudo /opt/puppetlabs/bin/puppetserver ca list --all"
```
4. Install Agents
```bash
for NODE in node0{1..2}; do
vagrant provision $NODE --provision-with "bootstrap"
done
```
5. Issue Certificate Requests
```bash
for NODE in node0{1..2}; do
printf "\n$NODE: Testing connection (expect failure)\n"
vagrant ssh $NODE --command 'sudo /opt/puppetlabs/bin/puppet agent --test'
done
```
6. Verify
```bash
vagrant ssh puppetserver01 --command "sudo /opt/puppetlabs/bin/puppetserver ca list"
```
7. Sign Certificates
```bash
for NODE in node0{1..2}.local; do
printf "\nSigning $NODE\n"
vagrant ssh puppetserver01 --command \
"sudo /opt/puppetlabs/bin/puppetserver ca sign --certname $NODE"
done
```
8. Verify
```bash
vagrant ssh puppetserver01 --command "sudo /opt/puppetlabs/bin/puppetserver ca list --all"
```
9. Test Connectivity
```bash
for NODE in node0{1..2}; do
printf "\n$NODE: Testing connection (expect success)\n"
vagrant ssh $NODE --command 'sudo /opt/puppetlabs/bin/puppet agent --test'
done
```
10. Provision
```bash
for NODE in node0{1..2}; do vagrant provision $NODE; done
```
11. Test Results
```bash
for NODE in node0{1..2}; do
vagrant ssh $NODE --command "curl --include localhost"
done
```
12. Cleanup
```bash
vagrant destroy --force
```
35 changes: 35 additions & 0 deletions vagrant/puppet_server/hello_web_proj/Vagrantfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Vagrant.configure("2") do |config|
config.vm.provision "bootstrap", before: :all,
type: "shell", path: "./bootstrap.sh"

# Puppet Master node
config.vm.define "puppetserver01" do |puppetserver|
puppetserver.vm.box = "generic/ubuntu2204"
puppetserver.vm.hostname = "puppetserver01.local"
puppetserver.vm.network "private_network", ip: "192.168.50.4"
puppetserver.vm.synced_folder "site",
"/etc/puppetlabs/code/environments/production"
end

# Node 1
config.vm.define "node01" do |node01|
node01.vm.box = "generic/ubuntu2204"
node01.vm.hostname = "node01.local"
node01.vm.network "private_network", ip: "192.168.50.5"
node01.vm.provision "puppet_server" do |puppet|
puppet.puppet_server = "puppetserver01.local"
puppet.options = "--verbose --debug"
end
end

# Node 2
config.vm.define "node02" do |node02|
node02.vm.box = "generic/ubuntu2204"
node02.vm.hostname = "node02.local"
node02.vm.network "private_network", ip: "192.168.50.6"
node02.vm.provision "puppet_server" do |puppet|
puppet.puppet_server = "puppetserver01.local"
puppet.options = "--verbose --debug"
end
end
end
89 changes: 89 additions & 0 deletions vagrant/puppet_server/hello_web_proj/boostrap.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#!/usr/bin/env bash

# global variables
PUPPET_FQDN="puppetserver01.local"
HOSTNAME_FQDN="$(hostname -f)"
HOSTS_ENTRIES="192.168.50.4 puppetserver01.local puppetserver01
192.168.50.5 node01.local node01
192.168.50.6 node02.local node02"

# main
main() {
if [[ "$HOSTNAME_FQDN" == "$PUPPET_FQDN" ]]; then
setup_hosts_file
if ! systemctl status puppetserver > /dev/null; then
install_puppet_server
configure_puppet_server "$PUPPET_FQDN"
sudo systemctl start puppetserver
systemctl status puppetserver && sudo systemctl enable puppetserver
else
echo "Puppet Server is already installed! skipping"
fi
else
setup_hosts_file
if ! command -v puppet > /dev/null; then
install_puppet_agent
configure_puppet_agent "$PUPPET_FQDN" "$HOSTNAME_FQDN"
else
echo "The Puppet Agent is already installed! skipping"
fi
fi
}

# setup /etc/hosts file
setup_hosts_file() {
if [[ "$HOSTNAME_FQDN" == "$PUPPET_FQDN" ]]; then
grep -q 'puppet$' /etc/hosts \
|| sudo sed -i '/127\.0\.0\.1 localhost/s/$/ puppet/' /etc/hosts
fi

while read -r ENTRY; do
grep -q ${ENTRY##* } /etc/hosts || \
sudo sh -c "echo '$ENTRY' >> /etc/hosts"
done <<< "$HOSTS_ENTRIES"
}

# add remote registry for puppet packages
add_puppet_registry() {
wget https://apt.puppetlabs.com/puppet8-release-$(lsb_release -cs).deb
sudo dpkg -i puppet8-release-$(lsb_release -cs).deb
}

# install puppet agent
install_puppet_agent() {
add_puppet_registry
sudo apt-get -qq update
sudo apt-get install -y puppet-agent
}

# install puppet server
install_puppet_server() {
add_puppet_registry
sudo apt-get -qq update
sudo apt-get install -y puppetserver
}

# configure puppet server
configure_puppet_server() {
# add entries if they do not yet exist
grep -q "dns_alt_names" /etc/puppetlabs/puppet/puppet.conf \
|| sudo sh -c \
"echo 'dns_alt_names = $1,${1%%.*},puppet' >> /etc/puppetlabs/puppet/puppet.conf"
grep -q "certname" /etc/puppetlabs/puppet/puppet.conf \
|| sudo sh -c "echo 'certname = $1' >> /etc/puppetlabs/puppet/puppet.conf"

# set default memory for test vm guest
sudo sed -i \
's/JAVA_ARGS="-Xms2g -Xmx2g/JAVA_ARGS="-Xms512m -Xmx512m/' \
/etc/default/puppetserver
}

# configure puppet agent
configure_puppet_agent() {
sudo bash -c "cat << EOF > /etc/puppetlabs/puppet/puppet.conf
server = $1
certname = $2
EOF"
}

main
16 changes: 16 additions & 0 deletions vagrant/puppet_server/hello_web_proj/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

PROJ_HOME=~/vagrant-puppetserver

# craete directory structure
mkdir -p \
$PROJ_HOME/site/{data,manifests,modules/hello_web/{files,manifests}}

cd $PROJ_HOME

# create files
touch \
Vagrantfile \
bootstrap.sh \
site/manifests/site.pp \
site/modules/hello_web/{manifests/init.pp,files/index.html,metadata.json}
7 changes: 7 additions & 0 deletions vagrant/puppet_server/hello_web_proj/site/manifests/site.pp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node "node01.local" {
class { 'hello_web': }
}

node "node02.local" {
class { 'hello_web': }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
<h1>Hello World!</h1>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class hello_web (
$package_name = 'apache2',
$service_name = 'apache2',
$doc_root = '/var/www/html'
) {

package { $package_name:
ensure => present,
}

service { $service_name:
ensure => running,
enable => true,
}

file { "$doc_root/index.html":
source => "puppet:///modules/hello_web/index.html",
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "joachim8675309-hello_web",
"version": "0.1.0",
"author": "joachim8675309",
"summary": "Hello World Tutorial",
"license": "Apache-2.0",
"source": "https://github.com/darkn3rd/blog_tutorials",
"dependencies": [],
"operatingsystem_support": [
{
"operatingsystem": "Ubuntu",
"operatingsystemrelease": ["22.04"]
}
],
"requirements": [
{
"name": "puppet",
"version_requirement": ">= 7.24 < 9.0.0"
}
]
}

0 comments on commit f54c12f

Please sign in to comment.