Skip to content

Instantly share code, notes, and snippets.

@martezr
Last active September 13, 2020 19:37
Show Gist options
  • Save martezr/69df2ce998ac31a803b68fb80c2f5d82 to your computer and use it in GitHub Desktop.
Save martezr/69df2ce998ac31a803b68fb80c2f5d82 to your computer and use it in GitHub Desktop.
Puppet Bolt Terraform Inventory Advanced Blog Post
bolt plan run todoapp::todoapp -m /Users/martez.reed/.puppetlabs/bolt-code/modules:bolt/site-modules -i bolt/inventory
.yaml web=web database=database
version: 2
groups:
- name: web
targets:
- _plugin: terraform
resource_type: vsphere_virtual_machine.todoappweb
uri: default_ip_address
dir: .
name: name
backend: remote
- name: database
targets:
- _plugin: terraform
resource_type: vsphere_virtual_machine.todoappdb
uri: default_ip_address
dir: .
name: name
backend: remote
terraform {
backend "consul" {
address = "10.0.0.6:8500"
scheme = "http"
path = "terraform/bolt/terraform.tfstate"
datacenter = "puppet-bolt"
}
}
provider "vsphere" {
user = "${var.vsphere_user}"
password = "${var.vsphere_password}"
vsphere_server = "${var.vsphere_server}"
# If you have a self-signed cert
allow_unverified_ssl = true
}
data "vsphere_datacenter" "dc" {
name = "GRT"
}
data "vsphere_datastore" "datastore" {
name = "Local_Storage"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}
data "vsphere_compute_cluster" "cluster" {
name = "GRT-Cluster"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}
data "vsphere_network" "network" {
name = "VM Network"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}
data "vsphere_virtual_machine" "template" {
name = "${var.template_name}"
datacenter_id = "${data.vsphere_datacenter.dc.id}"
}
resource "vsphere_virtual_machine" "todoappdb" {
name = "todoappdb"
num_cpus = 2
memory = 4096
resource_pool_id = "${data.vsphere_compute_cluster.cluster.resource_pool_id}"
datastore_id = "${data.vsphere_datastore.datastore.id}"
guest_id = "${data.vsphere_virtual_machine.template.guest_id}"
scsi_type = "${data.vsphere_virtual_machine.template.scsi_type}"
network_interface {
network_id = "${data.vsphere_network.network.id}"
adapter_type = "${data.vsphere_virtual_machine.template.network_interface_types[0]}"
}
disk {
label = "disk0"
size = "${data.vsphere_virtual_machine.template.disks.0.size}"
eagerly_scrub = "${data.vsphere_virtual_machine.template.disks.0.eagerly_scrub}"
thin_provisioned = "${data.vsphere_virtual_machine.template.disks.0.thin_provisioned}"
}
clone {
template_uuid = "${data.vsphere_virtual_machine.template.id}"
customize {
linux_options {
host_name = "todoappdb"
domain = "grt.local"
}
network_interface {}
dns_server_list = ["10.0.0.200"]
ipv4_gateway = "10.0.0.1"
}
}
}
resource "vsphere_virtual_machine" "todoappweb" {
name = "todoappweb"
num_cpus = 2
memory = 4096
resource_pool_id = "${data.vsphere_compute_cluster.cluster.resource_pool_id}"
datastore_id = "${data.vsphere_datastore.datastore.id}"
guest_id = "${data.vsphere_virtual_machine.template.guest_id}"
scsi_type = "${data.vsphere_virtual_machine.template.scsi_type}"
network_interface {
network_id = "${data.vsphere_network.network.id}"
adapter_type = "${data.vsphere_virtual_machine.template.network_interface_types[0]}"
}
disk {
label = "disk0"
size = "${data.vsphere_virtual_machine.template.disks.0.size}"
eagerly_scrub = "${data.vsphere_virtual_machine.template.disks.0.eagerly_scrub}"
thin_provisioned = "${data.vsphere_virtual_machine.template.disks.0.thin_provisioned}"
}
clone {
template_uuid = "${data.vsphere_virtual_machine.template.id}"
customize {
linux_options {
host_name = "todoappweb"
domain = "grt.local"
}
network_interface {}
dns_server_list = ["10.0.0.200"]
ipv4_gateway = "10.0.0.1"
}
}
}
plan todoapp::todoapp(
TargetSpec $database,
TargetSpec $web
) {
# Ensure puppet tools are installed and gather facts for the apply
apply_prep([$database, $web])
apply($database){
class {'mongodb::globals':
manage_package_repo => true,
version => '4.2.0',
bind_ip => ['0.0.0.0'],
user => 'mongod',
group => 'mongod',
server_package_name => 'mongodb-org-server',
}
-> class {'mongodb::server': }
}
run_plan(facts, nodes => $database)
$target = get_targets($database)[0]
$database_ip = $target.facts['networking']['ip']
apply($web){
package { 'git':
ensure => present,
}
class { 'nodejs': }
vcsrepo { '/opt/node-todo':
ensure => present,
provider => git,
source => 'https://github.com/scotch-io/node-todo.git',
trust_server_cert => true,
}
$databaseconfig = @("DBCONFIG"/L)
module.exports = {
remoteUrl: 'mongodb://${$database_ip}:27017/uw03mypu',
localUrl: 'mongodb://${$database_ip}:27017/meanstacktutorials'
};
| DBCONFIG
file { '/opt/node-todo/config/database.js':
ensure => file,
content => $databaseconfig,
}
package { 'pm2':
ensure => 'present',
provider => 'npm',
}
}
run_command('npm install /opt/node-todo', $web, '_catch_errors' => true)
run_command('pm2 start /opt/node-todo/server.js | exit 0', $web, '_catch_errors' => true)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment