Permalink
Browse files

Added provisioner options to specify tags for the idempotency test

Signed-off-by: Russell Snyder <ru.snyder@gmail.com>
  • Loading branch information...
rusnyder committed Jun 15, 2018
1 parent 879ba23 commit 925a8cd7073a5170a55141f7e394ad761ac05d0f
@@ -73,6 +73,8 @@ class Config
default_config :ansible_playbook_command, nil
default_config :ansible_host_key_checking, true
default_config :idempotency_test, nil
default_config :idempotency_tags, []
default_config :idempotency_skip_tags, []
default_config :ansible_inventory, nil
default_config :ansible_inventory_file, nil
default_config :ansible_limit, nil
@@ -401,25 +401,29 @@ def run_command
cmd = "#{cd_ansible} #{cmd}" if !config[:ansible_sudo].nil? && !config[:ansible_sudo]
cmd = "#{copy_private_key_cmd} #{cmd}" if config[:private_key]
result = [
cmd,
ansible_inventory_flag,
ansible_limit_flag,
ansible_connection_flag,
"-M #{File.join(config[:root_path], 'modules')}",
ansible_verbose_flag,
ansible_check_flag,
ansible_diff_flag,
ansible_vault_flag,
private_key,
extra_vars,
extra_vars_file,
tags,
ansible_extra_flags,
"#{File.join(config[:root_path], File.basename(config[:playbook]))}"
].join(' ')
def _run(cmd, idempotence = false)
[
cmd,
ansible_inventory_flag,
ansible_limit_flag,
ansible_connection_flag,
"-M #{File.join(config[:root_path], 'modules')}",
ansible_verbose_flag,
ansible_check_flag,
ansible_diff_flag,
ansible_vault_flag,
private_key,
extra_vars,
extra_vars_file,
tags(idempotence),
ansible_extra_flags,
"#{File.join(config[:root_path], File.basename(config[:playbook]))}"
].join(' ')
end
result = _run(cmd)
if config[:idempotency_test]
result = "#{result} && (echo 'Going to invoke ansible-playbook second time:'; #{result} | tee /tmp/idempotency_test.txt; if grep -qE 'changed=[1-9].*failed=|changed=.*failed=[1-9]' /tmp/idempotency_test.txt; then echo 'Idempotence test: FAIL' && exit 1; else echo 'Idempotence test: PASS' && exit 0; fi)"
idempotency_result = _run(cmd, true)
result = "#{result} && (echo 'Going to invoke ansible-playbook second time:'; #{idempotency_result} | tee /tmp/idempotency_test.txt; if grep -qE 'changed=[1-9].*failed=|changed=.*failed=[1-9]' /tmp/idempotency_test.txt; then echo 'Idempotence test: FAIL' && exit 1; else echo 'Idempotence test: PASS' && exit 0; fi)"
end
if config[:custom_post_play_command]
custom_post_play_trap = <<-TRAP
@@ -824,14 +828,24 @@ def extra_vars_file
bash_extra_vars
end
def tags
def tags(idempotence = false)
bash_tags = config.key?(:attributes) && config[:attributes].key?(:tags) && config[:attributes][:tags].is_a?(Array) ? config[:attributes][:tags] : config[:tags]
return nil if bash_tags.empty?
bash_skip_tags = []
if idempotence and config[:idempotency_test]
bash_tags += config[:idempotency_tags]
bash_skip_tags += config[:idempotency_skip_tags]
end
return nil if (bash_tags.empty? and bash_skip_tags.empty?)
bash_tags = bash_tags.join(',')
bash_tags = "-t '#{bash_tags}'"
debug(bash_tags)
bash_tags
result = ''
if not bash_tags.empty?
result += " --tags=#{bash_tags.join(',')}"
end
if not bash_skip_tags.empty?
result += " --skip-tags=#{bash_skip_tags.join(',')}"
end
debug(result)
result
end
def chef_url
View
@@ -73,6 +73,8 @@ hosts | | Create Ansible hosts file for localhost with this server group or lis
http_proxy | nil | Use HTTP proxy when installing Ansible, packages and running Ansible
https_proxy | nil | Use HTTPS proxy when installing Ansible, packages and running Ansible
idempotency_test | false | Enable to test Ansible playbook idempotency
idempotency_tags | [] | Adds a `--tags` parameter with the specified tags to the second invocation of `ansible-playbook` when `idempotency_test` is set to `true`
idempotency_skip_tags | [] | Adds a `--skip-tags` parameter with the specified tags to the second invocation of `ansible-playbook` when `idempotency_test` is set to `true`
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
@@ -64,6 +64,31 @@
it 'should give a sane run_command' do
expect(provisioner.run_command).to match(/ansible-playbook.*--skip-tags=skipme.*/)
end
idempotence_msg = 'Going to invoke ansible-playbook second time:'
describe "with idempotence" do
let(:config) do
custom_config.dup.merge!(idempotency_test: true)
end
it 'should add an idempotence phase to the run command' do
expect(provisioner.run_command).to match(/ansible-playbook.*#{idempotence_msg}.*ansible-playbook.*/)
end
end
describe "with idempotence tags" do
let(:config) do
custom_config.dup.merge!({
idempotency_test: true,
idempotency_tags: ['UUT'],
idempotency_skip_tags: ['setup']
})
end
it 'should add the idempotence tags to the run command' do
expect(provisioner.run_command).to match(/ansible-playbook.*#{idempotence_msg}.*ansible-playbook.*--tags=UUT.*--skip-tags=setup.*/)
end
end
end
describe '#prepare_ansible_vault_password_file' do

0 comments on commit 925a8cd

Please sign in to comment.