Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Investigation of testing with ansible

3,087 views

Published on

Published in: Technology
  • Be the first to comment

Investigation of testing with ansible

  1. 1. Investigation of Testing with Ansible Dennis Rowe @shr3kst3r wpengine.com
  2. 2. This is just a test • This is just one way to test your Ansible code. • These are basically my notes from some exploratory testing. • There be dragons. It is provided “as is” without warranty of any kind, either expressed or implied…
  3. 3. 3 Step Program • Lint it • Deploy it • Verify it
  4. 4. Lint It • ansible-lint • https://github.com/willthames/ansible-lint • pip install https://github.com/willthames/ansible-lint/ archive/master.zip
  5. 5. Deploy It • Vagrant • Ansible • More later
  6. 6. Verify It • Serverspec • http://serverspec.org/ • http://serverspec.org/resource_types.html • gem install serverspec
  7. 7. Steps
  8. 8. Prepare Vagrant $ mkdir ansible-hosts-example $ cd ansible-hosts-example $ vagrant init hashicorp/precise32 $ vagrant up $ vagrant destroy
  9. 9. Prepare Serverspec $ ansible-hosts-example serverspec-init Select OS type: 1) UN*X 2) Windows Select number: 1 Select a backend type: 1) SSH 2) Exec (local) Select number: 1 Vagrant instance y/n: y Auto-configure Vagrant from Vagrantfile? y/n: y + spec/ + spec/default/ + spec/default/httpd_spec.rb + spec/spec_helper.rb + Rakefile
  10. 10. Add to Vagrant config.vm.provision "ansible" do |ansible| ansible.playbook = "test.yml" ansible.sudo = true end
  11. 11. Create a Really Simple Playbook Don’t really use this playbook
  12. 12. test.yml --- - hosts: all vars_files: - vars/test.yml tasks: - include: tasks/main.yml
  13. 13. tasks/main.yml --- ! - name: ensure /etc/hosts template: src=etc/hosts dest=/etc/ hosts owner=root group=root mode=0644
  14. 14. vars/test.yml --- ! hosts: localhost: 127.0.0.1 precise32: 127.0.0.1 testdomain: 127.0.0.1
  15. 15. templates/etc/hosts {% for name in hosts|sort %} {{ hosts[name] }} {{ name }} {% endfor %}
  16. 16. Quick Test find . -type f -name "*.yml" | xargs ansible-lint vagrant up vagrant provision
  17. 17. spec/default/hosts_spec.rb require 'spec_helper' ! describe file('/etc/hosts') do it { should be_file } its(:content) { should match /127.0.0.1 testdomain/ } end ! describe command('getent ahostsv4 testdomain') do it { should return_stdout /127.0.0.1/ } end
  18. 18. Test It! $ rake spec /System/Library/Frameworks/Ruby.framework/ Versions/2.0/usr/bin/ruby -S rspec spec/ default/httpd_spec.rb ... ! Finished in 5.8 seconds 3 examples, 0 failures
  19. 19. Source • https://github.com/shr3kst3r/ansible-hosts-example • Has a couple helper scripts • test_it - Runs lint, provision, and spec • test_watch - Tries to watch the directory and test as changes occur
  20. 20. Break It Fix It!

×