Home

Awesome

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.)

[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
- 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