Skip to content
This repository was archived by the owner on Apr 7, 2025. It is now read-only.

Commit 2f8b531

Browse files
committed
Add playbooks config
1 parent 20eefbd commit 2f8b531

File tree

5 files changed

+52
-12
lines changed

5 files changed

+52
-12
lines changed

.rubocop.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,6 @@ Metrics/CyclomaticComplexity:
3939
# Offense count: 3
4040
Metrics/PerceivedComplexity:
4141
Max: 30
42+
43+
Metrics/ClassLength:
44+
Max: 260

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ gem install kitchen-ansiblepush-<version>.gem
4242
provisioner :
4343
## required options
4444
name : ansible_push
45+
## Either `playbook` or `playbooks` is required
4546
playbook : "../../plays/web.yml" # Path to Play yaml
47+
playbooks :
48+
- "../../plays/database.yml" # Path to database play yaml
49+
- "../../plays/web.yml" # Path to web play yaml
4650
##
4751
## Optional argument
4852
ansible_config : "/path/to/ansible/ansible.cfg" # path to ansible config file

lib/kitchen-ansible/idempotancy.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22

33
def idempotency_test
44
info('*************** idempotency test ***************')
5+
conf[:playbooks].each do |playbook|
6+
_idempotency_test_single(playbook)
7+
end
8+
end
9+
10+
def _idempotency_test_single(playbook)
511
file_path = "/tmp/kitchen_ansible_callback/#{SecureRandom.uuid}.changes"
612
exec_ansible_command(
713
command_env.merge(
814
'ANSIBLE_CALLBACK_PLUGINS' => "#{File.dirname(__FILE__)}/../../callback/",
915
'ANSIBLE_CALLBACK_WHITELIST' => 'changes',
1016
'PLUGIN_CHANGES_FILE' => file_path
11-
), command, 'ansible-playbook'
17+
),
18+
command(playbook),
19+
'ansible-playbook'
1220
)
1321
debug("idempotency file #{file_path}")
1422
# Check ansible callback if changes has occured in the second run

lib/kitchen/provisioner/ansible_push.rb

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class AnsiblePush < Base
3939
default_config :host_key_checking, false
4040
default_config :mygroup, nil
4141
default_config :playbook, nil
42+
default_config :playbooks, []
4243
default_config :generate_inv, true
4344
default_config :generate_inv_path, '`which kitchen-ansible-inventory`'
4445
default_config :raw_arguments, nil
@@ -66,9 +67,24 @@ class AnsiblePush < Base
6667
def conf
6768
return @validated_config if defined? @validated_config
6869

69-
raise UserError, 'No playbook defined. Please specify one in .kitchen.yml' unless config[:playbook]
70+
unless config[:playbooks].is_a?(Array)
71+
raise UserError,
72+
"ansible playbooks is not an `Array` type. Given type: #{config[:playbooks].class}"
73+
end
74+
75+
playbooks_to_run = config[:playbooks].clone
76+
77+
if config[:playbooks] && config[:playbook]
78+
playbooks_to_run << config[:playbook]
79+
end
7080

71-
raise UserError, "playbook '#{config[:playbook]}' could not be found. Please check path" unless File.exist?(config[:playbook])
81+
if !playbooks_to_run || playbooks_to_run.empty?
82+
raise UserError, 'No `playbook` or `playbooks` defined. Please specify one in .kitchen.yml'
83+
end
84+
85+
playbooks_to_run.each do |playbook|
86+
raise UserError, "playbook '#{config[:playbook]}' could not be found. Please check path" unless File.exist?(playbook)
87+
end
7288

7389
if config[:vault_password_file] && !File.exist?(config[:vault_password_file])
7490
raise UserError, "Vault password '#{config[:vault_password_file]}' could not be found. Please check path"
@@ -167,10 +183,9 @@ def options
167183
@options = options
168184
end
169185

170-
def command
171-
return @command if defined? @command
186+
def command(playbook)
172187
@command = [conf[:ansible_playbook_bin]]
173-
@command = (@command << options << conf[:playbook]).flatten.join(' ')
188+
@command = (@command << options << playbook).flatten.join(' ')
174189
debug("Ansible push command= #{@command}")
175190
@command
176191
end
@@ -209,7 +224,7 @@ def true_command
209224
def install_command
210225
info('*************** AnsiblePush install_command ***************')
211226
# Test if ansible-playbook is installed and give a meaningful error message
212-
version_check = command + ' --version'
227+
version_check = command(conf[:playbooks].first) + ' --version'
213228
_, stdout, stderr, wait_thr = Open3.popen3(command_env, version_check)
214229
exit_status = wait_thr.value
215230
raise UserError, "#{version_check} returned a non zero '#{exit_status}' stdout : '#{stdout.read}', stderr: '#{stderr.read}'" unless exit_status.success?
@@ -232,8 +247,11 @@ def chef_installation(chef_url, omnibus_download_dir)
232247

233248
def run_command
234249
info('*************** AnsiblePush run ***************')
235-
exec_ansible_command(command_env, command, 'ansible-playbook')
236-
idempotency_test if conf[:idempotency_test]
250+
conf[:playbooks].each do |playbook|
251+
exec_ansible_command(command_env, command(playbook), 'ansible-playbook')
252+
idempotency_test if conf[:idempotency_test]
253+
end
254+
237255
info('*************** AnsiblePush end run *******************')
238256
debug("[#{name}] Converge completed (#{conf[:sleep]}s).")
239257
true_command # Place holder so a string is returned. This will execute true on remote host

spec/kitchen/provisioner/ansible_push_spec.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,14 @@
2424
end
2525

2626
let(:instance) do
27-
instance_double('Kitchen::Instance', name: 'coolbeans', logger: logger, suite: suite, platform: platform)
27+
double = instance_double(
28+
'Kitchen::Instance',
29+
name: 'coolbeans',
30+
logger: logger,
31+
suite: suite,
32+
platform: platform
33+
)
34+
double
2835
end
2936

3037
# let(:machine_name) do
@@ -39,11 +46,11 @@
3946
expect(provisioner.diagnose_plugin[:api_version]).to eq(2)
4047
end
4148

42-
it 'Should fail with no playbook file' do
49+
it "fails with no 'playbook' and 'playbooks' specified" do
4350
expect { provisioner.prepare_command }.to raise_error(Kitchen::UserError)
4451
end
4552

46-
describe 'Baisc config' do
53+
describe 'Basic config' do
4754
let(:config) do
4855
{
4956
test_base_path: '/b',

0 commit comments

Comments
 (0)