GithubHelp home page GithubHelp logo

ansible-sample-tdd's Introduction

ansible-sample-tdd

Test Driven Development for Ansible by ServerSpec. It's Sample

ServerSpec is a test framework based on Ruby.

NOTICE
If you want to use these specfiles on other project,
Please install ansible_spec ruby-gem to parse Ansible playbook & inventory files.

Environment

$ ruby -v
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]

$ gem list (needs)
ansible_spec (0.3)
ansible-vault (0.2.1)
serverspec (2.41.3)
specinfra (2.75.1)
hostlist_expression (0.2.1)

Important( from v0.1)

this sample use (Rubygem) ansible_spec

gem install ansible_spec

Directory

.
├── .ansiblespec                 #Create file (use Serverspec)
├── README.md
├── hosts                        #use Ansible and Serverspec if `.ansiblespec` do not exist.
├── exec_hosts.sh                #Dynamic Inventory sample. if you use DynamicInventory, change `.ansiblespec`
├── site.yml                     #use Ansible and Serverspec if `.ansiblespec` do not exist.
├── nginx.yml                    #(comment-out) included by site.yml
├── roles
│   ├── mariadb
│   │   ├── spec
│   │   │   └── mariadb_spec.rb
│   │   ├── tasks
│   │   │   └── main.yml
│   │   └── templates
│   │       └── mariadb.repo
│   └── nginx
│       ├── handlers
│       │   └── main.yml
│       ├── spec                 #use Serverspec
│       │   └── nginx_spec.rb
│       ├── tasks
│       │   └── main.yml
│       ├── templates
│       │   └── nginx.repo
│       └── vars
│           └── main.yml
├── Rakefile                     #use Serverspec
└── spec                         #use Serverspec 
    └── spec_helper.rb

Change .ansiblespec(v0.0.1.3)

read this section If .ansiblespec exist, use variables(playbook and inventory).
So, If you don't use site.yml and hosts, you need to change this file.
If .ansiblespec is not found, use site.yml as playbook and hosts as inventory.

--- 
- 
  playbook: site.yml
  inventory: hosts

Run Playbook

Please change the target IP address of server -> hosts (default is 192.168.0.103 and 104)

$ ansible-playbook site.yml -i hosts

#Test

Serverspec with Ansible

Serverspec use this file. (Rakefile understand the syntax of Ansible.)

  • hosts
    hosts can use [group_name]
[server]
192.168.100.203

# under sample

#192.168.0.103:22
#192.168.0.103 ansible_ssh_port=22
#192.168.0.103 ansible_ssh_private_key_file=~/.ssh/id_rsa
#test ansible_ssh_host=192.168.0.103
#192.168.0.103 ansible_ssh_user=root
#jumper ansible_ssh_port=22 ansible_ssh_host=192.168.0.103

#[sample]
#(comment) www1.example.com to www99.example.com
#www[1:99].example.com

#(comment)  www01.example.com to www99.example.com
#www[01:99].example.com

#(comment)  db-a.example.com to db-z.example.com
#db-[a:z].example.com

#(comment)  db-A.example.com to db-Z.example.com
#db-[A:Z].example.com

#[databases]
#192.168.0.103

#(comment)  Multi Group. use server & databases
#[group:children]
#sample
#databases
  • site.yml
    site.yml can use include
- name: Ansible-Sample-TDD
  hosts: server
  user: root
  roles:
    - nginx
    - mariadb

Run Test

$ rake -T
rake serverspec:Ansible-Sample-TDD  # Run serverspec for Ansible-Sample-TDD / Run serverspec for Ansible-Sample-TDD 

$ rake serverspec:Ansible-Sample-TDD
Run serverspec for Ansible-Sample-TDD to 192.168.0.103
/Users/Adr/.rvm/rubies/ruby-1.9.2-p320/bin/ruby -S rspec roles/mariadb/spec/mariadb_spec.rb roles/nginx/spec/nginx_spec.rb
...........

Finished in 0.40289 seconds
11 examples, 0 failures
Run serverspec for Ansible-Sample-TDD to 192.168.0.104
/Users/Adr/.rvm/rubies/ruby-1.9.2-p320/bin/ruby -S rspec roles/mariadb/spec/mariadb_spec.rb roles/nginx/spec/nginx_spec.rb
...........

Finished in 0.4004 seconds
11 examples, 0 failures

ansible-sample-tdd's People

Contributors

mscherer avatar okamototk avatar volanja avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ansible-sample-tdd's Issues

[QUESTION] Support for multiple inventories

I currently use static inventories with the following structure in ansible:

enviro/dev/hosts
enviro/staging/hosts
enviro/live/hosts

Any idea on how should I play around .ansiblespec in order to be able to run rake -T staging or rake -T dev?

"NoMethodError: undefined method `map' for nil:NilClass"

Hello,

With a quick test of one of my playbook, I have one working and one not.
it seems ansiblespec is not covering roles of the second group inside the playbook.

$ grep -A3 roles vpn.yml 
  roles:
    - openvpn-restorekeys
    - Stouts.openvpn
  tasks:
--
  roles:
    - openvpnclient
  tasks:
    - set_fact:

$ rake -T
rake all                               # Run serverspec to all test
rake serverspec:Ansible-vpn-satellite  # Run serverspec for Ansible-vpn-satellite
rake serverspec:Ansible-vpnserver      # Run serverspec for Ansible-vpnserver

