kitchen-ansible 0.0.17 → 0.0.19
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +195 -172
- data/kitchen-ansible.gemspec +35 -35
- data/lib/kitchen-ansible/version.rb +5 -5
- data/lib/kitchen/provisioner/ansible/config.rb +144 -139
- data/lib/kitchen/provisioner/ansible/librarian.rb +83 -83
- data/lib/kitchen/provisioner/ansible_playbook.rb +673 -632
- data/provisioner_options.md +93 -92
- metadata +3 -3
@@ -1,139 +1,144 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
#
|
3
|
-
# Author:: Michael Heap (<m@michaelheap.com>)
|
4
|
-
#
|
5
|
-
# Copyright (C) 2015 Michael Heap
|
6
|
-
#
|
7
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
-
# you may not use this file except in compliance with the License.
|
9
|
-
# You may obtain a copy of the License at
|
10
|
-
#
|
11
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
-
#
|
13
|
-
# Unless required by applicable law or agreed to in writing, software
|
14
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
-
# See the License for the specific language governing permissions and
|
17
|
-
# limitations under the License.
|
18
|
-
#
|
19
|
-
|
20
|
-
require 'json'
|
21
|
-
|
22
|
-
module Kitchen
|
23
|
-
|
24
|
-
module Provisioner
|
25
|
-
|
26
|
-
module Ansible
|
27
|
-
#
|
28
|
-
# Ansible Playbook provisioner.
|
29
|
-
#
|
30
|
-
class Config
|
31
|
-
include Kitchen::Configurable
|
32
|
-
|
33
|
-
attr_reader :instance
|
34
|
-
|
35
|
-
default_config :ansible_verbose, false
|
36
|
-
default_config :require_ansible_omnibus, false
|
37
|
-
default_config :ansible_omnibus_url, nil
|
38
|
-
default_config :ansible_omnibus_remote_path, '/opt/ansible'
|
39
|
-
default_config :ansible_version, nil
|
40
|
-
default_config :require_ansible_repo, true
|
41
|
-
default_config :extra_vars, {}
|
42
|
-
default_config :tags, []
|
43
|
-
default_config :ansible_apt_repo, "ppa:ansible/ansible"
|
44
|
-
default_config :ansible_yum_repo, "https://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm"
|
45
|
-
default_config :chef_bootstrap_url, "https://www.getchef.com/chef/install.sh"
|
46
|
-
default_config :require_chef_for_busser, false
|
47
|
-
default_config :require_ruby_for_busser, true
|
48
|
-
default_config :requirements_path, false
|
49
|
-
default_config :ansible_verbose, false
|
50
|
-
default_config :ansible_verbosity, 1
|
51
|
-
default_config :ansible_check, false
|
52
|
-
default_config :ansible_diff, false
|
53
|
-
default_config :ansible_platform, ''
|
54
|
-
default_config :update_package_repos, true
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
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
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
candidates
|
127
|
-
|
128
|
-
candidates.
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
end
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Author:: Michael Heap (<m@michaelheap.com>)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2015 Michael Heap
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
|
20
|
+
require 'json'
|
21
|
+
|
22
|
+
module Kitchen
|
23
|
+
|
24
|
+
module Provisioner
|
25
|
+
|
26
|
+
module Ansible
|
27
|
+
#
|
28
|
+
# Ansible Playbook provisioner.
|
29
|
+
#
|
30
|
+
class Config
|
31
|
+
include Kitchen::Configurable
|
32
|
+
|
33
|
+
attr_reader :instance
|
34
|
+
|
35
|
+
default_config :ansible_verbose, false
|
36
|
+
default_config :require_ansible_omnibus, false
|
37
|
+
default_config :ansible_omnibus_url, nil
|
38
|
+
default_config :ansible_omnibus_remote_path, '/opt/ansible'
|
39
|
+
default_config :ansible_version, nil
|
40
|
+
default_config :require_ansible_repo, true
|
41
|
+
default_config :extra_vars, {}
|
42
|
+
default_config :tags, []
|
43
|
+
default_config :ansible_apt_repo, "ppa:ansible/ansible"
|
44
|
+
default_config :ansible_yum_repo, "https://download.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm"
|
45
|
+
default_config :chef_bootstrap_url, "https://www.getchef.com/chef/install.sh"
|
46
|
+
default_config :require_chef_for_busser, false
|
47
|
+
default_config :require_ruby_for_busser, true
|
48
|
+
default_config :requirements_path, false
|
49
|
+
default_config :ansible_verbose, false
|
50
|
+
default_config :ansible_verbosity, 1
|
51
|
+
default_config :ansible_check, false
|
52
|
+
default_config :ansible_diff, false
|
53
|
+
default_config :ansible_platform, ''
|
54
|
+
default_config :update_package_repos, true
|
55
|
+
default_config :require_ansible_source, false
|
56
|
+
|
57
|
+
default_config :playbook do |provisioner|
|
58
|
+
provisioner.calculate_path('default.yml', :file) or
|
59
|
+
raise "No playbook found or specified! Please either set a playbook in your .kitchen.yml config, or create a default wrapper playbook for your role in test/integration/playbooks/default.yml or test/integration/default.yml"
|
60
|
+
end
|
61
|
+
|
62
|
+
default_config :roles_path do |provisioner|
|
63
|
+
provisioner.calculate_path('roles') or
|
64
|
+
raise 'No roles_path detected. Please specify one in .kitchen.yml'
|
65
|
+
end
|
66
|
+
|
67
|
+
default_config :group_vars_path do |provisioner|
|
68
|
+
provisioner.calculate_path('group_vars', :directory)
|
69
|
+
end
|
70
|
+
|
71
|
+
default_config :additional_copy_path do |provisioner|
|
72
|
+
provisioner.calculate_path('additional_copy', :directory)
|
73
|
+
end
|
74
|
+
|
75
|
+
default_config :host_vars_path do |provisioner|
|
76
|
+
provisioner.calculate_path('host_vars', :directory)
|
77
|
+
end
|
78
|
+
|
79
|
+
default_config :modules_path do |provisioner|
|
80
|
+
provisioner.calculate_path('modules', :directory)
|
81
|
+
end
|
82
|
+
|
83
|
+
default_config :ansiblefile_path do |provisioner|
|
84
|
+
provisioner.calculate_path('Ansiblefile', :file)
|
85
|
+
end
|
86
|
+
|
87
|
+
default_config :filter_plugins_path do |provisioner|
|
88
|
+
provisioner.calculate_path('filter_plugins', :directory)
|
89
|
+
end
|
90
|
+
|
91
|
+
default_config :ansible_vault_password_file do |provisioner|
|
92
|
+
provisioner.calculate_path('ansible-vault-password', :file)
|
93
|
+
end
|
94
|
+
|
95
|
+
def initialize(config = {})
|
96
|
+
init_config(config)
|
97
|
+
end
|
98
|
+
|
99
|
+
def set_instance(instance)
|
100
|
+
@instance = instance
|
101
|
+
end
|
102
|
+
|
103
|
+
def []=(attr, val)
|
104
|
+
config[attr] = val
|
105
|
+
end
|
106
|
+
|
107
|
+
def [](attr)
|
108
|
+
config[attr]
|
109
|
+
end
|
110
|
+
|
111
|
+
def key?(k)
|
112
|
+
return config.key?(k)
|
113
|
+
end
|
114
|
+
|
115
|
+
def keys
|
116
|
+
config.keys
|
117
|
+
end
|
118
|
+
|
119
|
+
def calculate_path(path, type = :directory)
|
120
|
+
|
121
|
+
if not instance
|
122
|
+
raise "Please ensure that an instance is provided before calling calculate_path"
|
123
|
+
end
|
124
|
+
|
125
|
+
base = config[:test_base_path]
|
126
|
+
candidates = []
|
127
|
+
candidates << File.join(base, instance.suite.name, 'ansible', path)
|
128
|
+
candidates << File.join(base, instance.suite.name, path)
|
129
|
+
candidates << File.join(base, path)
|
130
|
+
candidates << File.join(Dir.pwd, path)
|
131
|
+
candidates << File.join(Dir.pwd) if path == 'roles'
|
132
|
+
|
133
|
+
candidates.find do |c|
|
134
|
+
type == :directory ? File.directory?(c) : File.file?(c)
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
|
139
|
+
end
|
140
|
+
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
end
|
@@ -1,83 +1,83 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
#
|
3
|
-
# Author:: Fletcher Nichol (<fnichol@nichol.ca>) Neill Turner (<neillwturner@gmail.com>)
|
4
|
-
#
|
5
|
-
# Copyright (C) 2013, Fletcher Nichol, Neill Turner
|
6
|
-
#
|
7
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
-
# you may not use this file except in compliance with the License.
|
9
|
-
# You may obtain a copy of the License at
|
10
|
-
#
|
11
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
-
#
|
13
|
-
# Unless required by applicable law or agreed to in writing, software
|
14
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
-
# See the License for the specific language governing permissions and
|
17
|
-
# limitations under the License.
|
18
|
-
|
19
|
-
require 'kitchen/errors'
|
20
|
-
require 'kitchen/logging'
|
21
|
-
|
22
|
-
module Kitchen
|
23
|
-
|
24
|
-
module Provisioner
|
25
|
-
|
26
|
-
module Ansible
|
27
|
-
|
28
|
-
# Ansible module resolver that uses Librarian-Ansible and a Ansiblefile to
|
29
|
-
# calculate # dependencies.
|
30
|
-
#
|
31
|
-
class Librarian
|
32
|
-
|
33
|
-
include Logging
|
34
|
-
|
35
|
-
|
36
|
-
def initialize(ansiblefile, path, logger = Kitchen.logger)
|
37
|
-
@ansiblefile = ansiblefile
|
38
|
-
@path = path
|
39
|
-
@logger = logger
|
40
|
-
end
|
41
|
-
|
42
|
-
def self.load!(logger = Kitchen.logger)
|
43
|
-
load_librarian!(logger)
|
44
|
-
end
|
45
|
-
|
46
|
-
def resolve
|
47
|
-
version = ::Librarian::Ansible::VERSION
|
48
|
-
info("Resolving role dependencies with Librarian-Ansible #{version}...")
|
49
|
-
debug("Using Ansiblefile from #{ansiblefile}")
|
50
|
-
|
51
|
-
env = ::Librarian::Ansible::Environment.new(
|
52
|
-
:project_path => File.dirname(ansiblefile))
|
53
|
-
env.config_db.local["path"] = path
|
54
|
-
::Librarian::Action::Resolve.new(env).run
|
55
|
-
::Librarian::Action::Install.new(env).run
|
56
|
-
end
|
57
|
-
|
58
|
-
attr_reader :ansiblefile, :path, :logger
|
59
|
-
|
60
|
-
def self.load_librarian!(logger)
|
61
|
-
first_load = require 'librarian/ansible'
|
62
|
-
require 'librarian/ansible/environment'
|
63
|
-
require 'librarian/action/resolve'
|
64
|
-
require 'librarian/action/install'
|
65
|
-
|
66
|
-
version = ::Librarian::Ansible::VERSION
|
67
|
-
if first_load
|
68
|
-
logger.debug("Librarian-Ansible #{version} library loaded")
|
69
|
-
else
|
70
|
-
logger.debug("Librarian-Ansible #{version} previously loaded")
|
71
|
-
end
|
72
|
-
rescue LoadError => e
|
73
|
-
logger.fatal("The `librarian-ansible' gem is missing and must be installed" +
|
74
|
-
" or cannot be properly activated. Run" +
|
75
|
-
" `gem install librarian-ansible` or add the following to your" +
|
76
|
-
" Gemfile if you are using Bundler: `gem 'librarian-ansible'`.")
|
77
|
-
raise UserError,
|
78
|
-
"Could not load or activate Librarian-Ansible (#{e.message})"
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|
82
|
-
end
|
83
|
-
end
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Author:: Fletcher Nichol (<fnichol@nichol.ca>) Neill Turner (<neillwturner@gmail.com>)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2013, Fletcher Nichol, Neill Turner
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
|
19
|
+
require 'kitchen/errors'
|
20
|
+
require 'kitchen/logging'
|
21
|
+
|
22
|
+
module Kitchen
|
23
|
+
|
24
|
+
module Provisioner
|
25
|
+
|
26
|
+
module Ansible
|
27
|
+
|
28
|
+
# Ansible module resolver that uses Librarian-Ansible and a Ansiblefile to
|
29
|
+
# calculate # dependencies.
|
30
|
+
#
|
31
|
+
class Librarian
|
32
|
+
|
33
|
+
include Logging
|
34
|
+
|
35
|
+
|
36
|
+
def initialize(ansiblefile, path, logger = Kitchen.logger)
|
37
|
+
@ansiblefile = ansiblefile
|
38
|
+
@path = path
|
39
|
+
@logger = logger
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.load!(logger = Kitchen.logger)
|
43
|
+
load_librarian!(logger)
|
44
|
+
end
|
45
|
+
|
46
|
+
def resolve
|
47
|
+
version = ::Librarian::Ansible::VERSION
|
48
|
+
info("Resolving role dependencies with Librarian-Ansible #{version}...")
|
49
|
+
debug("Using Ansiblefile from #{ansiblefile}")
|
50
|
+
|
51
|
+
env = ::Librarian::Ansible::Environment.new(
|
52
|
+
:project_path => File.dirname(ansiblefile))
|
53
|
+
env.config_db.local["path"] = path
|
54
|
+
::Librarian::Action::Resolve.new(env).run
|
55
|
+
::Librarian::Action::Install.new(env).run
|
56
|
+
end
|
57
|
+
|
58
|
+
attr_reader :ansiblefile, :path, :logger
|
59
|
+
|
60
|
+
def self.load_librarian!(logger)
|
61
|
+
first_load = require 'librarian/ansible'
|
62
|
+
require 'librarian/ansible/environment'
|
63
|
+
require 'librarian/action/resolve'
|
64
|
+
require 'librarian/action/install'
|
65
|
+
|
66
|
+
version = ::Librarian::Ansible::VERSION
|
67
|
+
if first_load
|
68
|
+
logger.debug("Librarian-Ansible #{version} library loaded")
|
69
|
+
else
|
70
|
+
logger.debug("Librarian-Ansible #{version} previously loaded")
|
71
|
+
end
|
72
|
+
rescue LoadError => e
|
73
|
+
logger.fatal("The `librarian-ansible' gem is missing and must be installed" +
|
74
|
+
" or cannot be properly activated. Run" +
|
75
|
+
" `gem install librarian-ansible` or add the following to your" +
|
76
|
+
" Gemfile if you are using Bundler: `gem 'librarian-ansible'`.")
|
77
|
+
raise UserError,
|
78
|
+
"Could not load or activate Librarian-Ansible (#{e.message})"
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -1,632 +1,673 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
#
|
3
|
-
# Author:: Neill Turner (<neillwturner@gmail.com>)
|
4
|
-
#
|
5
|
-
# Copyright (C) 2013,2014 Neill Turner
|
6
|
-
#
|
7
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
-
# you may not use this file except in compliance with the License.
|
9
|
-
# You may obtain a copy of the License at
|
10
|
-
#
|
11
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
-
#
|
13
|
-
# Unless required by applicable law or agreed to in writing, software
|
14
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
-
# See the License for the specific language governing permissions and
|
17
|
-
# limitations under the License.
|
18
|
-
#
|
19
|
-
# See https://github.com/neillturner/kitchen-ansible/blob/master/provisioner_options.md
|
20
|
-
# for documentation configuration parameters with ansible_playbook provisioner.
|
21
|
-
#
|
22
|
-
|
23
|
-
require 'json'
|
24
|
-
require 'kitchen/provisioner/base'
|
25
|
-
require 'kitchen/provisioner/ansible/config'
|
26
|
-
require 'kitchen/provisioner/ansible/librarian'
|
27
|
-
|
28
|
-
module Kitchen
|
29
|
-
|
30
|
-
class Busser
|
31
|
-
|
32
|
-
def non_suite_dirs
|
33
|
-
%w{data}
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
module Provisioner
|
38
|
-
#
|
39
|
-
# Ansible Playbook provisioner.
|
40
|
-
#
|
41
|
-
class AnsiblePlaybook < Base
|
42
|
-
attr_accessor :tmp_dir
|
43
|
-
|
44
|
-
def initialize(provisioner_config)
|
45
|
-
config = Kitchen::Provisioner::Ansible::Config.new(provisioner_config)
|
46
|
-
super(config)
|
47
|
-
end
|
48
|
-
|
49
|
-
def finalize_config!(instance)
|
50
|
-
config.set_instance(instance)
|
51
|
-
super(instance)
|
52
|
-
end
|
53
|
-
|
54
|
-
def verbosity_level(level = 1)
|
55
|
-
level = level.to_sym if level.is_a? String
|
56
|
-
log_levels = { :info => 1, :warn => 2, :debug => 3, :trace => 4 }
|
57
|
-
if level.is_a? Symbol and log_levels.include? level
|
58
|
-
# puts "Log Level is: #{log_levels[level]}"
|
59
|
-
log_levels[level]
|
60
|
-
elsif level.is_a? Integer and level > 0
|
61
|
-
# puts "Log Level is: #{level}"
|
62
|
-
level
|
63
|
-
else
|
64
|
-
raise 'Invalid ansible_verbosity setting. Valid values are: 1, 2, 3, 4 OR :info, :warn, :debug, :trace'
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
def install_command
|
69
|
-
|
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
|
-
fi
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
#
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
#
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
end
|
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
|
-
end
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
252
|
-
|
253
|
-
|
254
|
-
|
255
|
-
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
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
|
-
|
298
|
-
|
299
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
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
|
-
|
382
|
-
|
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
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
446
|
-
|
447
|
-
|
448
|
-
|
449
|
-
|
450
|
-
|
451
|
-
|
452
|
-
|
453
|
-
|
454
|
-
|
455
|
-
|
456
|
-
|
457
|
-
|
458
|
-
|
459
|
-
|
460
|
-
|
461
|
-
|
462
|
-
|
463
|
-
|
464
|
-
|
465
|
-
|
466
|
-
|
467
|
-
|
468
|
-
|
469
|
-
|
470
|
-
|
471
|
-
|
472
|
-
|
473
|
-
config[:
|
474
|
-
end
|
475
|
-
|
476
|
-
|
477
|
-
|
478
|
-
|
479
|
-
|
480
|
-
|
481
|
-
|
482
|
-
|
483
|
-
|
484
|
-
|
485
|
-
|
486
|
-
|
487
|
-
|
488
|
-
|
489
|
-
|
490
|
-
|
491
|
-
|
492
|
-
|
493
|
-
|
494
|
-
|
495
|
-
|
496
|
-
|
497
|
-
|
498
|
-
|
499
|
-
|
500
|
-
|
501
|
-
|
502
|
-
|
503
|
-
|
504
|
-
|
505
|
-
|
506
|
-
|
507
|
-
|
508
|
-
|
509
|
-
|
510
|
-
|
511
|
-
|
512
|
-
|
513
|
-
|
514
|
-
|
515
|
-
|
516
|
-
|
517
|
-
|
518
|
-
|
519
|
-
|
520
|
-
|
521
|
-
|
522
|
-
|
523
|
-
|
524
|
-
|
525
|
-
|
526
|
-
|
527
|
-
|
528
|
-
|
529
|
-
|
530
|
-
|
531
|
-
|
532
|
-
|
533
|
-
|
534
|
-
|
535
|
-
|
536
|
-
|
537
|
-
|
538
|
-
|
539
|
-
|
540
|
-
|
541
|
-
|
542
|
-
|
543
|
-
|
544
|
-
|
545
|
-
|
546
|
-
|
547
|
-
|
548
|
-
|
549
|
-
|
550
|
-
end
|
551
|
-
|
552
|
-
debug("
|
553
|
-
|
554
|
-
|
555
|
-
|
556
|
-
|
557
|
-
|
558
|
-
|
559
|
-
|
560
|
-
|
561
|
-
|
562
|
-
|
563
|
-
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
569
|
-
|
570
|
-
|
571
|
-
|
572
|
-
|
573
|
-
|
574
|
-
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
579
|
-
|
580
|
-
|
581
|
-
|
582
|
-
|
583
|
-
|
584
|
-
|
585
|
-
|
586
|
-
|
587
|
-
|
588
|
-
|
589
|
-
|
590
|
-
|
591
|
-
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
|
596
|
-
|
597
|
-
|
598
|
-
|
599
|
-
|
600
|
-
|
601
|
-
|
602
|
-
|
603
|
-
|
604
|
-
|
605
|
-
|
606
|
-
|
607
|
-
|
608
|
-
|
609
|
-
|
610
|
-
|
611
|
-
|
612
|
-
|
613
|
-
|
614
|
-
|
615
|
-
|
616
|
-
|
617
|
-
|
618
|
-
|
619
|
-
|
620
|
-
|
621
|
-
|
622
|
-
|
623
|
-
|
624
|
-
|
625
|
-
|
626
|
-
|
627
|
-
|
628
|
-
|
629
|
-
|
630
|
-
|
631
|
-
|
632
|
-
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
#
|
3
|
+
# Author:: Neill Turner (<neillwturner@gmail.com>)
|
4
|
+
#
|
5
|
+
# Copyright (C) 2013,2014 Neill Turner
|
6
|
+
#
|
7
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
8
|
+
# you may not use this file except in compliance with the License.
|
9
|
+
# You may obtain a copy of the License at
|
10
|
+
#
|
11
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
12
|
+
#
|
13
|
+
# Unless required by applicable law or agreed to in writing, software
|
14
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
15
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
16
|
+
# See the License for the specific language governing permissions and
|
17
|
+
# limitations under the License.
|
18
|
+
#
|
19
|
+
# See https://github.com/neillturner/kitchen-ansible/blob/master/provisioner_options.md
|
20
|
+
# for documentation configuration parameters with ansible_playbook provisioner.
|
21
|
+
#
|
22
|
+
|
23
|
+
require 'json'
|
24
|
+
require 'kitchen/provisioner/base'
|
25
|
+
require 'kitchen/provisioner/ansible/config'
|
26
|
+
require 'kitchen/provisioner/ansible/librarian'
|
27
|
+
|
28
|
+
module Kitchen
|
29
|
+
|
30
|
+
class Busser
|
31
|
+
|
32
|
+
def non_suite_dirs
|
33
|
+
%w{data}
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
module Provisioner
|
38
|
+
#
|
39
|
+
# Ansible Playbook provisioner.
|
40
|
+
#
|
41
|
+
class AnsiblePlaybook < Base
|
42
|
+
attr_accessor :tmp_dir
|
43
|
+
|
44
|
+
def initialize(provisioner_config)
|
45
|
+
config = Kitchen::Provisioner::Ansible::Config.new(provisioner_config)
|
46
|
+
super(config)
|
47
|
+
end
|
48
|
+
|
49
|
+
def finalize_config!(instance)
|
50
|
+
config.set_instance(instance)
|
51
|
+
super(instance)
|
52
|
+
end
|
53
|
+
|
54
|
+
def verbosity_level(level = 1)
|
55
|
+
level = level.to_sym if level.is_a? String
|
56
|
+
log_levels = { :info => 1, :warn => 2, :debug => 3, :trace => 4 }
|
57
|
+
if level.is_a? Symbol and log_levels.include? level
|
58
|
+
# puts "Log Level is: #{log_levels[level]}"
|
59
|
+
log_levels[level]
|
60
|
+
elsif level.is_a? Integer and level > 0
|
61
|
+
# puts "Log Level is: #{level}"
|
62
|
+
level
|
63
|
+
else
|
64
|
+
raise 'Invalid ansible_verbosity setting. Valid values are: 1, 2, 3, 4 OR :info, :warn, :debug, :trace'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def install_command
|
69
|
+
if config[:require_ansible_omnibus]
|
70
|
+
cmd = install_omnibus_command
|
71
|
+
elsif config[:require_ansible_repo]
|
72
|
+
case ansible_platform
|
73
|
+
when "debian", "ubuntu"
|
74
|
+
info("Installing ansible on #{ansible_platform}")
|
75
|
+
cmd = install_debian_command
|
76
|
+
when "redhat", "centos", "fedora"
|
77
|
+
info("Installing ansible on #{ansible_platform}")
|
78
|
+
cmd = install_redhat_command
|
79
|
+
when "amazon"
|
80
|
+
info("Installing ansible on #{ansible_platform}")
|
81
|
+
cmd = install_amazon_linux_command
|
82
|
+
else
|
83
|
+
info("Installing ansible, will try to determine platform os")
|
84
|
+
cmd = <<-INSTALL
|
85
|
+
if [ ! $(which ansible) ]; then
|
86
|
+
if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ]; then
|
87
|
+
if ! [ grep -q 'Amazon Linux' /etc/system-release ]; then
|
88
|
+
#{install_redhat_command}
|
89
|
+
else
|
90
|
+
#{install_amazon_linux_command}
|
91
|
+
fi
|
92
|
+
else
|
93
|
+
#{install_debian_command}
|
94
|
+
fi
|
95
|
+
fi
|
96
|
+
INSTALL
|
97
|
+
end
|
98
|
+
elsif config[:require_ansible_source]
|
99
|
+
info("Installing ansible from source")
|
100
|
+
cmd = install_ansible_from_source_command
|
101
|
+
else
|
102
|
+
return
|
103
|
+
end
|
104
|
+
cmd + install_busser_prereqs
|
105
|
+
end
|
106
|
+
|
107
|
+
def install_busser_prereqs
|
108
|
+
install = ''
|
109
|
+
install << <<-INSTALL
|
110
|
+
#{Util.shell_helpers}
|
111
|
+
# Fix for https://github.com/test-kitchen/busser/issues/12
|
112
|
+
if [ -h /usr/bin/ruby ]; then
|
113
|
+
L=$(readlink -f /usr/bin/ruby)
|
114
|
+
#{sudo('rm')} /usr/bin/ruby
|
115
|
+
#{sudo('ln')} -s $L /usr/bin/ruby
|
116
|
+
fi
|
117
|
+
INSTALL
|
118
|
+
|
119
|
+
if require_ruby_for_busser
|
120
|
+
install << <<-INSTALL
|
121
|
+
if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ]; then
|
122
|
+
if ! [ grep -q 'Amazon Linux' /etc/system-release ]; then
|
123
|
+
rhelversion=$(cat /etc/redhat-release | grep 'release 6')
|
124
|
+
# For CentOS6/RHEL6 install ruby from SCL
|
125
|
+
if [ -n "$rhelversion" ]; then
|
126
|
+
if [ ! -d "/opt/rh/ruby193" ]; then
|
127
|
+
echo "-----> Installing ruby SCL in CentOS6/RHEL6 to install busser to run tests"
|
128
|
+
#{sudo('yum')} install -y centos-release-SCL
|
129
|
+
#{sudo('yum')} install -y ruby193
|
130
|
+
#{sudo('yum')} install -y ruby193-ruby-devel
|
131
|
+
echo "-----> Enabling ruby193"
|
132
|
+
source /opt/rh/ruby193/enable
|
133
|
+
echo "/opt/rh/ruby193/root/usr/lib64" | sudo tee -a /etc/ld.so.conf
|
134
|
+
sudo ldconfig
|
135
|
+
sudo ln -s /opt/rh/ruby193/root/usr/bin/ruby /usr/bin/ruby
|
136
|
+
sudo ln -s /opt/rh/ruby193/root/usr/bin/gem /usr/bin/gem
|
137
|
+
fi
|
138
|
+
else
|
139
|
+
if [ ! $(which ruby) ]; then
|
140
|
+
#{update_packages_redhat_cmd}
|
141
|
+
#{sudo('yum')} -y install ruby ruby-devel
|
142
|
+
fi
|
143
|
+
fi
|
144
|
+
else
|
145
|
+
#{update_packages_redhat_cmd}
|
146
|
+
#{sudo('yum')} -y install ruby ruby-devel gcc
|
147
|
+
fi
|
148
|
+
else
|
149
|
+
if [ ! $(which ruby) ]; then
|
150
|
+
#{update_packages_debian_cmd}
|
151
|
+
#{sudo('apt-get')} -y install ruby1.9.1 ruby1.9.1-dev
|
152
|
+
fi
|
153
|
+
fi
|
154
|
+
INSTALL
|
155
|
+
|
156
|
+
elsif require_chef_for_busser && chef_url then
|
157
|
+
install << <<-INSTALL
|
158
|
+
# install chef omnibus so that busser works as this is needed to run tests :(
|
159
|
+
if [ ! -d "/opt/chef" ]
|
160
|
+
then
|
161
|
+
echo "-----> Installing Chef Omnibus to install busser to run tests"
|
162
|
+
do_download #{chef_url} /tmp/install.sh
|
163
|
+
#{sudo('sh')} /tmp/install.sh
|
164
|
+
fi
|
165
|
+
INSTALL
|
166
|
+
end
|
167
|
+
|
168
|
+
install
|
169
|
+
end
|
170
|
+
|
171
|
+
def init_command
|
172
|
+
dirs = %w{modules roles group_vars host_vars}.
|
173
|
+
map { |dir| File.join(config[:root_path], dir) }.join(" ")
|
174
|
+
cmd = "#{sudo('rm')} -rf #{dirs};"
|
175
|
+
cmd = cmd+" mkdir -p #{config[:root_path]}"
|
176
|
+
debug(cmd)
|
177
|
+
cmd
|
178
|
+
end
|
179
|
+
|
180
|
+
def create_sandbox
|
181
|
+
super
|
182
|
+
debug("Creating local sandbox in #{sandbox_path}")
|
183
|
+
|
184
|
+
yield if block_given?
|
185
|
+
|
186
|
+
prepare_playbook
|
187
|
+
prepare_modules
|
188
|
+
prepare_roles
|
189
|
+
prepare_ansible_cfg
|
190
|
+
prepare_group_vars
|
191
|
+
prepare_additional_copy_path
|
192
|
+
prepare_host_vars
|
193
|
+
prepare_hosts
|
194
|
+
prepare_filter_plugins
|
195
|
+
prepare_ansible_vault_password_file
|
196
|
+
info('Finished Preparing files for transfer')
|
197
|
+
|
198
|
+
end
|
199
|
+
|
200
|
+
def cleanup_sandbox
|
201
|
+
return if sandbox_path.nil?
|
202
|
+
debug("Cleaning up local sandbox in #{sandbox_path}")
|
203
|
+
FileUtils.rmtree(sandbox_path)
|
204
|
+
end
|
205
|
+
|
206
|
+
def prepare_command
|
207
|
+
commands = []
|
208
|
+
|
209
|
+
# Prevent failure when ansible package installation doesn't contain /etc/ansible
|
210
|
+
commands << [
|
211
|
+
sudo("bash -c '[ -d /etc/ansible ] || mkdir /etc/ansible'")
|
212
|
+
]
|
213
|
+
|
214
|
+
commands << [
|
215
|
+
sudo('cp'),File.join(config[:root_path], 'ansible.cfg'),'/etc/ansible',
|
216
|
+
].join(' ')
|
217
|
+
|
218
|
+
commands << [
|
219
|
+
sudo('cp -r'), File.join(config[:root_path],'group_vars'), '/etc/ansible/.',
|
220
|
+
].join(' ')
|
221
|
+
|
222
|
+
commands << [
|
223
|
+
sudo('cp -r'), File.join(config[:root_path],'host_vars'), '/etc/ansible/.',
|
224
|
+
].join(' ')
|
225
|
+
|
226
|
+
if galaxy_requirements
|
227
|
+
if config[:require_ansible_source]
|
228
|
+
commands << setup_ansible_env_from_source
|
229
|
+
end
|
230
|
+
commands << [
|
231
|
+
'ansible-galaxy', 'install', '--force',
|
232
|
+
'-p', File.join(config[:root_path], 'roles'),
|
233
|
+
'-r', File.join(config[:root_path], galaxy_requirements),
|
234
|
+
].join(' ')
|
235
|
+
end
|
236
|
+
|
237
|
+
command = commands.join(' && ')
|
238
|
+
debug(command)
|
239
|
+
command
|
240
|
+
end
|
241
|
+
|
242
|
+
def run_command
|
243
|
+
if config[:require_ansible_source]
|
244
|
+
cmd = "#{setup_ansible_env_from_source} && ansible-playbook"
|
245
|
+
else
|
246
|
+
cmd = sudo("ansible-playbook")
|
247
|
+
end
|
248
|
+
[
|
249
|
+
cmd,
|
250
|
+
"-i #{File.join(config[:root_path], 'hosts')}",
|
251
|
+
"-M #{File.join(config[:root_path], 'modules')}",
|
252
|
+
ansible_verbose_flag,
|
253
|
+
ansible_check_flag,
|
254
|
+
ansible_diff_flag,
|
255
|
+
ansible_vault_flag,
|
256
|
+
extra_vars,
|
257
|
+
tags,
|
258
|
+
"#{File.join(config[:root_path], File.basename(config[:playbook]))}",
|
259
|
+
].join(" ")
|
260
|
+
end
|
261
|
+
|
262
|
+
protected
|
263
|
+
|
264
|
+
def load_needed_dependencies!
|
265
|
+
if File.exists?(ansiblefile)
|
266
|
+
debug("Ansiblefile found at #{ansiblefile}, loading Librarian-Ansible")
|
267
|
+
Ansible::Librarian.load!(logger)
|
268
|
+
end
|
269
|
+
end
|
270
|
+
|
271
|
+
def install_ansible_from_source_command
|
272
|
+
<<-INSTALL
|
273
|
+
if [ ! -d #{config[:root_path]}/ansible ]; then
|
274
|
+
if [ -f /etc/centos-release ] || [ -f /etc/redhat-release ]; then
|
275
|
+
#{update_packages_redhat_cmd}
|
276
|
+
#{sudo('yum')} -y install libselinux-python python2-devel git python-setuptools python-setuptools-dev
|
277
|
+
else
|
278
|
+
#{update_packages_debian_cmd}
|
279
|
+
#{sudo('apt-get')} -y install git python python-setuptools
|
280
|
+
fi
|
281
|
+
|
282
|
+
git clone git://github.com/ansible/ansible.git --recursive #{config[:root_path]}/ansible
|
283
|
+
#{sudo('easy_install')} pip
|
284
|
+
sudo -EH 'pip' install paramiko PyYAML Jinja2 httplib2
|
285
|
+
fi
|
286
|
+
INSTALL
|
287
|
+
end
|
288
|
+
|
289
|
+
def install_omnibus_command
|
290
|
+
info("Installing ansible using ansible omnibus")
|
291
|
+
version = if !config[:ansible_version].nil?
|
292
|
+
"-v #{config[:ansible_version]}"
|
293
|
+
else
|
294
|
+
""
|
295
|
+
end
|
296
|
+
<<-INSTALL
|
297
|
+
#{Util.shell_helpers}
|
298
|
+
|
299
|
+
if [ ! -d "#{config[:ansible_omnibus_remote_path]}" ]; then
|
300
|
+
echo "-----> Installing Ansible Omnibus"
|
301
|
+
do_download #{config[:ansible_omnibus_url]} /tmp/ansible_install.sh
|
302
|
+
#{sudo('sh')} /tmp/ansible_install.sh #{version}
|
303
|
+
fi
|
304
|
+
INSTALL
|
305
|
+
end
|
306
|
+
|
307
|
+
def install_debian_command
|
308
|
+
<<-INSTALL
|
309
|
+
if [ ! $(which ansible) ]; then
|
310
|
+
#{update_packages_debian_cmd}
|
311
|
+
|
312
|
+
## Install apt-utils to silence debconf warning: http://serverfault.com/q/358943/77156
|
313
|
+
#{sudo('apt-get')} -y install apt-utils git
|
314
|
+
|
315
|
+
## Fix debconf tty warning messages
|
316
|
+
export DEBIAN_FRONTEND=noninteractive
|
317
|
+
|
318
|
+
## 13.10, 14.04 include add-apt-repository in software-properties-common
|
319
|
+
#{sudo('apt-get')} -y install software-properties-common
|
320
|
+
|
321
|
+
## 10.04, 12.04 include add-apt-repository in
|
322
|
+
#{sudo('apt-get')} -y install python-software-properties
|
323
|
+
|
324
|
+
## 10.04 version of add-apt-repository doesn't accept --yes
|
325
|
+
## later versions require interaction from user, so we must specify --yes
|
326
|
+
## First try with -y flag, else if it fails, try without.
|
327
|
+
## "add-apt-repository: error: no such option: -y" is returned but is ok to ignore, we just retry
|
328
|
+
#{sudo('add-apt-repository')} -y #{ansible_apt_repo} || #{sudo('add-apt-repository')} #{ansible_apt_repo}
|
329
|
+
#{sudo('apt-get')} update
|
330
|
+
#{sudo('apt-get')} -y install ansible
|
331
|
+
fi
|
332
|
+
INSTALL
|
333
|
+
end
|
334
|
+
|
335
|
+
def install_redhat_command
|
336
|
+
<<-INSTALL
|
337
|
+
if [ ! $(which ansible) ]; then
|
338
|
+
#{sudo('rpm')} -ivh #{ansible_yum_repo}
|
339
|
+
#{update_packages_redhat_cmd}
|
340
|
+
#{sudo('yum')} -y install ansible#{ansible_redhat_version} libselinux-python git
|
341
|
+
fi
|
342
|
+
INSTALL
|
343
|
+
end
|
344
|
+
|
345
|
+
def install_amazon_linux_command
|
346
|
+
<<-INSTALL
|
347
|
+
if [ ! $(which ansible) ]; then
|
348
|
+
#{sudo('yum-config-manager')} --enable epel/x86_64
|
349
|
+
#{sudo('yum')} -y install ansible#{ansible_redhat_version} git
|
350
|
+
#{sudo('alternatives')} --set python /usr/bin/python2.6
|
351
|
+
#{sudo('yum')} clean all
|
352
|
+
#{sudo('yum')} install yum-python26 -y
|
353
|
+
fi
|
354
|
+
INSTALL
|
355
|
+
end
|
356
|
+
|
357
|
+
def setup_ansible_env_from_source
|
358
|
+
"cd #{config[:root_path]}/ansible && source hacking/env-setup && cd ../"
|
359
|
+
end
|
360
|
+
|
361
|
+
def tmp_modules_dir
|
362
|
+
File.join(sandbox_path, 'modules')
|
363
|
+
end
|
364
|
+
|
365
|
+
def tmp_playbook_path
|
366
|
+
File.join(sandbox_path, File.basename(playbook))
|
367
|
+
end
|
368
|
+
|
369
|
+
def tmp_host_vars_dir
|
370
|
+
File.join(sandbox_path, 'host_vars')
|
371
|
+
end
|
372
|
+
|
373
|
+
def tmp_roles_dir
|
374
|
+
File.join(sandbox_path, 'roles')
|
375
|
+
end
|
376
|
+
|
377
|
+
def tmp_filter_plugins_dir
|
378
|
+
File.join(sandbox_path, 'filter_plugins')
|
379
|
+
end
|
380
|
+
|
381
|
+
def tmp_ansible_vault_password_file_path
|
382
|
+
File.join(sandbox_path, File.basename(ansible_vault_password_file))
|
383
|
+
end
|
384
|
+
|
385
|
+
def ansiblefile
|
386
|
+
config[:ansiblefile_path] || ''
|
387
|
+
end
|
388
|
+
|
389
|
+
def galaxy_requirements
|
390
|
+
config[:requirements_path] || nil
|
391
|
+
end
|
392
|
+
|
393
|
+
def playbook
|
394
|
+
config[:playbook]
|
395
|
+
end
|
396
|
+
|
397
|
+
def hosts
|
398
|
+
config[:hosts]
|
399
|
+
end
|
400
|
+
|
401
|
+
def roles
|
402
|
+
config[:roles_path]
|
403
|
+
end
|
404
|
+
|
405
|
+
def role_name
|
406
|
+
File.basename(roles) == 'roles' ? '' : File.basename(roles)
|
407
|
+
end
|
408
|
+
|
409
|
+
def modules
|
410
|
+
config[:modules_path]
|
411
|
+
end
|
412
|
+
|
413
|
+
def group_vars
|
414
|
+
config[:group_vars_path].to_s
|
415
|
+
end
|
416
|
+
|
417
|
+
def additional_copy
|
418
|
+
config[:additional_copy_path]
|
419
|
+
end
|
420
|
+
|
421
|
+
def host_vars
|
422
|
+
config[:host_vars_path].to_s
|
423
|
+
end
|
424
|
+
|
425
|
+
def filter_plugins
|
426
|
+
config[:filter_plugins_path].to_s
|
427
|
+
end
|
428
|
+
|
429
|
+
def ansible_vault_password_file
|
430
|
+
config[:ansible_vault_password_file]
|
431
|
+
end
|
432
|
+
|
433
|
+
def ansible_debian_version
|
434
|
+
config[:ansible_version] ? "=#{config[:ansible_version]}" : nil
|
435
|
+
end
|
436
|
+
|
437
|
+
def ansible_redhat_version
|
438
|
+
config[:ansible_version] ? "-#{config[:ansible_version]}" : nil
|
439
|
+
end
|
440
|
+
|
441
|
+
def ansible_verbose_flag
|
442
|
+
config[:ansible_verbose] ? '-' << ('v' * verbosity_level(config[:ansible_verbosity])) : nil
|
443
|
+
end
|
444
|
+
|
445
|
+
def ansible_check_flag
|
446
|
+
config[:ansible_check] ? '--check' : nil
|
447
|
+
end
|
448
|
+
|
449
|
+
def ansible_diff_flag
|
450
|
+
config[:ansible_diff] ? '--diff' : nil
|
451
|
+
end
|
452
|
+
|
453
|
+
def ansible_vault_flag
|
454
|
+
debug(config[:ansible_vault_password_file])
|
455
|
+
config[:ansible_vault_password_file] ? "--vault-password-file=#{File.join(config[:root_path], File.basename(config[:ansible_vault_password_file]))}" : nil
|
456
|
+
end
|
457
|
+
|
458
|
+
def ansible_platform
|
459
|
+
config[:ansible_platform].to_s.downcase
|
460
|
+
end
|
461
|
+
|
462
|
+
def update_packages_debian_cmd
|
463
|
+
config[:update_package_repos] ? "#{sudo('apt-get')} update" : nil
|
464
|
+
end
|
465
|
+
|
466
|
+
def update_packages_redhat_cmd
|
467
|
+
config[:update_package_repos] ? "#{sudo('yum')} makecache" : nil
|
468
|
+
end
|
469
|
+
|
470
|
+
def extra_vars
|
471
|
+
bash_vars = config[:extra_vars]
|
472
|
+
if config.key?(:attributes) && config[:attributes].key?(:extra_vars) && config[:attributes][:extra_vars].is_a?(Hash)
|
473
|
+
bash_vars = config[:attributes][:extra_vars]
|
474
|
+
end
|
475
|
+
|
476
|
+
return nil if bash_vars.none?
|
477
|
+
bash_vars = JSON.dump(bash_vars)
|
478
|
+
bash_vars = "-e '#{bash_vars}'"
|
479
|
+
debug(bash_vars)
|
480
|
+
bash_vars
|
481
|
+
end
|
482
|
+
|
483
|
+
def tags
|
484
|
+
bash_tags = config.key?(:attributes) && config[:attributes].key?(:tags) && config[:attributes][:tags].is_a?(Array) ? config[:attributes][:tags] : config[:tags]
|
485
|
+
return nil if bash_tags.empty?
|
486
|
+
|
487
|
+
bash_tags = bash_tags.join(",")
|
488
|
+
bash_tags = "-t '#{bash_tags}'"
|
489
|
+
debug(bash_tags)
|
490
|
+
bash_tags
|
491
|
+
end
|
492
|
+
|
493
|
+
def ansible_apt_repo
|
494
|
+
config[:ansible_apt_repo]
|
495
|
+
end
|
496
|
+
|
497
|
+
def ansible_apt_repo_file
|
498
|
+
config[:ansible_apt_repo].split('/').last
|
499
|
+
end
|
500
|
+
|
501
|
+
def ansible_yum_repo
|
502
|
+
config[:ansible_yum_repo]
|
503
|
+
end
|
504
|
+
|
505
|
+
def chef_url
|
506
|
+
config[:chef_bootstrap_url]
|
507
|
+
end
|
508
|
+
|
509
|
+
def require_ruby_for_busser
|
510
|
+
config[:require_ruby_for_busser]
|
511
|
+
end
|
512
|
+
|
513
|
+
def require_chef_for_busser
|
514
|
+
config[:require_chef_for_busser]
|
515
|
+
end
|
516
|
+
|
517
|
+
def prepare_roles
|
518
|
+
info('Preparing roles')
|
519
|
+
debug("Using roles from #{roles}")
|
520
|
+
|
521
|
+
resolve_with_librarian if File.exists?(ansiblefile)
|
522
|
+
|
523
|
+
if galaxy_requirements
|
524
|
+
FileUtils.cp(galaxy_requirements, File.join(sandbox_path, galaxy_requirements))
|
525
|
+
end
|
526
|
+
|
527
|
+
# Detect whether we are running tests on a role
|
528
|
+
# If so, make sure to copy into VM so dir structure is like: /tmp/kitchen/roles/role_name
|
529
|
+
|
530
|
+
FileUtils.mkdir_p(File.join(tmp_roles_dir, role_name))
|
531
|
+
FileUtils.cp_r(Dir.glob("#{roles}/*"), File.join(tmp_roles_dir, role_name))
|
532
|
+
end
|
533
|
+
|
534
|
+
# /etc/ansible/ansible.cfg should contain
|
535
|
+
# roles_path = /tmp/kitchen/roles
|
536
|
+
def prepare_ansible_cfg
|
537
|
+
info('Preparing ansible.cfg file')
|
538
|
+
ansible_config_file = "#{File.join(sandbox_path, 'ansible.cfg')}"
|
539
|
+
|
540
|
+
roles_paths = []
|
541
|
+
roles_paths << File.join(config[:root_path], 'roles') unless config[:roles_path].nil?
|
542
|
+
additional_files.each do |additional_file|
|
543
|
+
roles_paths << File.join(config[:root_path], File.basename(additional_file))
|
544
|
+
end
|
545
|
+
|
546
|
+
if roles_paths.empty?
|
547
|
+
info('No roles have been set. empty ansible.cfg generated')
|
548
|
+
File.open(ansible_config_file, "wb") do |file|
|
549
|
+
file.write("#no roles path specified\n")
|
550
|
+
end
|
551
|
+
else
|
552
|
+
debug("Setting roles_path inside VM to #{ roles_paths.join(':') }")
|
553
|
+
File.open( ansible_config_file, "wb") do |file|
|
554
|
+
file.write("[defaults]\nroles_path = #{ roles_paths.join(':') }\n")
|
555
|
+
end
|
556
|
+
end
|
557
|
+
end
|
558
|
+
|
559
|
+
|
560
|
+
# localhost ansible_connection=local
|
561
|
+
# [example_servers]
|
562
|
+
# localhost
|
563
|
+
def prepare_hosts
|
564
|
+
info('Preparing hosts file')
|
565
|
+
|
566
|
+
if config[:hosts].nil?
|
567
|
+
raise 'No hosts have been set. Please specify one in .kitchen.yml'
|
568
|
+
else
|
569
|
+
debug("Using host from #{hosts}")
|
570
|
+
File.open(File.join(sandbox_path, "hosts"), "wb") do |file|
|
571
|
+
file.write("localhost ansible_connection=local\n[#{hosts}]\nlocalhost\n")
|
572
|
+
end
|
573
|
+
end
|
574
|
+
end
|
575
|
+
|
576
|
+
def prepare_playbook
|
577
|
+
info('Preparing playbook')
|
578
|
+
debug("Copying playbook from #{playbook} to #{tmp_playbook_path}")
|
579
|
+
FileUtils.cp_r(playbook, tmp_playbook_path)
|
580
|
+
end
|
581
|
+
|
582
|
+
|
583
|
+
def prepare_group_vars
|
584
|
+
info('Preparing group_vars')
|
585
|
+
tmp_group_vars_dir = File.join(sandbox_path, 'group_vars')
|
586
|
+
FileUtils.mkdir_p(tmp_group_vars_dir)
|
587
|
+
|
588
|
+
unless File.directory?(group_vars)
|
589
|
+
info 'nothing to do for group_vars'
|
590
|
+
return
|
591
|
+
end
|
592
|
+
|
593
|
+
debug("Using group_vars from #{group_vars}")
|
594
|
+
FileUtils.cp_r(Dir.glob("#{group_vars}/*"), tmp_group_vars_dir)
|
595
|
+
end
|
596
|
+
|
597
|
+
def prepare_additional_copy_path
|
598
|
+
info('Preparing additional_copy_path')
|
599
|
+
additional_files.each do |file|
|
600
|
+
destination = File.join(sandbox_path, File.basename(file))
|
601
|
+
if File.directory?(file)
|
602
|
+
info("Copy dir: #{file} #{destination}")
|
603
|
+
FileUtils.cp_r(file, destination)
|
604
|
+
else
|
605
|
+
info("Copy file: #{file} #{destination}")
|
606
|
+
FileUtils.cp file, destination
|
607
|
+
end
|
608
|
+
end
|
609
|
+
end
|
610
|
+
|
611
|
+
def additional_files
|
612
|
+
additional_files = []
|
613
|
+
if ( additional_copy )
|
614
|
+
additional_files = additional_copy.kind_of?(Array) ? additional_copy : [additional_copy]
|
615
|
+
end
|
616
|
+
additional_files.map { |additional_dir | additional_dir.to_s }
|
617
|
+
end
|
618
|
+
|
619
|
+
def prepare_host_vars
|
620
|
+
info('Preparing host_vars')
|
621
|
+
FileUtils.mkdir_p(tmp_host_vars_dir)
|
622
|
+
|
623
|
+
unless File.directory?(host_vars)
|
624
|
+
info 'nothing to do for host_vars'
|
625
|
+
return
|
626
|
+
end
|
627
|
+
|
628
|
+
debug("Using host_vars from #{host_vars}")
|
629
|
+
FileUtils.cp_r(Dir.glob("#{host_vars}/*"), tmp_host_vars_dir)
|
630
|
+
end
|
631
|
+
|
632
|
+
def prepare_modules
|
633
|
+
info('Preparing modules')
|
634
|
+
|
635
|
+
FileUtils.mkdir_p(tmp_modules_dir)
|
636
|
+
|
637
|
+
if modules && File.directory?(modules)
|
638
|
+
debug("Using modules from #{modules}")
|
639
|
+
FileUtils.cp_r(Dir.glob("#{modules}/*"), tmp_modules_dir, remove_destination: true)
|
640
|
+
else
|
641
|
+
info 'nothing to do for modules'
|
642
|
+
end
|
643
|
+
end
|
644
|
+
|
645
|
+
def prepare_filter_plugins
|
646
|
+
info('Preparing filter_plugins')
|
647
|
+
FileUtils.mkdir_p(tmp_filter_plugins_dir)
|
648
|
+
|
649
|
+
if filter_plugins && File.directory?(filter_plugins)
|
650
|
+
debug("Using filter_plugins from #{filter_plugins}")
|
651
|
+
FileUtils.cp_r(Dir.glob("#{filter_plugins}/*.py"), tmp_filter_plugins_dir, remove_destination: true)
|
652
|
+
else
|
653
|
+
info 'nothing to do for filter_plugins'
|
654
|
+
end
|
655
|
+
end
|
656
|
+
|
657
|
+
def prepare_ansible_vault_password_file
|
658
|
+
if ansible_vault_password_file
|
659
|
+
info('Preparing ansible vault password')
|
660
|
+
debug("Copying ansible vault password file from #{ansible_vault_password_file} to #{tmp_ansible_vault_password_file_path}")
|
661
|
+
|
662
|
+
FileUtils.cp(ansible_vault_password_file, tmp_ansible_vault_password_file_path)
|
663
|
+
end
|
664
|
+
end
|
665
|
+
|
666
|
+
def resolve_with_librarian
|
667
|
+
Kitchen.mutex.synchronize do
|
668
|
+
Ansible::Librarian.new(ansiblefile, tmp_roles_dir, logger).resolve
|
669
|
+
end
|
670
|
+
end
|
671
|
+
end
|
672
|
+
end
|
673
|
+
end
|