我正在使用 Packer 在 AWS 中创建一个基于 Amazon Linux AMI 的 AMI,并使用 Ansible 进行预置。
我的一些 serverspec 测试失败了,我不明白为什么。第一个总是失败的方法是尝试确保 Docker 已安装并正在运行。尝试测试服务是否已启用并正在运行时总是失败。这是我的规格
require 'spec_helper'
describe package('docker') do
it { should be_installed }
end
describe service('docker') do
it { should be_enabled }
it { should be_running }
end
谷歌搜索了一下后,我尝试了相同的结果
require 'spec_helper'
describe package('docker') do
it { should be_installed }
end
describe service('docker') do
it { should be_enabled.with_level(2) }
it { should be_enabled.with_level(3) }
it { should be_enabled.with_level(4) }
it { should be_enabled.with_level(5) }
it { should be_running }
end
在使用 Ansible 进行配置时,我尝试执行 serverspec 用于规范的相同命令,我得到了我理解的正确答案,但规范仍然失败。
这些是 serverspec 正在使用的命令
amazon-ebs: 2) Service "docker" should be enabled with level 2
amazon-ebs: On host `localhost'
amazon-ebs: Failure/Error: it { should be_enabled.with_level(2) }
amazon-ebs: expected Service "docker" to be enabled with level 2
amazon-ebs: /bin/sh -c chkconfig\ --list\ docker\ \|\ grep\ 2:on
amazon-ebs:
amazon-ebs: # ./spec/localhost/docker_spec.rb:8:in `block (2 levels) in <top (required)>'
amazon-ebs:
amazon-ebs: 3) Service "docker" should be enabled with level 3
amazon-ebs: On host `localhost'
amazon-ebs: Failure/Error: it { should be_enabled.with_level(3) }
amazon-ebs: expected Service "docker" to be enabled with level 3
amazon-ebs: /bin/sh -c chkconfig\ --list\ docker\ \|\ grep\ 3:on
amazon-ebs:
amazon-ebs: # ./spec/localhost/docker_spec.rb:9:in `block (2 levels) in <top (required)>'
amazon-ebs:
amazon-ebs: 4) Service "docker" should be enabled with level 4
amazon-ebs: On host `localhost'
amazon-ebs: Failure/Error: it { should be_enabled.with_level(4) }
amazon-ebs: expected Service "docker" to be enabled with level 4
amazon-ebs: /bin/sh -c chkconfig\ --list\ docker\ \|\ grep\ 4:on
amazon-ebs:
amazon-ebs: # ./spec/localhost/docker_spec.rb:10:in `block (2 levels) in <top (required)>'
amazon-ebs:
amazon-ebs: 5) Service "docker" should be enabled with level 5
amazon-ebs: On host `localhost'
amazon-ebs: Failure/Error: it { should be_enabled.with_level(5) }
amazon-ebs: expected Service "docker" to be enabled with level 5
amazon-ebs: /bin/sh -c chkconfig\ --list\ docker\ \|\ grep\ 5:on
amazon-ebs:
amazon-ebs: # ./spec/localhost/docker_spec.rb:11:in `block (2 levels) in <top (required)>'
amazon-ebs:
amazon-ebs: 6) Service "docker" should be running
amazon-ebs: On host `localhost'
amazon-ebs: Failure/Error: it { should be_running }
amazon-ebs: expected Service "docker" to be running
amazon-ebs: /bin/sh -c service\ docker\ status
amazon-ebs:
amazon-ebs: # ./spec/localhost/docker_spec.rb:12:in `block (2 levels) in <top (required)>'
这是安装 Docker 的剧本,打印与 serverspec 使用的相同命令的输出
---
- name: Install yum dependencies
yum: name=docker state=present
- name: "Starting Docker service"
service: name=docker enabled=yes state=started
- command: "chkconfig --list docker"
register: docker_enabled
- debug: var=docker_enabled
- command: "service docker status"
register: docker_status
- debug: var=docker_status
两个命令的输出都是
amazon-ebs: TASK [debug] *******************************************************************
amazon-ebs: ok: [127.0.0.1] => {
amazon-ebs: "stdout": "docker \t0:off\t1:off\t2:on\t3:on\t4:on\t5:on\t6:off",
amazon-ebs: }
amazon-ebs:
amazon-ebs: TASK [debug] *******************************************************************
amazon-ebs: ok: [127.0.0.1] => {
amazon-ebs: "stdout": "docker (pid 2943) is running...",
amazon-ebs: }
我做错了什么?