Error: $cron_values is false, not a hash or array - vagrant

Vagrant installs and starts the Centos 6.6 VM up correctly, but fails to provision it with this error message:
Error: $cron_values is false, not a hash or array at /tmp/vagrant-puppet/manifests-893fcda21077f5a583c2dbbba8515c0a/nodes/Cron.pp:3 on node localhost.localdomain
Just before that this was displayed:
==> default: Running provisioner: puppet...
==> default: Running Puppet with site.pp...
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/composer/lib/facter/composer_home.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/concat/lib/facter/concat_basedir.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/firewall/lib/facter/ip6tables_version.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/firewall/lib/facter/iptables_persistent_version.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/firewall/lib/facter/iptables_version.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/git/lib/facter/git_exec_path.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/git/lib/facter/git_version.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/php/lib/facter/php_fact_extension_dir.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/php/lib/facter/php_fact_version.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/puppi/lib/facter/last_run.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/puppi/lib/facter/puppi_projects.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/puppi/lib/facter/windows_common_appdata.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/rabbitmq/lib/facter/rabbitmq_erlang_cookie.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/rvm/lib/facter/rvm_installed.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/rvm/lib/facter/rvm_version.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/staging/lib/facter/staging_http_get.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/staging/lib/facter/staging_windir.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/stdlib/lib/facter/facter_dot_d.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/stdlib/lib/facter/pe_version.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/stdlib/lib/facter/puppet_vardir.rb
==> default: Info: Loading facts in /tmp/vagrant-puppet/modules-419a158586c48436c8f5c2349fe39c11/stdlib/lib/facter/root_home.rb
Honestly, I'm not sure where to even look for the problem. I can SSH into the VM and that part seems to work correctly. Apache doesn't seem to be installed yet as the host does not resolve.
I'm also not sure if there is more information that could be of benefit? If so, I am more than happy to provide it. There does not seem to be very many people at all who have experienced this problem. Even if you don't know the solution, I'd be grateful if you could at least provide some direction on where to look for perhaps a more verbose log (I ran vagrant provision --debug but the error logs didn't make much more sense there), or something that could get me started?
I am running this machine with Parallels as the provider.
Any ideas?
Update:
The VagrantFile is generated by puppet (or really PuPHPet). Here is the config.yaml file:
vagrantfile-local:
vm:
box: puphpet/centos65-x64
box_url: puphpet/centos65-x64
hostname: ''
memory: '512'
cpus: '1'
chosen_provider: parallels
network:
private_network: 192.168.58.101
forwarded_port:
48IqH2aTQkz4:
host: '9321'
guest: '22'
post_up_message: ''
provider:
virtualbox:
modifyvm:
natdnshostresolver1: on
vmware:
numvcpus: 1
parallels:
cpus: 1
provision:
puppet:
manifests_path: puphpet/puppet
manifest_file: site.pp
module_path: puphpet/puppet/modules
options:
- '--verbose'
- '--hiera_config /vagrant/puphpet/puppet/hiera.yaml'
- '--parser future'
synced_folder:
jzVfnoa6cO3T:
owner: ''
group: ''
source: ./
target: /Users/myname/Sites/website/
sync_type: nfs
rsync:
auto: 'false'
usable_port_range:
start: 10200
stop: 10500
ssh:
host: null
port: null
private_key_path: null
username: vagrant
guest_port: null
keep_alive: true
forward_agent: false
forward_x11: false
shell: 'bash -l'
vagrant:
host: detect
server:
install: '1'
packages:
- ImageMagick
- vim-common
- php-soap
firewall:
install: '1'
rules: null
apache:
install: '1'
settings:
user: www-data
group: www-data
default_vhost: true
manage_user: false
manage_group: false
sendfile: 0
modules:
- rewrite
vhosts:
MvR5v9C4Vsuh:
servername: myserver.mag
serveraliases:
- www.awesome.dev
docroot: /Users/myname/Sites/sitename/base/
port: '80'
setenv:
- 'APP_ENV dev'
override:
- All
options:
- Indexes
- FollowSymLinks
- MultiViews
engine: ''
custom_fragment: ''
ssl_cert: ''
ssl_key: ''
ssl_chain: ''
ssl_certs_dir: ''
mod_pagespeed: 0
php:
install: '1'
version: '55'
composer: '1'
composer_home: ''
modules:
php:
- cli
- intl
- mcrypt
pear: { }
pecl:
- pecl_http
- imagick
ini:
display_errors: On
error_reporting: '-1'
session.save_path: /var/lib/php/session
timezone: America/Chicago
mod_php: 0
xdebug:
install: '1'
settings:
xdebug.default_enable: '1'
xdebug.remote_autostart: '0'
xdebug.remote_connect_back: '1'
xdebug.remote_enable: '1'
xdebug.remote_handler: dbgp
xdebug.remote_port: '9000'
ruby:
install: '1'
versions: { }
nodejs:
install: '0'
npm_packages: { }
python:
install: '1'
packages: { }
versions: { }
mysql:
install: '1'
override_options: { }
root_password: root
adminer: 0
databases:
ouWYyTlD0olg:
grant:
- ALL
name: db_name
host: localhost
user: user_name
password: password
sql_file: ../databases/output.sql
redis:
install: '1'
settings:
conf_port: '6379'
VagrantFile:
require 'yaml'
dir = File.dirname(File.expand_path(__FILE__))
configValues = YAML.load_file("#{dir}/puphpet/config.yaml")
data = configValues['vagrantfile-local']
Vagrant.require_version '>= 1.6.0'
Vagrant.configure('2') do |config|
config.vm.box = "#{data['vm']['box']}"
config.vm.box_url = "#{data['vm']['box_url']}"
if data['vm']['hostname'].to_s.strip.length != 0
config.vm.hostname = "#{data['vm']['hostname']}"
end
if data['vm']['network']['private_network'].to_s != ''
config.vm.network 'private_network', ip: "#{data['vm']['network']['private_network']}"
end
data['vm']['network']['forwarded_port'].each do |i, port|
if port['guest'] != '' && port['host'] != ''
config.vm.network :forwarded_port, guest: port['guest'].to_i, host: port['host'].to_i
end
end
if !data['vm']['post_up_message'].nil?
config.vm.post_up_message = "#{data['vm']['post_up_message']}"
end
if Vagrant.has_plugin?('vagrant-hostmanager')
hosts = Array.new()
if !configValues['apache']['install'].nil? &&
configValues['apache']['install'].to_i == 1 &&
configValues['apache']['vhosts'].is_a?(Hash)
configValues['apache']['vhosts'].each do |i, vhost|
hosts.push(vhost['servername'])
if vhost['serveraliases'].is_a?(Array)
vhost['serveraliases'].each do |vhost_alias|
hosts.push(vhost_alias)
end
end
end
elsif !configValues['nginx']['install'].nil? &&
configValues['nginx']['install'].to_i == 1 &&
configValues['nginx']['vhosts'].is_a?(Hash)
configValues['nginx']['vhosts'].each do |i, vhost|
hosts.push(vhost['server_name'])
if vhost['server_aliases'].is_a?(Array)
vhost['server_aliases'].each do |x, vhost_alias|
hosts.push(vhost_alias)
end
end
end
end
if hosts.any?
if config.vm.hostname.to_s.strip.length == 0
config.vm.hostname = 'puphpet-dev-machine'
end
config.hostmanager.enabled = true
config.hostmanager.manage_host = true
config.hostmanager.ignore_private_ip = false
config.hostmanager.include_offline = false
config.hostmanager.aliases = hosts
end
end
if Vagrant.has_plugin?('vagrant-cachier')
config.cache.scope = :box
end
data['vm']['synced_folder'].each do |i, folder|
if folder['source'] != '' && folder['target'] != ''
sync_owner = !folder['sync_owner'].nil? ? folder['sync_owner'] : 'www-data'
sync_group = !folder['sync_group'].nil? ? folder['sync_group'] : 'www-data'
if folder['sync_type'] == 'nfs'
config.vm.synced_folder "#{folder['source']}", "#{folder['target']}", id: "#{i}", type: 'nfs'
if Vagrant.has_plugin?('vagrant-bindfs')
config.bindfs.bind_folder "#{folder['target']}", "/mnt/vagrant-#{i}"
end
elsif folder['sync_type'] == 'smb'
config.vm.synced_folder "#{folder['source']}", "#{folder['target']}", id: "#{i}", type: 'smb'
elsif folder['sync_type'] == 'rsync'
rsync_args = !folder['rsync']['args'].nil? ? folder['rsync']['args'] : ['--verbose', '--archive', '-z']
rsync_auto = !folder['rsync']['auto'].nil? ? folder['rsync']['auto'] : true
rsync_exclude = !folder['rsync']['exclude'].nil? ? folder['rsync']['exclude'] : ['.vagrant/']
config.vm.synced_folder "#{folder['source']}", "#{folder['target']}", id: "#{i}",
rsync__args: rsync_args, rsync__exclude: rsync_exclude, rsync__auto: rsync_auto, type: 'rsync', group: sync_group, owner: sync_owner
elsif data['vm']['chosen_provider'] == 'parallels'
config.vm.synced_folder "#{folder['source']}", "#{folder['target']}", id: "#{i}",
group: sync_group, owner: sync_owner, mount_options: ['share']
else
config.vm.synced_folder "#{folder['source']}", "#{folder['target']}", id: "#{i}",
group: sync_group, owner: sync_owner, mount_options: ['dmode=775', 'fmode=764']
end
end
end
config.vm.usable_port_range = (data['vm']['usable_port_range']['start'].to_i..data['vm']['usable_port_range']['stop'].to_i)
if data['vm']['chosen_provider'].empty? || data['vm']['chosen_provider'] == 'virtualbox'
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'virtualbox'
config.vm.provider :virtualbox do |virtualbox|
data['vm']['provider']['virtualbox']['modifyvm'].each do |key, value|
if key == 'memory'
next
end
if key == 'cpus'
next
end
if key == 'natdnshostresolver1'
value = value ? 'on' : 'off'
end
virtualbox.customize ['modifyvm', :id, "--#{key}", "#{value}"]
end
virtualbox.customize ['modifyvm', :id, '--memory', "#{data['vm']['memory']}"]
virtualbox.customize ['modifyvm', :id, '--cpus', "#{data['vm']['cpus']}"]
if data['vm']['provider']['virtualbox']['modifyvm']['name'].nil? ||
data['vm']['provider']['virtualbox']['modifyvm']['name'].empty?
if data['vm']['hostname'].to_s.strip.length != 0
virtualbox.customize ['modifyvm', :id, '--name', config.vm.hostname]
end
end
end
end
if data['vm']['chosen_provider'] == 'vmware_fusion' || data['vm']['chosen_provider'] == 'vmware_workstation'
ENV['VAGRANT_DEFAULT_PROVIDER'] = (data['vm']['chosen_provider'] == 'vmware_fusion') ? 'vmware_fusion' : 'vmware_workstation'
config.vm.provider :vmware_fusion do |v, override|
data['vm']['provider']['vmware'].each do |key, value|
if key == 'memsize'
next
end
if key == 'cpus'
next
end
v.vmx["#{key}"] = "#{value}"
end
v.vmx['memsize'] = "#{data['vm']['memory']}"
v.vmx['numvcpus'] = "#{data['vm']['cpus']}"
if data['vm']['provider']['vmware']['displayName'].nil? ||
data['vm']['provider']['vmware']['displayName'].empty?
if data['vm']['hostname'].to_s.strip.length != 0
v.vmx['displayName'] = config.vm.hostname
end
end
end
end
if data['vm']['chosen_provider'] == 'parallels'
ENV['VAGRANT_DEFAULT_PROVIDER'] = 'parallels'
config.vm.provider 'parallels' do |v|
data['vm']['provider']['parallels'].each do |key, value|
if key == 'memsize'
next
end
if key == 'cpus'
next
end
v.customize ['set', :id, "--#{key}", "#{value}"]
end
v.memory = "#{data['vm']['memory']}"
v.cpus = "#{data['vm']['cpus']}"
if data['vm']['provider']['parallels']['name'].nil? ||
data['vm']['provider']['parallels']['name'].empty?
if data['vm']['hostname'].to_s.strip.length != 0
v.name = config.vm.hostname
end
end
end
end
ssh_username = !data['ssh']['username'].nil? ? data['ssh']['username'] : 'vagrant'
config.vm.provision 'shell' do |s|
s.path = 'puphpet/shell/initial-setup.sh'
s.args = '/vagrant/puphpet'
end
config.vm.provision 'shell' do |kg|
kg.path = 'puphpet/shell/ssh-keygen.sh'
kg.args = "#{ssh_username}"
end
config.vm.provision :shell, :path => 'puphpet/shell/install-ruby.sh'
config.vm.provision :shell, :path => 'puphpet/shell/install-puppet.sh'
config.vm.provision :puppet do |puppet|
puppet.facter = {
'ssh_username' => "#{ssh_username}",
'provisioner_type' => ENV['VAGRANT_DEFAULT_PROVIDER'],
'vm_target_key' => 'vagrantfile-local',
}
puppet.manifests_path = "#{data['vm']['provision']['puppet']['manifests_path']}"
puppet.manifest_file = "#{data['vm']['provision']['puppet']['manifest_file']}"
puppet.module_path = "#{data['vm']['provision']['puppet']['module_path']}"
if !data['vm']['provision']['puppet']['options'].empty?
puppet.options = data['vm']['provision']['puppet']['options']
end
end
config.vm.provision :shell do |s|
s.path = 'puphpet/shell/execute-files.sh'
s.args = ['exec-once', 'exec-always']
end
config.vm.provision :shell, run: 'always' do |s|
s.path = 'puphpet/shell/execute-files.sh'
s.args = ['startup-once', 'startup-always']
end
config.vm.provision :shell, :path => 'puphpet/shell/important-notices.sh'
customKey = "#{dir}/files/dot/ssh/id_rsa"
vagrantKey = "#{dir}/.vagrant/machines/default/#{ENV['VAGRANT_DEFAULT_PROVIDER']}/private_key"
if File.file?(customKey)
config.ssh.private_key_path = [
customKey,
"#{ENV['HOME']}/.vagrant.d/insecure_private_key"
]
if File.file?(vagrantKey) and ! FileUtils.compare_file(customKey, vagrantKey)
File.delete(vagrantKey)
end
if ! File.directory?(File.dirname(vagrantKey))
FileUtils.mkdir_p(File.dirname(vagrantKey))
end
if ! File.file?(vagrantKey)
FileUtils.cp(customKey, vagrantKey)
end
end
if !data['ssh']['host'].nil?
config.ssh.host = "#{data['ssh']['host']}"
end
if !data['ssh']['port'].nil?
config.ssh.port = "#{data['ssh']['port']}"
end
if !data['ssh']['username'].nil?
config.ssh.username = "#{data['ssh']['username']}"
end
if !data['ssh']['guest_port'].nil?
config.ssh.guest_port = data['ssh']['guest_port']
end
if !data['ssh']['shell'].nil?
config.ssh.shell = "#{data['ssh']['shell']}"
end
if !data['ssh']['keep_alive'].nil?
config.ssh.keep_alive = data['ssh']['keep_alive']
end
if !data['ssh']['forward_agent'].nil?
config.ssh.forward_agent = data['ssh']['forward_agent']
end
if !data['ssh']['forward_x11'].nil?
config.ssh.forward_x11 = data['ssh']['forward_x11']
end
if !data['vagrant']['host'].nil?
config.vagrant.host = data['vagrant']['host'].gsub(':', '').intern
end
end

