Class: Kitchen::Provisioner::AnsiblePlaybook

Inherits:
Base
  • Object
show all
Defined in:
lib/kitchen/provisioner/ansible_playbook.rb

Overview

Ansible Playbook provisioner.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provisioner_config) ⇒ AnsiblePlaybook

Returns a new instance of AnsiblePlaybook.



44
45
46
47
48
49
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 44

def initialize(provisioner_config)
  config = Kitchen::Provisioner::Ansible::Config.new(provisioner_config)
  super(config)

  @os = Kitchen::Provisioner::Ansible::Os.make(ansible_platform, config)
end

Instance Attribute Details

#tmp_dirObject

Returns the value of attribute tmp_dir.



42
43
44
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 42

def tmp_dir
  @tmp_dir
end

Instance Method Details

#ansible_command(script) ⇒ Object



443
444
445
446
447
448
449
450
451
452
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 443

def ansible_command(script)
  if config[:ansible_sudo].nil? || config[:ansible_sudo] == true
    s = https_proxy ? "https_proxy=#{https_proxy}" : nil
    p = http_proxy ? "http_proxy=#{http_proxy}" : nil
    n = no_proxy ? "no_proxy=#{no_proxy}" : nil
    p || s || n ? " #{p} #{s} #{n} #{config[:sudo_command]} -s #{cd_ansible} #{script}" : "#{config[:sudo_command]} -s #{cd_ansible} #{script}"
  else
    return script
  end
end

#ansible_galaxy_commandObject



454
455
456
457
458
459
460
461
462
463
464
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 454

def ansible_galaxy_command
  cmd = [
      'ansible-galaxy', 'install', '--force',
      '-p', File.join(config[:root_path], 'roles'),
      '-r', File.join(config[:root_path], galaxy_requirements)
  ].join(' ')
  cmd = "https_proxy=#{https_proxy} #{cmd}" if https_proxy
  cmd = "http_proxy=#{http_proxy} #{cmd}" if http_proxy
  cmd = "no_proxy=#{no_proxy} #{cmd}" if no_proxy
  cmd
end

#cd_ansibleObject



466
467
468
469
470
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 466

def cd_ansible
 # this is not working so just return nil for now

 # File.exist?('ansible.cfg') ? "cd #{config[:root_path]};" : nil

 nil
end

#cleanup_sandboxObject



299
300
301
302
303
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 299

def cleanup_sandbox
  return if sandbox_path.nil?
  debug("Cleaning up local sandbox in #{sandbox_path}")
  FileUtils.rmtree(sandbox_path)
end

#create_sandboxObject



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 273

def create_sandbox
  super
  debug("Creating local sandbox in #{sandbox_path}")

  yield if block_given?

  prepare_playbook
  prepare_inventory
  prepare_modules
  prepare_roles
  prepare_ansible_cfg
  prepare_group_vars
  prepare_additional_copy_path
  prepare_host_vars
  prepare_hosts
  prepare_spec
  prepare_library_plugins
  prepare_callback_plugins
  prepare_filter_plugins
  prepare_lookup_plugins
  prepare_ansible_vault_password_file
  prepare_kerberos_conf_file
  prepare_additional_ssh_private_keys
  info('Finished Preparing files for transfer')
end

#custom_post_install_commandObject



258
259
260
261
262
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 258

def custom_post_install_command
  "    \#{config[:custom_post_install_command]}\n  INSTALL\nend\n"

#custom_pre_install_commandObject



251
252
253
254
255
256
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 251

def custom_pre_install_command
  "\n    \#{config[:custom_pre_install_command]}\n  INSTALL\nend\n"

#detect_debugObject



117
118
119
120
121
122
123
124
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 117

def detect_debug
  if ARGV.include? 'debug' or config[:show_command_output]
    result = "/dev/stdout"
  else
    result = "/dev/null"
  end
  return result
end

#finalize_config!(instance) ⇒ Object



51
52
53
54
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 51

def finalize_config!(instance)
  config.instance = instance
  super(instance)
end

#init_commandObject



264
265
266
267
268
269
270
271
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 264

