Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pass more supported arguments to Ansible #12

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
*.gem
*.rbc
.bundle
.config
Expand All @@ -9,10 +8,9 @@ _yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
pkg

5 changes: 4 additions & 1 deletion lib/vagrant-guest_ansible/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class Config < Vagrant.plugin("2", :config)
attr_accessor :skip_tags
attr_accessor :start_at_task
attr_accessor :groups
attr_accessor :vault_password_file
attr_accessor :host_key_checking

# Joker attribute, used to pass unsupported arguments to ansible anyway
Expand All @@ -34,6 +35,7 @@ def initialize
@start_at_task = UNSET_VALUE
@raw_arguments = UNSET_VALUE
@groups = UNSET_VALUE
@vault_password_file = UNSET_VALUE
@host_key_checking = "true"
end

Expand Down Expand Up @@ -66,6 +68,7 @@ def finalize!
@skip_tags = nil if @skip_tags == UNSET_VALUE
@start_at_task = nil if @start_at_task == UNSET_VALUE
@raw_arguments = nil if @raw_arguments == UNSET_VALUE
@vault_password_file = nil if @vault_password_file == UNSET_VALUE
@groups = {} if @groups == UNSET_VALUE
@host_key_checking = nil if @host_key_checking == UNSET_VALUE
end
Expand Down Expand Up @@ -124,4 +127,4 @@ def validate(machine)
end
end
end
end
end
14 changes: 7 additions & 7 deletions lib/vagrant-guest_ansible/guest_script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

ANSIBLE_PLAYBOOK=$1
ANSIBLE_HOSTS=$2
ANSIBLE_EXTRA_VARS="$3"

# Strip arguments we're not going to pass to Ansible
shift 2;

TEMP_HOSTS="/tmp/ansible_hosts"

if [ ! -f /vagrant/$ANSIBLE_PLAYBOOK ]; then
Expand All @@ -28,7 +31,7 @@ if ! command -v ansible >/dev/null; then
exit 1
fi
echo "Installing pip via easy_install."
wget http://peak.telecommunity.com/dist/ez_setup.py
wget https://bootstrap.pypa.io/ez_setup.py
sudo python ez_setup.py && rm -f ez_setup.py
sudo easy_install pip
# Make sure setuptools are installed crrectly.
Expand All @@ -38,16 +41,13 @@ if ! command -v ansible >/dev/null; then
sudo pip install ansible
fi

if [ ! -z "$ANSIBLE_EXTRA_VARS" -a "$ANSIBLE_EXTRA_VARS" != " " ]; then
ANSIBLE_EXTRA_VARS=" --extra-vars $ANSIBLE_EXTRA_VARS"
fi

# stream output
export PYTHONUNBUFFERED=1
# show ANSI-colored output
export ANSIBLE_FORCE_COLOR=true

cp /vagrant/${ANSIBLE_HOSTS} ${TEMP_HOSTS} && chmod -x ${TEMP_HOSTS}
echo "Running Ansible as $USER:"
ansible-playbook /vagrant/${ANSIBLE_PLAYBOOK} --inventory-file=${TEMP_HOSTS} --connection=local $ANSIBLE_EXTRA_VARS

ansible-playbook --inventory-file=${TEMP_HOSTS} --connection=local "$@" /vagrant/$ANSIBLE_PLAYBOOK
rm ${TEMP_HOSTS}
30 changes: 21 additions & 9 deletions lib/vagrant-guest_ansible/provisioner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@ def initialize(machine, config)
end

def provision

args = [
config.playbook,
File.basename(self.setup_inventory_file),
format_extra_vars(config.extra_vars)
self.setup_inventory_file,
config.extra_vars && "--extra-vars=#{format_extra_vars(config.extra_vars)}" || "",
config.vault_password_file && "--vault-password-file=#{config.vault_password_file}" || "",
config.tags && "--tags=#{format_tags(config.tags)}" || "",
config.skip_tags && "--skip-tags=#{format_tags(config.skip_tags)}" || "",
config.verbose && "-#{config.verbose}" || "",
config.start_at_task && "--start-at-task=#{config.start_at_task}" || ""
].join(' ')

command = "chmod +x #{config.upload_path} && #{config.upload_path} #{args}"
Expand Down Expand Up @@ -52,11 +56,19 @@ def provision
protected

# converts the extra_vars to a properly formatted string
def format_extra_vars(extra_vars)
if extra_vars.kind_of?(String)
extra_vars.strip
elsif extra_vars.kind_of?(Hash)
"\"#{extra_vars.to_json.gsub('"', '\"')}\""
def format_extra_vars(arg)
if arg.kind_of?(String)
arg.strip
elsif arg.kind_of?(Hash)
"\"#{arg.to_json.gsub('"', '\"')}\""
end
end

def format_tags(arg)
if arg.kind_of?(String)
arg.strip
elsif arg.kind_of?(Array)
"\"#{arg.join(',')}\""
end
end

Expand Down Expand Up @@ -146,7 +158,7 @@ def setup_inventory_file
end
end

return generated_inventory_file.to_s
return File.basename(generated_inventory_file.to_s)
end

end
Expand Down
Binary file added pkg/vagrant-guest_ansible-0.0.3.gem
Binary file not shown.