I recently came across this problem myself and here is how I fixed it. When you create a Puphpet config file, and do not specify any cron jobs, this error is created.
To fix it make your cron key in config.yaml look like one of these:
Option #1 (for kicking off the Magento cron job):
cron:
install: '1'
jobs:
cj_lfgowfjsb51y:
name: magento
user: ''
command: '/bin/sh /var/www/sites/cron.sh'
minute: '*/5'
hour: '*'
weekday: '*'
month: '*'
monthday: ''
Option #2
cron:
install:'1'
jobs: []

Related

Provisioning a vagrantfile using json

Hey so i want to provision my vagrantfile using json, i have done this with yaml before and tried to follow the syntax alongside a little research on json. I have included print commands to confirm that the values from my json are readable but im struggling in understanding how to put them into my code as values? so my question is how do i call these values within a vagrant file?
require 'json'
filej = File.read('info.json')
jinfo = JSON.parse(filej)
vagrant.configure("2") do |config|
jinfo.each do |key, value|
p key
p value
config.vm.define jinfo['name'] do |js|
js.vm.box = jinfo['box']
js.vm.network 'private_network', ip: js['ip_addr']
js.vm.provider "virtualbox" do |vb|
vb.cpus = jinfo['vcpu']
vb.memory = jinfo['ram']
end
end
end
end
and my json is as follows
[
{
"name": "jenkins",
"box": "centos/7",
"ram": 4096,
"vcpu": 2,
"ip_addr": "192.168.33.11"
},
{
"name": "server",
"box": "centos/7",
"ram": 4096,
"vcpu": 2,
"ip_addr": "192.168.33.10"
}
]
If your Vagrantfile is same as pasted above, it looks like this:
require 'json'
filej = File.read('info.json')
jinfo = JSON.parse(filej)
vagrant.configure("2") do |config|
jinfo.each do |key, value|
p key
p value
config.vm.define jinfo['name'] do |js|
js.vm.box = jinfo['box']
js.vm.network 'private_network', ip: js['ip_addr']
js.vm.provider "virtualbox" do |vb|
vb.cpus = jinfo['vcpu']
vb.memory = jinfo['ram']
end
end
end
end
jinfo is actually an array and not a hash. You are trying to loop through each key value pair of the json instead of looping through each value. Try this:
require 'json'
filej = File.read('info.json')
jinfos = JSON.parse(filej)
vagrant.configure("2") do |config|
jinfos.each do |jinfo|
p jinfo
config.vm.define jinfo['name'] do |js|
js.vm.box = jinfo['box']
js.vm.network 'private_network', ip: js['ip_addr']
js.vm.provider "virtualbox" do |vb|
vb.cpus = jinfo['vcpu']
vb.memory = jinfo['ram']
end
end
end
end
I changed the variable names slightly. You can remove the print part altogether and try.
Hope this helped.