def init_command
  dirs = %w(modules roles group_vars host_vars)
         .map { |dir| File.join(config[:root_path], dir) }.join(' ')
  cmd = "#{sudo_env('rm')} -rf #{dirs};"
  cmd += " mkdir -p #{config[:root_path]}"
  debug(cmd)
  cmd
end

#install_busser_prereqsObject



150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 150

def install_busser_prereqs
  install = ''
  install << "    \#{Util.shell_helpers}\n    # Fix for https://github.com/test-kitchen/busser/issues/12\n    if [ -h /usr/bin/ruby ]; then\n        L=$(readlink -f /usr/bin/ruby)\n        \#{sudo_env('rm')} /usr/bin/ruby\n        \#{sudo_env('ln')} -s $L /usr/bin/ruby\n    fi\n    INSTALL\n\n  if require_ruby_for_busser\n    install << <<-INSTALL\n      if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ]; then\n      if [ -z `grep -q 'Amazon Linux' /etc/system-release` ]; then\n      rhelversion6=$(cat /etc/redhat-release | grep 'release 6')\n      rhelversion7=$(cat /etc/redhat-release | grep 'release 7')\n      # For CentOS6/CentOS7/RHEL6/RHEL7 install ruby from SCL\n      if [ -n \"$rhelversion6\" ] || [ -n \"$rhelversion7\" ]; then\n      if [ ! -d \"/opt/rh/ruby200\" ]; then\n        echo \"-----> Installing ruby200 SCL in CentOS6/CentOS7/RHEL6/RHEL7 to install busser to run tests\"\n        \#{sudo_env('yum')} install -y centos-release-scl > \#{detect_debug}\n        \#{sudo_env('yum')} install -y ruby200 > \#{detect_debug}\n        \#{sudo_env('yum')} install -y ruby200-ruby-devel > \#{detect_debug}\n        echo \"-----> Enabling ruby200\"\n        source /opt/rh/ruby200/enable\n        echo \"/opt/rh/ruby200/root/usr/lib64\" | sudo tee -a /etc/ld.so.conf\n        \#{sudo_env('ldconfig')}\n        \#{sudo_env('ln')} -sf /opt/rh/ruby200/root/usr/bin/ruby /usr/bin/ruby\n        \#{sudo_env('ln')} -sf /opt/rh/ruby200/root/usr/bin/gem /usr/bin/gem\n      fi\n      else\n        if [ ! $(which ruby) ]; then\n          \#{update_packages_redhat_cmd} > \#{detect_debug}\n          \#{sudo_env('yum')} -y install ruby ruby-devel > \#{detect_debug}\n        fi\n      fi\n      else\n          \#{update_packages_redhat_cmd} > \#{detect_debug}\n          \#{sudo_env('yum')} -y install ruby ruby-devel gcc > \#{detect_debug}\n      fi\n      elif [ -f /etc/SuSE-release ]  || [ -f /etc/SUSE-brand ]; then\n          \#{update_packages_suse_cmd} > \#{detect_debug}\n          \#{sudo_env('zypper')} --non-interactive install ruby ruby-devel ca-certificates ca-certificates-cacert ca-certificates-mozilla > \#{detect_debug}\n          \#{sudo_env('gem')} sources --add https://rubygems.org/\n      elif [ -f /etc/alpine-release ]  || [ -d /etc/apk ]; then\n          \#{update_packages_alpine_cmd}\n          \#{sudo_env('apk')} add ruby ruby-dev ruby-io-console ca-certificates > \#{detect_debug}\n      else\n        if [ ! $(which ruby) ]; then\n          \#{update_packages_debian_cmd}\n          # default package selection for Debian/Ubuntu machines\n          PACKAGES=\"ruby1.9.1 ruby1.9.1-dev\"\n          if [ \"$(lsb_release -si)\" = \"Debian\" ]; then\n            debvers=$(sed 's/\\\\..*//' /etc/debian_version)\n            if [ $debvers -ge 8 ]; then\n              # this is jessie or better, where ruby1.9.1 is\n              # no longer in the repositories\n              PACKAGES=\"ruby ruby-dev ruby2.1 ruby2.1-dev\"\n            fi\n          fi\n          if [ \"$(lsb_release -si)\" = \"Ubuntu\" ]; then\n            ubuntuvers=$(lsb_release -sr | tr -d .)\n            if [ $ubuntuvers -ge 1410 ]; then\n              # Default ruby is 2.x in utopic and newer\n              PACKAGES=\"ruby ruby-dev\"\n            fi\n          fi\n          \#{sudo_env('apt-get')} -y install $PACKAGES > \#{detect_debug}\n          if [ $debvers -eq 6 ]; then\n              # in squeeze we need to update alternatives\n              # for enable ruby1.9.1\n              ALTERNATIVES_STRING=\"--install /usr/bin/ruby ruby /usr/bin/ruby1.9.1 10 --slave /usr/share/man/man1/ruby.1.gz ruby.1.gz /usr/share/man/man1/ruby1.9.1.1.gz --slave /usr/bin/erb erb /usr/bin/erb1.9.1 --slave /usr/bin/gem gem /usr/bin/gem1.9.1 --slave /usr/bin/irb irb /usr/bin/irb1.9.1 --slave /usr/bin/rake rake /usr/bin/rake1.9.1 --slave /usr/bin/rdoc rdoc /usr/bin/rdoc1.9.1 --slave /usr/bin/testrb testrb /usr/bin/testrb1.9.1 --slave /usr/share/man/man1/erb.1.gz erb.1.gz /usr/share/man/man1/erb1.9.1.1.gz --slave /usr/share/man/man1/gem.1.gz gem.1.gz /usr/share/man/man1/gem1.9.1.1.gz --slave /usr/share/man/man1/irb.1.gz irb.1.gz /usr/share/man/man1/irb1.9.1.1.gz --slave /usr/share/man/man1/rake.1.gz rake.1.gz /usr/share/man/man1/rake1.9.1.1.gz --slave /usr/share/man/man1/rdoc.1.gz rdoc.1.gz /usr/share/man/man1/rdoc1.9.1.1.gz --slave /usr/share/man/man1/testrb.1.gz testrb.1.gz /usr/share/man/man1/testrb1.9.1.1.gz\"\n              \#{sudo_env('update-alternatives')} $ALTERNATIVES_STRING\n              # need to update gem tool because gem 1.3.7 from ruby 1.9.1 is broken\n              \#{sudo_env('gem')} install rubygems-update > \#{detect_debug}\n              \#{sudo_env('/var/lib/gems/1.9.1/bin/update_rubygems')}\n              # clear local gem cache\n              \#{sudo_env('rm')} -r /home/vagrant/.gem\n          fi\n        fi\n     fi\n     INSTALL\n\n  elsif require_chef_for_busser && chef_url\n    install << <<-INSTALL\n      # install chef omnibus so that busser works as this is needed to run tests :(\n      if [ ! -d \"/opt/chef\" ]\n      then\n        echo \"-----> Installing Chef Omnibus to install busser to run tests\"\n        \#{export_http_proxy}\n        do_download \#{chef_url} /tmp/install.sh\n        \#{sudo_env('sh')} /tmp/install.sh > \#{detect_debug}\n      fi\n      INSTALL\n  end\n\n  install\nend\n"

