Class: Kitchen::Provisioner::AnsiblePlaybook
- Inherits:
-
Base
- Object
- Base
- Kitchen::Provisioner::AnsiblePlaybook
- Defined in:
- lib/kitchen/provisioner/ansible_playbook.rb
Overview
Ansible Playbook provisioner.
Instance Attribute Summary collapse
-
#tmp_dir ⇒ Object
Returns the value of attribute tmp_dir.
Instance Method Summary collapse
- #_run(cmd, idempotence = false) ⇒ Object
- #ansible_command(script) ⇒ Object
- #ansible_galacy_collection_command ⇒ Object
- #ansible_galaxy_command ⇒ Object
- #cd_ansible ⇒ Object
- #cleanup_sandbox ⇒ Object
- #create_sandbox ⇒ Object
- #custom_post_install_command ⇒ Object
- #custom_pre_install_command ⇒ Object
- #detect_debug ⇒ Object
- #finalize_config!(instance) ⇒ Object
- #init_command ⇒ Object
-
#initialize(provisioner_config) ⇒ AnsiblePlaybook
constructor
A new instance of AnsiblePlaybook.
- #install_busser_prereqs ⇒ Object
- #install_command ⇒ Object
- #install_windows_support ⇒ Object
- #prepare_command ⇒ Object
- #run_command ⇒ Object
- #verbosity_level(level = 1) ⇒ Object
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_dir ⇒ Object
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
#_run(cmd, idempotence = false) ⇒ Object
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 |
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 416 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, (idempotence), ansible_extra_flags, playbook_remote_path ].join(' ') end |
#ansible_command(script) ⇒ Object
459 460 461 462 463 464 465 466 467 468 |
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 459 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_galacy_collection_command ⇒ Object
483 484 485 486 487 488 489 490 491 492 493 |
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 483 def ansible_galacy_collection_command cmd = [ 'ansible-galaxy', 'collection', 'install', '--force', '-p', File.join(config[:root_path], 'collections'), '-r', File.join(config[:root_path], galaxy_requirements_collections) ].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 |
#ansible_galaxy_command ⇒ Object
470 471 472 473 474 475 476 477 478 479 480 481 |
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 470 def ansible_galaxy_command cmd = [ 'ansible-galaxy', 'install', '--force', galaxy_cert_ignore, '-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_ansible ⇒ Object
495 496 497 498 499 |
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 495 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_sandbox ⇒ Object
304 305 306 307 308 |
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 304 def cleanup_sandbox return if sandbox_path.nil? debug("Cleaning up local sandbox in #{sandbox_path}") FileUtils.rmtree(sandbox_path) end |
#create_sandbox ⇒ Object
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 278 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_command ⇒ Object
263 264 265 266 267 |
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 263 def custom_post_install_command <<-INSTALL #{config[:custom_post_install_command]} INSTALL end |
#custom_pre_install_command ⇒ Object
256 257 258 259 260 261 |
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 256 def custom_pre_install_command <<-INSTALL #{config[:custom_pre_install_command]} INSTALL end |
#detect_debug ⇒ Object
122 123 124 125 126 127 128 129 |
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 122 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_command ⇒ Object
269 270 271 272 273 274 275 276 |
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 269 def init_command dirs = %w(modules roles group_vars host_vars collections) .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_prereqs ⇒ Object
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 250 251 252 253 254 |
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 155 def install_busser_prereqs install = '' install << <<-INSTALL #{Util.shell_helpers} # Fix for https://github.com/test-kitchen/busser/issues/12 if [ -h /usr/bin/ruby ]; then L=$(readlink -f /usr/bin/ruby) #{sudo_env('rm')} /usr/bin/ruby #{sudo_env('ln')} -s $L /usr/bin/ruby fi INSTALL if require_ruby_for_busser install << <<-INSTALL if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ]; then if [ -z `grep -q 'Amazon Linux' /etc/system-release` ]; then rhelversion6=$(cat /etc/redhat-release | grep 'release 6') rhelversion7=$(cat /etc/redhat-release | grep 'release 7') # For CentOS6/CentOS7/RHEL6/RHEL7 install ruby from SCL if [ -n "$rhelversion6" ] || [ -n "$rhelversion7" ]; then if [ ! -d "/opt/rh/ruby200" ]; then echo "-----> Installing ruby200 SCL in CentOS6/CentOS7/RHEL6/RHEL7 to install busser to run tests" #{sudo_env('yum')} install -y centos-release-scl > #{detect_debug} #{sudo_env('yum')} install -y ruby200 > #{detect_debug} #{sudo_env('yum')} install -y ruby200-ruby-devel > #{detect_debug} echo "-----> Enabling ruby200" source /opt/rh/ruby200/enable echo "/opt/rh/ruby200/root/usr/lib64" | sudo tee -a /etc/ld.so.conf #{sudo_env('ldconfig')} #{sudo_env('ln')} -sf /opt/rh/ruby200/root/usr/bin/ruby /usr/bin/ruby #{sudo_env('ln')} -sf /opt/rh/ruby200/root/usr/bin/gem /usr/bin/gem fi else if [ ! $(which ruby) ]; then #{update_packages_redhat_cmd} > #{detect_debug} #{sudo_env('yum')} -y install ruby ruby-devel > #{detect_debug} fi fi else #{update_packages_redhat_cmd} > #{detect_debug} #{sudo_env('yum')} -y install ruby ruby-devel gcc > #{detect_debug} fi elif [ -f /etc/SuSE-release ] || [ -f /etc/SUSE-brand ]; then #{update_packages_suse_cmd} > #{detect_debug} #{sudo_env('zypper')} --non-interactive install ruby ruby-devel ca-certificates ca-certificates-cacert ca-certificates-mozilla > #{detect_debug} #{sudo_env('gem')} sources --add https://rubygems.org/ elif [ -f /etc/alpine-release ] || [ -d /etc/apk ]; then #{update_packages_alpine_cmd} #{sudo_env('apk')} add ruby ruby-dev ruby-io-console ca-certificates > #{detect_debug} else if [ ! $(which ruby) ]; then #{update_packages_debian_cmd} # default package selection for Debian/Ubuntu machines PACKAGES="ruby1.9.1 ruby1.9.1-dev" if [ "$(lsb_release -si)" = "Debian" ]; then debvers=$(sed 's/\\..*//' /etc/debian_version) if [ $debvers -ge 8 ]; then # this is jessie or better, where ruby1.9.1 is # no longer in the repositories PACKAGES="ruby ruby-dev ruby2.1 ruby2.1-dev" fi fi if [ "$(lsb_release -si)" = "Ubuntu" ]; then ubuntuvers=$(lsb_release -sr | tr -d .) if [ $ubuntuvers -ge 1410 ]; then # Default ruby is 2.x in utopic and newer PACKAGES="ruby ruby-dev" fi fi #{sudo_env('apt-get')} -y install $PACKAGES > #{detect_debug} if [ $debvers -eq 6 ]; then # in squeeze we need to update alternatives # for enable ruby1.9.1 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" #{sudo_env('update-alternatives')} $ALTERNATIVES_STRING # need to update gem tool because gem 1.3.7 from ruby 1.9.1 is broken #{sudo_env('gem')} install rubygems-update > #{detect_debug} #{sudo_env('/var/lib/gems/1.9.1/bin/update_rubygems')} # clear local gem cache #{sudo_env('rm')} -r /home/vagrant/.gem fi fi fi INSTALL elsif require_chef_for_busser && chef_url install << <<-INSTALL # install chef omnibus so that busser works as this is needed to run tests :( if [ ! -d "/opt/chef" ] then echo "-----> Installing Chef Omnibus to install busser to run tests" #{export_http_proxy} do_download #{chef_url} /tmp/install.sh #{sudo_env('sh')} /tmp/install.sh > #{detect_debug} fi INSTALL end install end |
#install_command ⇒ Object
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 116 117 118 119 120 |
# 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_pip3] info('Installing ansible through pip3') cmd = install_ansible_from_pip3_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 = <<-INSTALL if [ ! $(which ansible) ]; then if [ -f /etc/fedora-release ]; then #{Kitchen::Provisioner::Ansible::Os::Fedora.new('fedora', config).install_command} elif [ -f /etc/system-release ] && `grep -q 'Amazon Linux' /etc/system-release`; then #{Kitchen::Provisioner::Ansible::Os::Amazon.new('amazon', config).install_command} elif [ -f /etc/centos-release ] || [ -f /etc/redhat-release ]; then #{Kitchen::Provisioner::Ansible::Os::Redhat.new('redhat', config).install_command} elif [ -f /etc/SuSE-release ] || [ -f /etc/SUSE-brand ]; then #{Kitchen::Provisioner::Ansible::Os::Suse.new('suse', config).install_command} elif [[ "$OSTYPE" == "darwin"* ]]; then #{Kitchen::Provisioner::Ansible::Os::Darwin.new('darwin', config).install_command} elif [ -f /etc/alpine-release ] || [ -d /etc/apk ]; then #{Kitchen::Provisioner::Ansible::Os::Alpine.new('alpine', config).install_command} elif [ $(uname -s) = "OpenBSD" ]; then #{Kitchen::Provisioner::Ansible::Os::Openbsd.new('openbsd', config).install_command} elif [ $(uname -s) = "FreeBSD" ]; then #{Kitchen::Provisioner::Ansible::Os::Freebsd.new('freebsd', config).install_command} else #{Kitchen::Provisioner::Ansible::Os::Debian.new('debian', config).install_command} fi fi INSTALL end else return end result = custom_pre_install_command + cmd + install_windows_support + install_busser_prereqs + custom_post_install_command debug("Going to install ansible with: #{result}") result end |
#install_windows_support ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 131 def install_windows_support install = '' if require_windows_support info ("Installing Windows Support") info ("Installing pip") install << <<-INSTALL if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ]; then #{sudo_env('yum')} -y install python-devel krb5-devel krb5-libs krb5-workstation gcc > #{detect_debug} else if [ -f /etc/SuSE-release ] || [ -f /etc/SUSE-brand ]; then #{sudo_env('zypper')} ar #{python_sles_repo} > #{detect_debug} #{sudo_env('zypper')} --non-interactive install python python-devel krb5-client pam_krb5 > #{detect_debug} else #{sudo_env('apt-get')} -y install python-dev libkrb5-dev build-essential > #{detect_debug} fi fi #{export_http_proxy} #{sudo_env('easy_install')} pip > #{detect_debug} #{sudo_env('pip')} install pywinrm kerberos > #{detect_debug} INSTALL end install end |
#prepare_command ⇒ Object
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 382 383 384 385 386 387 388 389 390 391 392 393 |
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 310 def prepare_command commands = [] commands << [ "if [ $(uname -s) = 'FreeBSD' ]; then ETC_ANSIBLE='/usr/local/etc/ansible'; else ETC_ANSIBLE='/etc/ansible'; fi" ] # 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 galaxy_requirements_collections commands << ansible_galacy_collection_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_command ⇒ Object
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 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 |
# File 'lib/kitchen/provisioner/ansible_playbook.rb', line 395 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] 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, (idempotence), ansible_extra_flags, playbook_remote_path ].join(' ') end result = _run(cmd) if config[:idempotency_test] 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 function custom_post_play_command { #{config[:custom_post_play_command]} } trap custom_post_play_command EXIT TRAP end result = <<-RUN #{config[:custom_pre_play_command]} #{custom_post_play_trap} #{result} RUN debug("Going to invoke ansible-playbook with: #{result}") result end |
#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 |