1

参考:http ://scienceofficersblog.blogspot.com/2016/02/testing-ansible-with-inspec.html

有很多帖子提到使用 Chef inspec 进行 Ansible 测试。但他们通常会给出这样的例子:

可靠:

- hosts: all
  user: root
  tasks:
  - debug: msg="debug {{inventory_hostname}}"
  - apt: name=apache2 state=present

厨师检查:

impact 0.7
title "Test some simple resources"
describe package('apache2') do
    it { should be_installed }
end

因此,如果我执行相同的 Ansible 块,它将确保安装了 apache2 包。同样有很多例子,比如端口 80 应该打开,因为如果我们在 chech 模式(试运行)下执行相同的剧本,那么我也会知道端口 80 是否正在侦听。

那么,为什么我们不能使用 Ansible 本身呢?当我们可以使用 Ansible 完成几乎所有事情时,Chef inspec 的确切必要性是什么?

4

2 回答 2

2

这主要是因为这些示例非常非常简单。

对于相同的 ansible 块,我假设您希望安装、运行和侦听端口 80 的 apache,更好的 inspec 示例是:

impact 0.7
title "Test some simple resources"
describe package('apache2') do
    it { should be_installed }
end
describe service('apache2') do
    it { should be_installed }
    it { should be_enabled }
    it { should be_running }
end
describe port(80) do
  it { should be_listening }
  its('processes') {should include 'apache2'}
end

但主要的一点是,对所需的配置和测试进行编码以确保单独计划的内容允许采用 TDD 方法。

Inspec 在 formater 方面还有另一个有趣的地方,它可以返回各种格式的审计信息。

归根结底,没有什么要求 Inspec 测试 Ansible,这是一种将“测试”和“操作”分开的做法,因此 ansible 代码中的错误使 apache 监听端口 800 不会被 ansible 捕获是你要求它设置的(这很正常),让它们分开确保测试不是由操作代码派生的。

于 2019-07-12T13:45:30.433 回答
1

我不确定为什么人们似乎认为 Inpec 大厨对此很有必要。Ansible 有一个断言模块,它允许确保事情评估为真,如此有效地注册任务的输出并断言关于它的事情是你所期望的。

https://docs.ansible.com/ansible/latest/modules/assert_module.html

事实上,这就是几乎所有 Ansible 集成测试在上游编写的方式https://github.com/ansible/ansible/tree/devel/test/integration/targets

于 2019-07-09T03:26:53.440 回答