#install_commandObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 70

def install_command
  if config[:require_ansible_omnibus]
    cmd = install_omnibus_command
  elsif config[:require_ansible_source]
    info('Installing ansible from source')
    cmd = install_ansible_from_source_command
  elsif config[:require_pip]
    info('Installing ansible through pip')
    cmd = install_ansible_from_pip_command
  elsif config[:require_ansible_repo]
    if !@os.nil?
      info("Installing ansible on #{@os.name}")
      cmd =  @os.install_command
    else
      info('Installing ansible, will try to determine platform os')
      cmd = "\n      if [ ! $(which ansible) ]; then\n        if [ -f /etc/fedora-release ]; then\n          \#{Kitchen::Provisioner::Ansible::Os::Fedora.new('fedora', config).install_command}\n        elif [ -f /etc/system-release ] && [ `grep -q 'Amazon Linux' /etc/system-release` ]; then\n          \#{Kitchen::Provisioner::Ansible::Os::Amazon.new('amazon', config).install_command}\n        elif [ -f /etc/centos-release ] || [ -f /etc/redhat-release ]; then\n          \#{Kitchen::Provisioner::Ansible::Os::Redhat.new('redhat', config).install_command}\n        elif [ -f /etc/SuSE-release ] || [ -f /etc/SUSE-brand ]; then\n          \#{Kitchen::Provisioner::Ansible::Os::Suse.new('suse', config).install_command}\n        elif [[ \"$OSTYPE\" == \"darwin\"* ]]; then\n          \#{Kitchen::Provisioner::Ansible::Os::Darwin.new('darwin', config).install_command}\n        elif [ -f /etc/alpine-release ] || [ -d /etc/apk ]; then\n          \#{Kitchen::Provisioner::Ansible::Os::Alpine.new('alpine', config).install_command}\n        elif [ $(uname -s) = \"OpenBSD\" ]; then\n          \#{Kitchen::Provisioner::Ansible::Os::Openbsd.new('openbsd', config).install_command}\n        else\n          \#{Kitchen::Provisioner::Ansible::Os::Debian.new('debian', config).install_command}\n        fi\n      fi\n      INSTALL\n    end\n  else\n    return\n  end\n\n  result = custom_pre_install_command + cmd + install_windows_support + install_busser_prereqs + custom_post_install_command\n  debug(\"Going to install ansible with: \#{result}\")\n  result\nend\n"