$ rake serverspec:Ansible-vpnserver --trace
** Invoke serverspec:Ansible-vpnserver (first_time)
** Execute serverspec:Ansible-vpnserver
Run serverspec for Ansible-vpnserver to {"name"=>"HOST1 ansible_ssh_host=x.y.z.202 ansible_ssh_user=root ansible_sudo=true ansible_ssh_private_key_file=/home/user/.ssh/keys/id_rsa ansible_ssh_port=22", "port"=>22, "uri"=>"x.y.z.202", "user"=>"root", "private_key"=>"/home/user/.ssh/keys/id_rsa"}
/home/user/.rvm/rubies/ruby-2.3.0/bin/ruby -I/home/user/.rvm/gems/ruby-2.3.0/gems/rspec-support-3.5.0/lib:/home/user/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.2/lib /home/user/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.2/exe/rspec --pattern roles/\{openvpn-restorekeys,Stouts.openvpn\}/spec/\*_spec.rb

Service "openvpn"
  should be enabled

File "/usr/sbin/openvpn"
  should be executable

Finished in 0.06718 seconds (files took 0.21904 seconds to load)
2 examples, 0 failures

Run serverspec for Ansible-vpnserver to {"name"=>"HOST2 ansible_ssh_host=x.y.z.242 ansible_ssh_port=22 ansible_ssh_private_key_file=/home/user/.ssh/keys/id_rsa", "port"=>22, "uri"=>"x.y.z.242", "private_key"=>"/home/user/.ssh/keys/id_rsa"}
/home/user/.rvm/rubies/ruby-2.3.0/bin/ruby -I/home/user/.rvm/gems/ruby-2.3.0/gems/rspec-support-3.5.0/lib:/home/user/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.2/lib /home/user/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.2/exe/rspec --pattern roles/\{openvpn-restorekeys,Stouts.openvpn\}/spec/\*_spec.rb

Service "openvpn"
  should be enabled

File "/usr/sbin/openvpn"
  should be executable

Finished in 0.05259 seconds (files took 0.21698 seconds to load)
2 examples, 0 failures

$ rake serverspec:Ansible-vpn-satellite --trace
** Invoke serverspec:Ansible-vpn-satellite (first_time)
** Execute serverspec:Ansible-vpn-satellite
Run serverspec for Ansible-vpn-satellite to {"name"=>"HOST3 ansible_ssh_host=x.y.z.102 ansible_user=deploy ansible_become=true ansible_ssh_private_key_file=/home/user/.ssh/keys/id_rsa", "port"=>22, "uri"=>"x.y.z.102", "private_key"=>"/home/user/.ssh/keys/id_rsa"}
rake aborted!
NoMethodError: undefined method `map' for nil:NilClass
Did you mean?  tap
/home/user/.rvm/gems/ruby-2.3.0/gems/ansible_spec-0.2.15/lib/ansible_spec/load_ansible.rb:206:in `load_dependencies'
/opt/tmp/vagrant/homelab/Rakefile:36:in `block (5 levels) in <top (required)>'
/opt/tmp/vagrant/homelab/Rakefile:35:in `each'
/opt/tmp/vagrant/homelab/Rakefile:35:in `block (4 levels) in <top (required)>'
/home/user/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.2/lib/rspec/core/rake_task.rb:95:in `block (2 levels) in define'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/file_utils_ext.rb:58:in `verbose'
/home/user/.rvm/gems/ruby-2.3.0/gems/rspec-core-3.5.2/lib/rspec/core/rake_task.rb:94:in `block in define'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
/home/user/.rvm/rubies/ruby-2.3.0/lib/ruby/2.3.0/monitor.rb:214:in `mon_synchronize'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
/home/user/.rvm/gems/ruby-2.3.0@global/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/home/user/.rvm/rubies/ruby-2.3.0/bin/rake:23:in `load'
/home/user/.rvm/rubies/ruby-2.3.0/bin/rake:23:in `<main>'
Tasks: TOP => serverspec:Ansible-vpn-satellite

Matching Ansible Roles

Could you update your structure to match the Anible roles? It could serve as a template.

Option to change spec path?

Hello,

I'm using kitchen-test with serverspec and my directory tree for spec files is
roles/myrole/test/integration/(default|somethingelse)/serverspec

If I do a symlink as roles/myroles/spec, ansiblespec is working fine.
is it possible to add a configuration option to specfiles: test/integration/default/serverspec/*_spec.rb ?

Thanks

how to make host groups

I want to run differente test for differente host

[ubuntu]
172.23.10.22 ansible_ssh_host=172.23.10.22 ansible_ssh_user=test ansible_ssh_pass=test

[centos]
172.23.10.23 ansible_ssh_host=172.23.10.23 ansible_ssh_user=test ansible_ssh_pass=test

[other]
172.23.10.24 ansible_ssh_host=172.23.10.24 ansible_ssh_user=test ansible_ssh_pass=test

[pg]
ubuntu
centos

[mail]
other
ubuntu
centos

my site.yml

  • name: pg
    hosts: pg
    roles:
    • pg
  • name: mail
    hosts: mail
    roles:
    • mail

I want rake:pg run in ubuntu and centos hosts and rake:mail run in all hosts.

Therefore I need spec for centos are differents of test for ubuntu. It is possible? How?

[QUESTION] Support for multiple groups

I have host file like this:

 [group1]
 x.x.x.x
 x.x.x.x

 [group2]
 x.x.x.x
 x.x.x.x

 [group2]
 x.x.x.x
 x.x.x.x

 [group3]
 x.x.x.x
 x.x.x.x

 [group4]
 x.x.x.x
 x.x.x.x

And now you only can run tests with all nodes or only with a group with hosts: parameter in playbook.

- name: Ansible-Sample-TDD
  hosts: [server/all]
  user: root
  vars:
    worker_connections: 8192
  roles:
    - nginx
    - mariadb

Is possible run a test in more than one but not in all groups?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.