vagrant-guest_ansible 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: e79cdd8250c933184102b00d37202b3a19c550f2
4
+ data.tar.gz: fdb8611d8fa378890a87360fece1149e5cb493e8
5
+ SHA512:
6
+ metadata.gz: cf2af14c8fac3d6e9f63839776407f6dc80453991c77b3f10c36e62cb5a2fd9d603ef3c64c503ceb451249109083c6e32d9768ad6599b7d47ed5be9d634a20c1
7
+ data.tar.gz: 8fe3cb9abeeb92fba13fa7b8c5ffb3f83b71d88a8782c3fa768a40fde03211eabf2b54a0c653a82631c004e0735bd71e2e51652704b80ec96d6df78743dc5870
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ pkg
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in vagrant-guest_ansible.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Roberto Quintanilla
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,79 @@
1
+ # vagrant-guest_ansible Provisioner
2
+
3
+ Run ansible provisioning from Vagrant inside the guest machine.
4
+
5
+ This provider is a mix between the original Ansible provisioner bundled with
6
+ Vagrant, and the Shell provisioner which is also bundled in Vagrant.
7
+
8
+ The Ansible hosts file is generated in the shared folder (like the Ansible plugin) if no
9
+ hosts inventory file is given.
10
+
11
+ It uses a modified shell script file (see credits) that's uploaded to
12
+ the guest machine, installs any dependencies (Git, ansible, etc) and then
13
+ runs the ansible provisioning scripts locally (at the guest machine).
14
+
15
+ ## Installation
16
+
17
+ Use the Vagrant plugin installer:
18
+
19
+ ```bash
20
+ $ vagrant plugin install vagrant-guest_ansible
21
+ ```
22
+
23
+ ## Usage
24
+
25
+ In the Vagrantfile:
26
+
27
+ ```ruby
28
+ # -*- mode: ruby -*-
29
+ # vi: set ft=ruby :
30
+
31
+ # Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
32
+ VAGRANTFILE_API_VERSION = "2"
33
+
34
+ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
35
+ config.vm.provision :guest_ansible do |guest_ansible|
36
+ guest_ansible.playbook = "any_playbook.yml"
37
+ guest_ansible.extra_vars = extra_vars
38
+ guest_ansible.sudo = false
39
+ end
40
+ end
41
+ ```
42
+
43
+ This provisioner is actually more useful in Windows hosts, where ansible is not supported nor available.
44
+ A typical Vagrantfile that works in windows may look like this:
45
+
46
+ ```ruby
47
+ # -*- mode: ruby -*-
48
+ # vi: set ft=ruby :
49
+
50
+ # Vagrantfile API/syntax version. Don't touch unless you know what you're doing!
51
+ VAGRANTFILE_API_VERSION = "2"
52
+
53
+ Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
54
+ if Vagrant::Util::Platform.windows?
55
+ config.vm.provision :guest_ansible do |ansible|
56
+ ansible.playbook = "any_playbook.yml"
57
+ end
58
+ else
59
+ config.vm.provision :ansible do |ansible|
60
+ ansible.playbook = "any_playbook.yml"
61
+ end
62
+ end
63
+ end
64
+ ```
65
+
66
+ ## Credits
67
+
68
+ The shell script that is run in the guest machine was based on:
69
+
70
+ - https://github.com/KSid/windows-vagrant-ansible
71
+ - https://github.com/geerlingguy/JJG-Ansible-Windows
72
+
73
+ ## Contributing
74
+
75
+ 1. Fork it ( http://github.com/vovimayhem/vagrant-guest_ansible/fork )
76
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
77
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
78
+ 4. Push to the branch (`git push origin my-new-feature`)
79
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,2 @@
1
+ require "vagrant-guest_ansible/version"
2
+ require "vagrant-guest_ansible/plugin"
@@ -0,0 +1,127 @@
1
+
2
+
3
+ module VagrantPlugins
4
+ module GuestAnsible
5
+ class Config < Vagrant.plugin("2", :config)
6
+ attr_accessor :playbook
7
+ attr_accessor :extra_vars
8
+ attr_accessor :inventory_path
9
+ attr_accessor :ask_sudo_pass
10
+ attr_accessor :limit
11
+ attr_accessor :sudo
12
+ attr_accessor :sudo_user
13
+ attr_accessor :verbose
14
+ attr_accessor :tags
15
+ attr_accessor :skip_tags
16
+ attr_accessor :start_at_task
17
+ attr_accessor :groups
18
+ attr_accessor :host_key_checking
19
+
20
+ # Joker attribute, used to pass unsupported arguments to ansible anyway
21
+ attr_accessor :raw_arguments
22
+
23
+ def initialize
24
+ @playbook = UNSET_VALUE
25
+ @extra_vars = UNSET_VALUE
26
+ @inventory_path = UNSET_VALUE
27
+ @ask_sudo_pass = UNSET_VALUE
28
+ @limit = UNSET_VALUE
29
+ @sudo = UNSET_VALUE
30
+ @sudo_user = UNSET_VALUE
31
+ @verbose = UNSET_VALUE
32
+ @tags = UNSET_VALUE
33
+ @skip_tags = UNSET_VALUE
34
+ @start_at_task = UNSET_VALUE
35
+ @raw_arguments = UNSET_VALUE
36
+ @groups = UNSET_VALUE
37
+ @host_key_checking = "true"
38
+ end
39
+
40
+ def path
41
+ @path ||= File.expand_path("../guest_script.sh", __FILE__)
42
+ end
43
+
44
+ def upload_path
45
+ "/tmp/vagrant-shell"
46
+ end
47
+
48
+ def remote?
49
+ path =~ URI.regexp(["ftp", "http", "https"])
50
+ end
51
+
52
+ def binary
53
+ false
54
+ end
55
+
56
+ def finalize!
57
+ @playbook = nil if @playbook == UNSET_VALUE
58
+ @extra_vars = nil if @extra_vars == UNSET_VALUE
59
+ @inventory_path = nil if @inventory_path == UNSET_VALUE
60
+ @ask_sudo_pass = nil if @ask_sudo_pass == UNSET_VALUE
61
+ @limit = nil if @limit == UNSET_VALUE
62
+ @sudo = nil if @sudo == UNSET_VALUE
63
+ @sudo_user = nil if @sudo_user == UNSET_VALUE
64
+ @verbose = nil if @verbose == UNSET_VALUE
65
+ @tags = nil if @tags == UNSET_VALUE
66
+ @skip_tags = nil if @skip_tags == UNSET_VALUE
67
+ @start_at_task = nil if @start_at_task == UNSET_VALUE
68
+ @raw_arguments = nil if @raw_arguments == UNSET_VALUE
69
+ @groups = {} if @groups == UNSET_VALUE
70
+ @host_key_checking = nil if @host_key_checking == UNSET_VALUE
71
+ end
72
+
73
+ def validate(machine)
74
+ errors = _detected_errors
75
+
76
+ # Validate that a playbook path was provided
77
+ if !playbook
78
+ errors << I18n.t("vagrant.provisioners.ansible.no_playbook")
79
+ end
80
+
81
+ # Validate the existence of said playbook on the host
82
+ if playbook
83
+ expanded_path = Pathname.new(playbook).expand_path(machine.env.root_path)
84
+ if !expanded_path.file?
85
+ errors << I18n.t("vagrant.provisioners.ansible.playbook_path_invalid",
86
+ :path => expanded_path)
87
+ end
88
+ end
89
+
90
+ # Validate that extra_vars is either a hash, or a path to an
91
+ # existing file
92
+ if extra_vars
93
+ extra_vars_is_valid = extra_vars.kind_of?(Hash) || extra_vars.kind_of?(String)
94
+ if extra_vars.kind_of?(String)
95
+ # Accept the usage of '@' prefix in Vagrantfile (e.g. '@vars.yml'
96
+ # and 'vars.yml' are both supported)
97
+ match_data = /^@?(.+)$/.match(extra_vars)
98
+ extra_vars_path = match_data[1].to_s
99
+ expanded_path = Pathname.new(extra_vars_path).expand_path(machine.env.root_path)
100
+ extra_vars_is_valid = expanded_path.exist?
101
+ if extra_vars_is_valid
102
+ @extra_vars = '@' + extra_vars_path
103
+ end
104
+ end
105
+
106
+ if !extra_vars_is_valid
107
+ errors << I18n.t("vagrant.provisioners.ansible.extra_vars_invalid",
108
+ :type => extra_vars.class.to_s,
109
+ :value => extra_vars.to_s
110
+ )
111
+ end
112
+ end
113
+
114
+ # Validate the existence of the inventory_path, if specified
115
+ if inventory_path
116
+ expanded_path = Pathname.new(inventory_path).expand_path(machine.env.root_path)
117
+ if !expanded_path.exist?
118
+ errors << I18n.t("vagrant.provisioners.ansible.inventory_path_invalid",
119
+ :path => expanded_path)
120
+ end
121
+ end
122
+
123
+ { "ansible provisioner" => errors }
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,40 @@
1
+ #!/bin/bash
2
+
3
+ ANSIBLE_DIR=$1
4
+ ANSIBLE_PLAYBOOK=$2
5
+ ANSIBLE_HOSTS=$3
6
+ ANSIBLE_EXTRA_VARS=$4
7
+ TEMP_HOSTS="/tmp/ansible_hosts"
8
+
9
+ if [ ! -f /vagrant/$ANSIBLE_PLAYBOOK ]; then
10
+ echo "ERROR: Cannot find the given Ansible playbook."
11
+ exit 1
12
+ fi
13
+
14
+ if [ ! -f /vagrant/$ANSIBLE_HOSTS ]; then
15
+ echo "ERROR: Cannot find the given Ansible hosts file."
16
+ exit 2
17
+ fi
18
+
19
+ if [ ! -d $ANSIBLE_DIR ]; then
20
+ echo -n "Updating apt cache..."
21
+ sudo apt-get update -qq
22
+ echo " DONE!"
23
+
24
+ echo -n "Installing Ansible dependencies and Git..."
25
+ sudo apt-get install -y -qq git python-yaml python-paramiko python-jinja2
26
+ echo " DONE!"
27
+
28
+ # Clone ansible:
29
+ sudo git clone git://github.com/ansible/ansible.git ${ANSIBLE_DIR}
30
+ fi
31
+
32
+ if [ ! -z "$ANSIBLE_EXTRA_VARS" -a "$ANSIBLE_EXTRA_VARS" != " " ]; then
33
+ ANSIBLE_EXTRA_VARS=" --extra-vars \"$ANSIBLE_EXTRA_VARS\""
34
+ fi
35
+
36
+ cd ${ANSIBLE_DIR}
37
+ cp /vagrant/${ANSIBLE_HOSTS} ${TEMP_HOSTS} && chmod -x ${TEMP_HOSTS}
38
+ echo "Running Ansible as $USER:"
39
+ bash -c "source hacking/env-setup && ansible-playbook /vagrant/${ANSIBLE_PLAYBOOK} --inventory-file=${TEMP_HOSTS} --connection=local $ANSIBLE_EXTRA_VARS"
40
+ rm ${TEMP_HOSTS}
@@ -0,0 +1,24 @@
1
+ require "vagrant"
2
+
3
+ module VagrantPlugins
4
+ module GuestAnsible
5
+ class Plugin < Vagrant.plugin("2")
6
+ name "vagrant-guest_ansible"
7
+ description <<-DESC
8
+ Provides support for provisioning your virtual machines with
9
+ Ansible playbooks on host environments without ansible (Windows).
10
+ DESC
11
+
12
+ config(:guest_ansible, :provisioner) do
13
+ require File.expand_path("../config", __FILE__)
14
+ Config
15
+ end
16
+
17
+ provisioner(:guest_ansible) do
18
+ require File.expand_path("../provisioner", __FILE__)
19
+ Provisioner
20
+ end
21
+ end
22
+
23
+ end
24
+ end
@@ -0,0 +1,148 @@
1
+
2
+
3
+ module VagrantPlugins
4
+ module GuestAnsible
5
+ class Provisioner < Vagrant.plugin("2", :provisioner)
6
+
7
+ def initialize(machine, config)
8
+ super
9
+ end
10
+
11
+ def provision
12
+
13
+ args = [
14
+ './ansible',
15
+ config.playbook,
16
+ File.basename(self.setup_inventory_file),
17
+ (config.extra_vars.blank? ? "''" : config.extra_vars.map { |k,v| "#{k}=#{v}" }.join(" "))
18
+ ].join(' ')
19
+
20
+ command = "chmod +x #{config.upload_path} && #{config.upload_path} #{args}"
21
+
22
+ with_script_file do |path|
23
+
24
+ # Upload the script to the machine
25
+ @machine.communicate.tap do |comm|
26
+ # Reset upload path permissions for the current ssh user
27
+ user = @machine.ssh_info[:username]
28
+ comm.sudo("chown -R #{user} #{config.upload_path}",
29
+ :error_check => false)
30
+
31
+ comm.upload(path.to_s, config.upload_path)
32
+
33
+ @machine.ui.info(I18n.t("vagrant.provisioners.shell.running",
34
+ script: path.to_s))
35
+
36
+ # Execute it with sudo
37
+ comm.execute(command, sudo: config.sudo) do |type, data|
38
+ if [:stderr, :stdout].include?(type)
39
+ # Output the data with the proper color based on the stream.
40
+ color = type == :stdout ? :green : :red
41
+
42
+ options = {
43
+ new_line: false,
44
+ prefix: false,
45
+ }
46
+ options[:color] = color if !config.keep_color
47
+
48
+ @machine.env.ui.info(data, options)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ protected
56
+
57
+ # This method yields the path to a script to upload and execute
58
+ # on the remote server. This method will properly clean up the
59
+ # script file if needed.
60
+ def with_script_file
61
+ script = nil
62
+
63
+ if config.remote?
64
+ download_path = @machine.env.tmp_path.join("#{@machine.id}-remote-script")
65
+ download_path.delete if download_path.file?
66
+
67
+ Vagrant::Util::Downloader.new(config.path, download_path).download!
68
+ script = download_path.read
69
+
70
+ download_path.delete
71
+ elsif config.path
72
+ # Just yield the path to that file...
73
+ root_path = @machine.env.root_path
74
+ script = Pathname.new(config.path).expand_path(root_path).read
75
+ else
76
+ # The script is just the inline code...
77
+ script = config.inline
78
+ end
79
+
80
+ # Replace Windows line endings with Unix ones unless binary file
81
+ script.gsub!(/\r\n?$/, "\n") if !config.binary
82
+
83
+ # Otherwise we have an inline script, we need to Tempfile it,
84
+ # and handle it specially...
85
+ file = Tempfile.new('vagrant-shell')
86
+
87
+ # Unless you set binmode, on a Windows host the shell script will
88
+ # have CRLF line endings instead of LF line endings, causing havoc
89
+ # when the guest executes it. This fixes [GH-1181].
90
+ file.binmode
91
+
92
+ begin
93
+ file.write(script)
94
+ file.fsync
95
+ file.close
96
+ yield file.path
97
+ ensure
98
+ file.close
99
+ file.unlink
100
+ end
101
+ end
102
+
103
+ # Auto-generate "safe" inventory file based on Vagrantfile,
104
+ # unless inventory_path is explicitly provided
105
+ def setup_inventory_file
106
+ return config.inventory_path if config.inventory_path
107
+
108
+ ssh = @machine.ssh_info
109
+
110
+ generated_inventory_file =
111
+ @machine.env.root_path.join("vagrant_ansible_inventory_#{machine.name}")
112
+
113
+ generated_inventory_file.open('w') do |file|
114
+ file.write("# Generated by Vagrant\n\n")
115
+ file.write("#{machine.name} ansible_ssh_host=#{ssh[:host]} ansible_ssh_port=#{ssh[:port]}\n")
116
+
117
+ # Write out groups information. Only include current
118
+ # machine and its groups to avoid Ansible errors on
119
+ # provisioning.
120
+ groups_of_groups = {}
121
+ included_groups = []
122
+
123
+ config.groups.each_pair do |gname, gmembers|
124
+ if gname.end_with?(":children")
125
+ groups_of_groups[gname] = gmembers
126
+ elsif gmembers.include?("#{machine.name}")
127
+ included_groups << gname
128
+ file.write("\n[#{gname}]\n")
129
+ file.write("#{machine.name}\n")
130
+ end
131
+ end
132
+
133
+ groups_of_groups.each_pair do |gname, gmembers|
134
+ unless (included_groups & gmembers).empty?
135
+ file.write("\n[#{gname}]\n")
136
+ gmembers.each do |gm|
137
+ file.write("#{gm}\n") if included_groups.include?(gm)
138
+ end
139
+ end
140
+ end
141
+ end
142
+
143
+ return generated_inventory_file.to_s
144
+ end
145
+
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,5 @@
1
+ module Vagrant
2
+ module GuestAnsible
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'vagrant-guest_ansible/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "vagrant-guest_ansible"
8
+ spec.version = Vagrant::GuestAnsible::VERSION
9
+ spec.authors = ["Roberto Quintanilla"]
10
+ spec.email = ["roberto.quintanilla@naranya.com"]
11
+ spec.description = %q{Ansible provisioner intended to run in the guest machine.}
12
+ spec.summary = %q{Ansible provisioner for guest machine.}
13
+ spec.homepage = "https://github.com/vovimayhem/vagrant-guest_ansible"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.5"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vagrant-guest_ansible
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Roberto Quintanilla
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-03-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: Ansible provisioner intended to run in the guest machine.
42
+ email:
43
+ - roberto.quintanilla@naranya.com
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - .gitignore
49
+ - Gemfile
50
+ - LICENSE.txt
51
+ - README.md
52
+ - Rakefile
53
+ - lib/vagrant-guest_ansible.rb
54
+ - lib/vagrant-guest_ansible/config.rb
55
+ - lib/vagrant-guest_ansible/guest_script.sh
56
+ - lib/vagrant-guest_ansible/plugin.rb
57
+ - lib/vagrant-guest_ansible/provisioner.rb
58
+ - lib/vagrant-guest_ansible/version.rb
59
+ - vagrant-guest_ansible.gemspec
60
+ homepage: https://github.com/vovimayhem/vagrant-guest_ansible
61
+ licenses:
62
+ - MIT
63
+ metadata: {}
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - '>='
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ required_rubygems_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ requirements: []
79
+ rubyforge_project:
80
+ rubygems_version: 2.0.14
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Ansible provisioner for guest machine.
84
+ test_files: []