#install_windows_supportObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 126

def install_windows_support
  install = ''
  if require_windows_support
      info ("Installing Windows Support")
      info ("Installing pip")
      install << "        if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ]; then\n          \#{sudo_env('yum')} -y install python-devel krb5-devel krb5-libs krb5-workstation gcc > \#{detect_debug}\n        else\n          if [ -f /etc/SuSE-release ]  || [ -f /etc/SUSE-brand ]; then\n            \#{sudo_env('zypper')} ar \#{python_sles_repo} > \#{detect_debug}\n            \#{sudo_env('zypper')} --non-interactive install python python-devel krb5-client pam_krb5 > \#{detect_debug}\n          else\n            \#{sudo_env('apt-get')} -y install python-dev libkrb5-dev build-essential > \#{detect_debug}\n          fi\n        fi\n      \#{export_http_proxy}\n      \#{sudo_env('easy_install')} pip > \#{detect_debug}\n      \#{sudo_env('pip')} install pywinrm kerberos > \#{detect_debug}\n      INSTALL\n  end\n  install\nend\n"

#prepare_commandObject



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 305

def prepare_command
  commands = []

  # Prevent failure when ansible package installation doesn't contain /etc/ansible

  commands << [
    sudo_env("#{config[:shell_command]} -c '[ -d /etc/ansible ] || mkdir /etc/ansible'")
  ]

  commands << [
    sudo_env('cp'), File.join(config[:root_path], 'ansible.cfg'), '/etc/ansible'
  ].join(' ')

  commands << [
    sudo_env('cp -r'), File.join(config[:root_path], 'group_vars'), '/etc/ansible/.'
  ].join(' ')

  commands << [
    sudo_env('cp -r'), File.join(config[:root_path], 'host_vars'), '/etc/ansible/.'
  ].join(' ')

  if config[:ssh_known_hosts]
    config[:ssh_known_hosts].each do |host|
      info("Add #{host} to ~/.ssh/known_hosts")
      if host.include? ':'
        stripped_host, port = host.split(':')
        commands << "ssh-keyscan -p #{port} #{stripped_host} >> ~/.ssh/known_hosts 2> /dev/null"
      else
        commands << "ssh-keyscan #{host} >> ~/.ssh/known_hosts 2> /dev/null"
      end
    end
  end

  if config[:additional_ssh_private_keys]
    commands << [
      sudo_env('cp -r'), File.join(config[:root_path], 'ssh_private_keys'), '~/.ssh'
    ].join(' ')
  end

  if ansible_inventory
    if File.directory?(ansible_inventory)
      Dir.foreach(ansible_inventory) do |f|
        next if File.directory?("#{ansible_inventory}/#{f}")
        contents = File.open("#{ansible_inventory}/#{f}", 'rb') { |g| g.read }
        if contents.start_with?('#!')
          commands << [
            sudo_env('chmod +x'), File.join("#{config[:root_path]}/#{File.basename(ansible_inventory)}", File.basename(f))
          ].join(' ')
        end
      end
    else
      contents = File.open(ansible_inventory, 'rb') { |f| f.read }
      if contents.start_with?('#!')
        commands << [
          sudo_env('chmod +x'), File.join(config[:root_path], File.basename(ansible_inventory))
        ].join(' ')
      end
    end
  end

  if galaxy_requirements
    if config[:require_ansible_source]
      commands << setup_ansible_env_from_source
    end
    commands << ansible_galaxy_command
  end

  if kerberos_conf_file
    commands << [
      sudo_env('cp -f'), File.join(config[:root_path], 'krb5.conf'), '/etc'
    ].join(' ')
  end

  command = commands.join(' && ')
  debug("*** COMMAND TO RUN:")
  debug(command)
  command
