集成 Terraform 和 Serverspec
Integrating Terraform and Serverspec
我正在使用 Terraform 自动构建基于 AWS EC2 的 docker 主机,然后使用其远程执行选项下载 docker 文件,构建并 运行 它.
我希望将它与 Serverspec 集成,但我正在努力解决两件事:
将新创建的AWS EC2实例的外部dns传递给Serverspec的最佳方式
如何配置 Serverspec 的 SSH 选项,以便它使用 ec2-user 帐户在 Amazon Linux AMI 上正确执行。
我通常会使用预定义的密钥对连接到 EC2 实例并且从不使用密码但是 ServerSpec 似乎 运行 在服务器上使用 sudo -p 格式的命令。
非常感谢任何建议。
spec_helper.rb
的内容
require 'serverspec'
require 'net/ssh'
set :ssh_options, :user => 'ec2-user'
还使用如下编辑的 rakefile 强制正确的 EC2 外部 dns(屏蔽):
require 'rake'
require 'rspec/core/rake_task'
hosts = %w(
ec2-nn-nn-nn-nnn.eu-west-1.compute.amazonaws.com
)
set :ssh_options, :user => 'ec2-user'
task :spec => 'spec:all'
namespace :spec do
task :all => hosts.map {|h| 'spec:' + h.split('.')[0] }
hosts.each do |host|
short_name = host.split('.')[0]
role = short_name.match(/[^0-9]+/)[0]
desc "Run serverspec to #{host}"
RSpec::Core::RakeTask.new(short_name) do |t|
ENV['TARGET_HOST'] = host
t.pattern = "spec/Nexus/*_spec.rb"
end
end
end
您可以将 IP 地址设为 output in Terraform。事实上,link 给出了一个例子来获取 AWS 实例的 IP 地址,在这种情况下名为 web
:
output "address" {
value = "${aws_instance.web.public_dns}"
}
然后你可以在terraform apply
和terraform output address
之后从命令行获取这个值。
您可以使用配置选项设置 sudo 密码:sudo_password。如果 ec2-user 可以在没有密码的情况下 运行 sudo,请将其设置为“”。 (参见 this blog post for an example.) Or pass it in the SUDO_PASSWORD environment variable, described here: http://serverspec.org/tutorial.html
我正在使用 Terraform 自动构建基于 AWS EC2 的 docker 主机,然后使用其远程执行选项下载 docker 文件,构建并 运行 它.
我希望将它与 Serverspec 集成,但我正在努力解决两件事:
将新创建的AWS EC2实例的外部dns传递给Serverspec的最佳方式
如何配置 Serverspec 的 SSH 选项,以便它使用 ec2-user 帐户在 Amazon Linux AMI 上正确执行。
我通常会使用预定义的密钥对连接到 EC2 实例并且从不使用密码但是 ServerSpec 似乎 运行 在服务器上使用 sudo -p 格式的命令。
非常感谢任何建议。
spec_helper.rb
的内容require 'serverspec'
require 'net/ssh'
set :ssh_options, :user => 'ec2-user'
还使用如下编辑的 rakefile 强制正确的 EC2 外部 dns(屏蔽):
require 'rake'
require 'rspec/core/rake_task'
hosts = %w(
ec2-nn-nn-nn-nnn.eu-west-1.compute.amazonaws.com
)
set :ssh_options, :user => 'ec2-user'
task :spec => 'spec:all'
namespace :spec do
task :all => hosts.map {|h| 'spec:' + h.split('.')[0] }
hosts.each do |host|
short_name = host.split('.')[0]
role = short_name.match(/[^0-9]+/)[0]
desc "Run serverspec to #{host}"
RSpec::Core::RakeTask.new(short_name) do |t|
ENV['TARGET_HOST'] = host
t.pattern = "spec/Nexus/*_spec.rb"
end
end
end
您可以将 IP 地址设为 output in Terraform。事实上,link 给出了一个例子来获取 AWS 实例的 IP 地址,在这种情况下名为
web
:output "address" { value = "${aws_instance.web.public_dns}" }
然后你可以在
terraform apply
和terraform output address
之后从命令行获取这个值。您可以使用配置选项设置 sudo 密码:sudo_password。如果 ec2-user 可以在没有密码的情况下 运行 sudo,请将其设置为“”。 (参见 this blog post for an example.) Or pass it in the SUDO_PASSWORD environment variable, described here: http://serverspec.org/tutorial.html