Skip to content

Commit

Permalink
Merge pull request #294 from msurovcak/feature/add-keep_playbook_path
Browse files Browse the repository at this point in the history
feature: introduce keep_playbook_path to support playbook in dirs better
  • Loading branch information
neillturner authored Oct 26, 2018
2 parents 1a00344 + f1b3b72 commit fc6d005
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/kitchen/provisioner/ansible/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ class Config
default_config :show_command_output, false
default_config :ignore_ansible_cfg, false
default_config :galaxy_ignore_certs, false
default_config :keep_playbook_path, false

default_config :playbook do |provisioner|
provisioner.calculate_path('default.yml', :file) ||
Expand Down
11 changes: 9 additions & 2 deletions lib/kitchen/provisioner/ansible_playbook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ def _run(cmd, idempotence = false)
extra_vars_file,
tags(idempotence),
ansible_extra_flags,
"#{File.join(config[:root_path], File.basename(config[:playbook]))}"
playbook_remote_path
].join(' ')
end
result = _run(cmd)
Expand Down Expand Up @@ -573,7 +573,8 @@ def tmp_modules_dir
end

def tmp_playbook_path
File.join(sandbox_path, File.basename(playbook))
return File.join(sandbox_path, playbook).to_s if config[:keep_playbook_path]
File.join(sandbox_path, File.basename(playbook)).to_s
end

def tmp_host_vars_dir
Expand Down Expand Up @@ -637,6 +638,11 @@ def playbook
config[:playbook]
end

def playbook_remote_path
return File.join(config[:root_path], config[:playbook]).to_s if config[:keep_playbook_path]
File.join(config[:root_path], File.basename(config[:playbook])).to_s
end

def hosts
config[:hosts]
end
Expand Down Expand Up @@ -1034,6 +1040,7 @@ def prepare_hosts
def prepare_playbook
info('Preparing playbook')
debug("Copying playbook from #{playbook} to #{tmp_playbook_path}")
FileUtils.mkdir_p(File.dirname(tmp_playbook_path)) if config[:keep_playbook_path]
FileUtils.cp_r(playbook, tmp_playbook_path)
end

Expand Down
1 change: 1 addition & 0 deletions provisioner_options.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ idempotency_skip_tags | [] | Adds a `--skip-tags` parameter with the specified t
ignore_extensions_from_root | ['.pyc'] | allow extensions to be ignored when copying from roles using additional_copy_role_path or doing recursive_additional_copy_path
ignore_paths_from_root | [] | allow extra paths to be ignored when copying from roles using additional_copy_role_path or using recursive_additional_copy_path
kerberos_conf_file | | Path of krb5.conf file using in Windows support
keep_playbook_path | false | Keep directory structure of `playbook`, e.g. when including vars with relativ paths from playbook
library_plugins_path | library | Ansible repo library plugins directory
lookup_plugins_path | lookup_plugins | Ansible repo `lookup_plugins` directory
max_retries | 1 | maximum number of retry attempts of converge command
Expand Down
34 changes: 34 additions & 0 deletions spec/kitchen/provisioner/ansible_playbook_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,40 @@
end
end

describe '#prepare playbook' do
it 'should correct cp with default options' do

config[:playbook] = '.gitignore'

sandbox_path = Dir.mktmpdir
allow(provisioner).to receive(:sandbox_path).and_return(sandbox_path)

expect { provisioner.send(:prepare_playbook) }.to_not raise_error
expect(File.exists?(File.join(sandbox_path, config[:playbook]))).to eq(true)
end
it 'should correct copy deep playbook into root_path by default' do

config[:playbook] = 'spec/data/requirements.yml'

sandbox_path = Dir.mktmpdir
allow(provisioner).to receive(:sandbox_path).and_return(sandbox_path)

expect { provisioner.send(:prepare_playbook) }.to_not raise_error
expect(File.exists?(File.join(sandbox_path, File.basename(config[:playbook])))).to eq(true)
end
it 'should correct copy deep playbook deep with keep_playbook_path=true' do

config[:playbook] = 'spec/data/requirements.yml'
config[:keep_playbook_path] = true

sandbox_path = Dir.mktmpdir
allow(provisioner).to receive(:sandbox_path).and_return(sandbox_path)

expect { provisioner.send(:prepare_playbook) }.to_not raise_error
expect(File.exists?(File.join(sandbox_path, config[:playbook]))).to eq(true)
end
end

describe '#prepare_inventory' do
it 'copies the inventory file to the sandbox when present' do
allow(provisioner).to receive(:sandbox_path).and_return(Dir.mktmpdir)
Expand Down

0 comments on commit fc6d005

Please sign in to comment.