end

#run_commandObject



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 383

def run_command
  return config[:ansible_playbook_command] unless config[:ansible_playbook_command].nil?
  if config[:require_ansible_source] && !config[:ansible_binary_path]
    # this is an ugly hack to get around the fact that extra vars uses ' and "

    cmd = ansible_command("PATH=#{config[:root_path]}/ansible/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games PYTHONPATH=#{config[:root_path]}/ansible/lib MANPATH=#{config[:root_path]}/ansible/docs/man ansible-playbook")
  elsif config[:ansible_binary_path]
    cmd = ansible_command("#{config[:ansible_binary_path]}/ansible-playbook")
  else
    cmd = ansible_command('ansible-playbook')
  end

  cmd = "#{env_vars} #{cmd}" if !config[:env_vars].none?
  cmd = "HTTPS_PROXY=#{https_proxy} #{cmd}" if https_proxy
  cmd = "HTTP_PROXY=#{http_proxy} #{cmd}" if http_proxy
  cmd = "NO_PROXY=#{no_proxy} #{cmd}" if no_proxy
  cmd = "ANSIBLE_ROLES_PATH=#{ansible_roles_path} #{cmd}" if ansible_roles_path
  cmd = "ANSIBLE_HOST_KEY_CHECKING=false #{cmd}" if !ansible_host_key_checking

  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(' ')
  if config[:idempotency_test]
    result = "#{result} && (echo 'Going to invoke ansible-playbook second time:'; #{result} | tee /tmp/idempotency_test.txt; grep -q 'changed=0.*failed=0' /tmp/idempotency_test.txt && (echo 'Idempotence test: PASS' && exit 0) || (echo 'Idempotence test: FAIL' && exit 1))"
  end
  if config[:custom_post_play_command]
    custom_post_play_trap = "      function custom_post_play_command {\n        \#{config[:custom_post_play_command]}\n      }\n      trap custom_post_play_command EXIT\n    TRAP\n  end\n  result = <<-RUN\n    \#{config[:custom_pre_play_command]}\n    \#{custom_post_play_trap}\n    \#{result}\n  RUN\n\n  debug(\"Going to invoke ansible-playbook with: \#{result}\")\n  result\n\nend\n"

#verbosity_level(level = 1) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 56

def verbosity_level(level = 1)
  level = level.to_sym if level.is_a? String
  log_levels = { info: 1, warn: 2, debug: 3, trace: 4 }
  if level.is_a?(Symbol) && log_levels.include?(level)
    # puts "Log Level is: #{log_levels[level]}"

    log_levels[level]
  elsif level.is_a?(Integer) && level > 0
    # puts "Log Level is: #{level}"

    level
  else
    fail 'Invalid ansible_verbosity setting.  Valid values are: 1, 2, 3, 4 OR :info, :warn, :debug, :trace'
  end
end