From f1b3b7202561a9f7aafc9b782edb39b619c81361 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Surov=C4=8D=C3=A1k?= Date: Tue, 23 Oct 2018 19:28:42 +0200 Subject: [PATCH] feature: introduce keep_playbook_path to support playbook in dirs better --- lib/kitchen/provisioner/ansible/config.rb | 1 + lib/kitchen/provisioner/ansible_playbook.rb | 11 ++++-- provisioner_options.md | 1 + .../provisioner/ansible_playbook_spec.rb | 34 +++++++++++++++++++ 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lib/kitchen/provisioner/ansible/config.rb b/lib/kitchen/provisioner/ansible/config.rb index 92107a2..c264270 100644 --- a/lib/kitchen/provisioner/ansible/config.rb +++ b/lib/kitchen/provisioner/ansible/config.rb @@ -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) || diff --git a/lib/kitchen/provisioner/ansible_playbook.rb b/lib/kitchen/provisioner/ansible_playbook.rb index b440f06..d37a65f 100644 --- a/lib/kitchen/provisioner/ansible_playbook.rb +++ b/lib/kitchen/provisioner/ansible_playbook.rb @@ -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) @@ -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 @@ -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 @@ -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 diff --git a/provisioner_options.md b/provisioner_options.md index 167ac1f..2cd4300 100644 --- a/provisioner_options.md +++ b/provisioner_options.md @@ -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 diff --git a/spec/kitchen/provisioner/ansible_playbook_spec.rb b/spec/kitchen/provisioner/ansible_playbook_spec.rb index 2749532..bd49d28 100644 --- a/spec/kitchen/provisioner/ansible_playbook_spec.rb +++ b/spec/kitchen/provisioner/ansible_playbook_spec.rb @@ -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)