vagrant set ip address in loop with maths

I am trying to write a script to set up a dynamic number of nodes in vagrant.
so it runs as
# -*- mode: ruby -*-
# vi: set ft=ruby :
Vagrant.configure("2") do |config|
$num_instances = 3
(1..$num_instances).each do |i|
$ip = "10.1.10.#{i}+80"
config.vm.define "node#{i}" do |node|
node.vm.box = "generic/ubuntu1604"
node.vm.provider :libvirt do |domain|
domain.memory = 1024
domain.cpus = 1
domain.nic_model_type = "virtio"
domain.kvm_hidden = true
end
node.vm.hostname = "t2-node#{i}"
node.vm.network "public_network",
:ip => $ip,
:type => "bridge" ,
:dev => "br1",
:mode => "bridge",
:use_dhcp_assigned_default_route => true
end
end
end
but I get an error:
Unknown interface eth1
Error: ??? prefix is expected rather than "10.1.10.3+80/255.255.255.0".
so I guess the addition for the $1p variable isn't being done properly?
any ideas?
This is more a ruby question than a vagrant question but you do your string interpolation wrong so the statement
$ip = "10.1.10.#{i}+80"
returns a string that is 10.1.10.3+80 and of course this is not a valid IP.
what you're looking at is
ip = "10.1.10.#{i+80}"
which will make the right math.
You can easily check in irb on your command line
irb(main):001:0> num_instances = 3
=> 3
irb(main):002:0> (1..num_instances).each do |i|
irb(main):003:1* ip = "10.1.10.#{i+80}"
irb(main):004:1> p ip
irb(main):005:1> end
"10.1.10.81"
"10.1.10.82"
"10.1.10.83"
=> 1..3
PS: Note, you dont need all the $ when you declare variables in ruby.

