4

It is pretty easy with Travis-CI:

  # Run the role/playbook again, checking to make sure it's idempotent.
  - >
    ansible-playbook -i tests/inventory tests/test.yml --connection=local --sudo --extra-vars "take_ownership_of_tmp=$OWN_TMP"
    | grep -q 'changed=0.*failed=0'
    && (echo 'Idempotence test: pass' && exit 0)
    || (echo 'Idempotence test: fail' && exit 1)

but I cannot use Travis, because I need to test my role on Debian systems. I use Test-Kitchen with ansible_playbook provisioner.

For example:

my .kitchen.yml

---
driver:
  name: vagrant

provisioner:
  name: ansible_playbook
  hosts: test-kitchen
  ansible_verbosity: 2
  ansible_verbose: true
  require_ansible_repo: false
  require_chef_omnibus: false
  require_ansible_omnibus: true
  require_chef_for_busser: false
  ansible_omnibus_url: https://raw.githubusercontent.com/neillturner/omnibus-ansible/master/ansible_install.sh

platforms:
  - name: debian-6.0.10-64-nocm
    driver_config: 
      customize:
           memory: 1024
           cpus: 2
      box: puppetlabs/debian-6.0.10-64-nocm
      box_url: https://atlas.hashicorp.com/puppetlabs/boxes/debian-6.0.10-64-nocm/versions/1.0.2/providers/virtualbox.box


suites:
  - name: default

verifier:
  ruby_bindir: '/usr/bin'

test playbook (test/integration/default/default.yml) is very simple

---
- hosts: all
  roles:
    - preconf
  vars:
    #some vars here

I may add second call of preconf role to the default.yml, but it is not helps.

With one call kitchen returns me a number of changed items:

   PLAY RECAP ******************************************************************** 
   localhost                  : ok=12   changed=8    unreachable=0    failed=0  

But with two calls it returns sum of items instead of two separate results

   PLAY RECAP ******************************************************************** 
   localhost                  : ok=24   changed=10   unreachable=0    failed=0 

So, how I can run the playbook second time and check the results for idempotence test?

4

2 回答 2

4

通过 BATS 测试为自己解决了:

#!/usr/bin/env bats
#

#
# Idempotence test
#

@test "Second run should change nothing" {
    run bash -c "ansible-playbook -i /tmp/kitchen/hosts /tmp/kitchen/default.yml -c local | grep -q 'changed=0.*failed=0' && exit 0 || exit 1"
    [ "$status" -eq 0 ]
}

UPD Serverspec 变体:

describe command('ansible-playbook -i /tmp/kitchen/hosts /tmp/kitchen/default.yml -c local') do
  its(:stderr) { should match /changed=0.*failed=0/ }

  its(:exit_status) { should eq 0 }
end
于 2015-09-10T03:20:55.183 回答
1

ansible_playbookansible_push厨房供应商现在都支持该参数idempotency_test,该参数实质上是第二次运行剧本并检查failedchanged任务。

您可以简单地添加idempotency_test: true到您的.kitchen.yml喜欢中:

---
driver:
  name: vagrant

provisioner:
  name: ansible_playbook
  hosts: test-kitchen
  ansible_verbosity: 2
  ansible_verbose: true
  require_ansible_repo: false
  require_chef_omnibus: false
  require_ansible_omnibus: true
  require_chef_for_busser: false
  ansible_omnibus_url: https://raw.githubusercontent.com/neillturner/omnibus-ansible/master/ansible_install.sh
  idempotency_test: true

platforms:
  - name: debian-6.0.10-64-nocm
    driver_config: 
      customize:
           memory: 1024
           cpus: 2
      box: puppetlabs/debian-6.0.10-64-nocm
      box_url: https://atlas.hashicorp.com/puppetlabs/boxes/debian-6.0.10-64-nocm/versions/1.0.2/providers/virtualbox.box


suites:
  - name: default

verifier:
  ruby_bindir: '/usr/bin'
于 2018-04-06T14:22:11.270 回答