Vagrant execute command on specific guest after other guests are up

I have the following Vagrantfile:
Vagrant.configure(VAGRANT_API_VERSION) do |config|
config.vm.provider "virtualbox" do |vb|
vb.memory = 1024
vb.cpus = 2
end
config.vm.define :master do |master_config|
master_config.vm.box = "centos/7"
master_config.vm.host_name = 'saltmaster.local'
master_config.vm.network "private_network", ip: "172.16.10.10"
master_config.vm.synced_folder ".", "/vagrant", disabled: true
master_config.vm.synced_folder "states", "/vagrant/states", type: "virtualbox"
master_config.vm.synced_folder "pillar", "/vagrant/pillar", type: "virtualbox"
master_config.vm.provision :salt do |salt|
salt.master_config = "etc/master"
salt.install_type = "git"
salt.install_args = "v2016.11.7"
salt.no_minion = true
salt.install_master = true
salt.verbose = true
salt.colorize = true
salt.bootstrap_options = "-P -c /tmp"
end
end
config.vm.define :minion1 do |minion_config|
minion_config.vm.box = "centos/7"
minion_config.vm.host_name = 'saltminion1.local'
minion_config.vm.network "private_network", ip: "172.16.10.11"
minion_config.vm.synced_folder ".", "/vagrant", disabled: true
minion_config.vm.provision :salt do |salt|
salt.minion_config = "etc/minion1"
salt.install_type = "git"
salt.install_args = "v2016.11.7"
salt.verbose = true
salt.colorize = true
salt.bootstrap_options = "-P -c /tmp"
end
end
config.vm.define :minion2 do |minion_config|
minion_config.vm.box = "centos/7"
minion_config.vm.host_name = 'saltminion2.local'
minion_config.vm.network "private_network", ip: "172.16.10.12"
minion_config.vm.synced_folder ".", "/vagrant", disabled: true
minion_config.vm.provision :salt do |salt|
salt.minion_config = "etc/minion2"
salt.install_type = "git"
salt.install_args = "v2016.11.7"
salt.verbose = true
salt.colorize = true
salt.bootstrap_options = "-P -c /tmp"
end
end
end
Now after all the machines are up and running, I want to execute a command on the salt master using master_config.vm.provision "shell", inline: "salt '*' state.apply".
But the problem is, once Vagrant finished provisioning minion2, it can't access the master_config machine anymore. I think there should be a way to execute that command, without having to execute vagrant ssh master -c stalt '*' state.apply. I don't want to use a command on the host. And the highstate needs to be applied AFTER all the minions are up, due to some networking related configurations and states.
Can someone help me out?
... continued from comments, making here for the example.
You can use the vagrant trigger plugin. As mentioned in the doc:
Starting from version 0.5.0, triggers can also be run as a provisioner
you can target a specific instance (see https://github.com/emyl/vagrant-triggers#options)
so the following at the end of the file should work:
config.vm.provision "trigger", :vm => "minion_config" do |trigger|
trigger.fire do
run "script"
end
end

Multi-VM Vagrantfile with shared chef settings but different roles

I'm trying to use a single Vagrantfile to define 2 VMs with different Chef roles, and I'd like to share certain chef configuration betweeen both vms, but specify the runlist, environment, and attributes seperately. Is there a way I can do this without duplicating the common settings?
For instance, if I did something like:
# Development system
config.vm.define "dev", primary: true do |dev|
dev.vm.hostname = "server1"
dev.vm.provider "virtualbox" do |v|
v.name = "server1"
end
dev.vm.provision "chef_client" do |chef|
chef.run_list = [ "role[dev-system]" ]
chef.environment = "development"
chef.json = {
"key" => "value1"
}
end
end
# Production system
config.vm.define "pro", primary: true do |pro|
pro.vm.hostname = "server2"
pro.vm.provider "virtualbox" do |v|
v.name = "server2"
end
pro.vm.provision "chef_client" do |chef|
chef.run_list = [ "role[pro-system]" ]
chef.environment = "production"
chef.json = {
"key" => "value2"
}
end
end
config.vm.provision "chef_client" do |chef|
chef.chef_server_url = "https://example.com/organizations/tsrd"
chef.validation_key_path = "~/my-validator.pem"
chef.validation_client_name = "my-validator"
chef.delete_node = true
chef.delete_client = true
# ... PLUS OTHER COMMON CONFIGURATIONS...
end
Would it apply the individual chef sections for each vm along with the shared section, or just override the first sections with the last? If not, is there an easier way to do this? Perhaps a wrapper function that is called inside each vm definition block?
For this purpose I took another way.
I define my vms as a hash in the vagrant file like this:
machinesBases = {
'srv0643' => {
'ip_address' => '172.30.0.140',
'env' => 'Z1',
'alias' => 'z1frnessus01',
'roles' => ['nessus'],
'additionnal_recipes' => ['java']
},
'srv0683' => {
'ip_address' => '172.30.0.51',
'env' => 'P1',
'alias' => 'p1frkegin01',
'roles' => [],
'additionnal_recipes' => ['kegin_cookbook']
}
}
And then I loop over it, using the hash values
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
machinesBases.each do |hostname,properties|
config.vm.define hostname do |box|
box.vm.hostname = "#{hostname}.my.domain.org"
box.vm.network :private_network, ip: properties["ip_address"]
box.vm.provision :chef_client do |chef|
chef.chef_server_url = CHEF_CHEF_SERVER_URL
chef.validation_key_path = CHEF_VALIDATION_KEY_PATH
chef.environment = properties["env"]
chef.add_recipe "base-linux"
properties['roles'].each do |role|
chef.add_role role
end
properties['additionnal_recipes'].each do |recipe|
chef.add_recipe recipe
end
end
end
end unless machinesBases.length == 0
end
I stripped some parts of the vagrant file (constant definition for exemple) to avoid being too long here, but I think you got the idea.
This is perhaps not the best way, but I found it quite easy to extend/update.
I believe you could just add the two machines to an array and then loop over the array and add the common elements within the loop.
a = config.vm.define "dev", primary: true do |dev|
...
end
b = config.vm.define "pro", primary: true do |pro|
...
end
[a, b].each do |next_vm|
next_vm.provision "chef_client" do |chef|
chef.chef_server_url = "https://example.com/organizations/tsrd"
chef.validation_key_path = "~/my-validator.pem"
chef.validation_client_name = "my-validator"
chef.delete_node = true
chef.delete_client = true
# ... PLUS OTHER COMMON CONFIGURATIONS...
end
end

Ruby hash of hash

I need to have a hash to collect results, for example:
results = Hash.new()
results['127.0.0.1'] = Hash.new()
results['127.0.0.2'] = Hash.new()
results['127.0.0.1']['port'] = '80'
results['127.0.0.2']['port'] = '80'
results['127.0.0.1']['ver'] = 'abc'
results['127.0.0.1']['ver'] = 'def'
It seem works fine, but now can I show results? :)
I would like have:
ip: 127.0.0.1
port: 80
ver: abc
ip: 127.0.0.2
port: 80
ver: def
Thank you very much!
results.each do |k, v|
puts "IP: #{k}, Port: #{v['port']}, Ver: #{v['ver']}"
end
Better way to define hash:
result = {
'127.0.0.1' => {
port: 80,
ver: 'abc'
},
'127.0.0.2' => {
port: 80,
ver: 'def'
}
}
and then:
result.each do |key, value|
puts "ip: #{key}"
value.each { |k,v| puts "\t#{key}: #{value}" }
end
This method will work also if you add some extra options to hash.
But if you want it only for debugging read about awesome_print.

Resources