• Stars
    star
    350
  • Rank 121,229 (Top 3 %)
  • Language
    Ruby
  • License
    Other
  • Created over 10 years ago
  • Updated 9 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Ansible Provisioner for Test Kitchen

kitchen-ansible

Gem Version Gem Downloads Build Status

A Test Kitchen Provisioner for Ansible.

The provisioner works by passing the Ansible repository based on attributes in .kitchen.yml & calling ansible-playbook.

It installs Ansible on the server and runs ansible-playbook using host localhost.

It has been tested against the Ubuntu 12.04/14.04/16.04, Centos 6/7 and Debian 6/7/8 boxes running in vagrant/virtualbox.

Requirements

  • Test Kitchen.
  • a driver box without a Chef installation so Ansible can be installed.

Installation & Setup

  1. install the latest Ruby on your workstation (for windows see https://rubyinstaller.org/downloads/)

  2. If using Ruby version less than 2.3 first install earlier version of test-kitchen

gem install test-kitchen -v 1.16.0
  1. Install the kitchen-ansible gem in your system, along with kitchen-vagrant or kitchen-docker or any other suitable driver or the exec driver to run from your workstation:
gem install kitchen-ansible
gem install kitchen-vagrant

Resources

Example .kitchen.yml file

Based on the Tomcat Standalone example:

---
driver:
  name: vagrant

provisioner:
  name: ansible_playbook
  roles_path: roles
  hosts: tomcat-servers
  require_ansible_repo: true
  ansible_verbose: true
  ansible_version: latest
  require_chef_for_busser: false
  additional_ssh_private_keys:
  - /mykey/id_rsa

platforms:
  - name: nocm_centos-6.5
    driver_plugin: vagrant
    driver_config:
      box: nocm_centos-6.5
      box_url: http://puppet-vagrant-boxes.puppetlabs.com/centos-65-x64-virtualbox-nocm.box
      network:
      - ['forwarded_port', {guest: 8080, host: 8080}]
      - ['private_network', {ip: '192.168.33.11'}]

See example https://github.com/neillturner/ansible_repo

Windows Support

Windows is supported by creating a linux server to run Ansible with software required to support winrm. Then the winrm connection is used to configure the windows server.

In .kitchen.yml set:

  ansible_connection: winrm
  require_windows_support: true
  require_chef_for_busser: false

See the Ansible Windows repo example.

Test Kitchen Exec Driver

By using the test-kitchen exec driver ansible can be driven from your workstation. This provides similar functionality to kitchen-ansiblepush. Remote servers, as specified in the ansible inventory, can be built with ansible automatically installed and run from your workstation.

See example https://github.com/neillturner/ansible_exec_repo

Ansible AWX

Kitchen ansible supports installing and using the open source version of Ansible Tower Ansible AWX on a Centos 7. In future it will support the tower-cli for testing.

See example https://github.com/neillturner/ansible_awx_repo

Using Roles from Ansible Galaxy

Roles can be used from the Ansible Galaxy using two methods:

  1. Specify a requirements.yml file in your Ansible repository. For more details see here.

  2. Use librarian-ansible by creating an Ansiblefile in the top level of the repository and kitchen-ansible will automatically call librarian-ansible during convergence. For a description of setting up an Ansiblefile see here.

Tips

To use a single ~/.kitchen/config.yml file with multiple reposities by setting the WORKSPACE environment variable:

role_path: <%= ENV['WORKSPACE'] %>/roles

You can easily skip previous instructions and jump directly to the broken statement you just fixed by passing an environment variable. Add the following to your .kitchen.yml:

provisioner:
  name: ansible_playbook
  ansible_extra_flags: <%= ENV['ANSIBLE_EXTRA_FLAGS'] %>

Then run:

$ ANSIBLE_EXTRA_FLAGS='--start-at-task="myrole | name of last working instruction"' kitchen converge

You save a lot of time not running working instructions.

Ruby install to run Serverspec verify

By default test-kitchen installs Chef to get a Ruby version suitable to run Serverspec in the verify step. kitchen-verifier-serverspec installs its own ruby version so chef or ruby is not required to verify with serverspec :

require_chef_for_busser: false

And set the verifier section:

verifier:
  name: serverspec
  sudo_path: true

suites:
  - name: ansible
    driver_config:
      hostname: '54.229.34.169'
    verifier:
      patterns:
      - roles/tomcat/spec/tomcat_spec.rb
      bundler_path: '/usr/local/bin'
      rspec_path: '/usr/local/bin'
      env_vars:
        TARGET_HOST: 54.229.104.40
        LOGIN_USER: centos
        SUDO: true
        SSH_KEY: spec/test.pem

Please see the Provisioner Options for a complete listing.

Test-Kitchen Ansiblespec

By using kitchen-verifier-serverspec and the Runner ansiblespec_runner tests can be run against multiple servers with multiple roles in the ansiblespec format.

Serverspec uses ssh to communicate with the server to be tested and reads the Ansible playbook and inventory files to determine the hosts to test and the roles for each host.

See example https://github.com/neillturner/ansible_ansiblespec_repo

Example usage to create Tomcat servers:

test-kitchen, Ansible and ansiblespec

See ansible-sample-tdd.

Usage

Directory

In the Ansible repository specify:

  • spec files with the roles.
  • spec_helper in the spec folder (with code as below).
.
+-- roles
¦   +-- mariadb
¦   ¦   +-- spec
¦   ¦   ¦   +-- mariadb_spec.rb
¦   ¦   +-- tasks
¦   ¦   ¦   +-- main.yml
¦   ¦   +-- templates
¦   ¦       +-- mariadb.repo
¦   +-- nginx
¦       +-- handlers
¦       ¦   +-- main.yml
¦       +-- spec
¦       ¦   +-- nginx_spec.rb
¦       +-- tasks
¦       ¦   +-- main.yml
¦       +-- templates
¦       ¦   +-- nginx.repo
¦       +-- vars
¦           +-- main.yml
+-- spec
    +-- spec_helper.rb
    +-- my_private_key.pem

spec_helper

require 'rubygems'
require 'bundler/setup'

require 'serverspec'
require 'pathname'
require 'net/ssh'

RSpec.configure do |config|
  set :host,  ENV['TARGET_HOST']
  # ssh options at http://net-ssh.github.io/ssh/v1/chapter-2.html
  # ssh via password
  set :ssh_options, :user => ENV['LOGIN_USER'], :paranoid => false, :verbose => :error, :password => ENV['LOGIN_PASSWORD'] if ENV['LOGIN_PASSWORD']
  # ssh via ssh key
  set :ssh_options, :user => ENV['LOGIN_USER'], :paranoid => false, :verbose => :error, :host_key => 'ssh-rsa', :keys => [ ENV['SSH_KEY'] ] if ENV['SSH_KEY']
  set :backend, :ssh
  set :request_pty, true
end

See kitchen-verifier-serverspec.

Alternative Virtualization/Cloud providers for Vagrant

This could be adapted to use alternative virtualization/cloud providers such as Openstack/AWS/VMware Fusion according to whatever is supported by Vagrant.

platforms:
  - name: ubuntu-12.04
    driver_config:
      provider: aws
      box: my_base_box
      # username is based on what is configured in your box/ami
      username: ubuntu
      customize:
        access_key_id: 'AKKJHG659868LHGLH'
        secret_access_key: 'G8t7o+6HLG876JGF/58'
        ami: ami-7865ab765d
        instance_type: t2.micro
        # more customisation can go here, based on what the vagrant provider supports
        #security-groups: []

Notes

  • The default in all of the above is the name of the test suite defined in the suites section of your .kitchen.yml, so if you have more than one suite of tests or change the name, you'll need to adapt the example accordingly.
  • Serverspec test files must be named _spec.rb
  • Since I'm using Vagrant, my box definitions refer to Vagrant boxes, either standard, published boxes available from Atlas or custom-created boxes (perhaps using Packer and bento, in which case you'll need to provide the URL in box_url.

More Repositories

1

kitchen-puppet

A Test Kitchen Provisioner for Puppet
Ruby
104
star
2

cfndsl_examples

Cloud Formation examples for cfndsl ruby coding
Ruby
59
star
3

terraform-aws-lambda-scheduler

Stop and start EC2 and RDS instances according to schedule via lambda and terraform
Python
54
star
4

kitchen-verifier-serverspec

A Test Kitchen Serverspec Verifer without having to transit the Busser layer
Ruby
39
star
5

omnibus-ansible

Install latest Ansible via pip + dependencies via a shell script
Shell
27
star
6

terraform-github-actions

terraform github action workflow example with tests
HCL
24
star
7

kitchen-ssh

ssh and ssh_gzip driver for test-kitchen for any running server with an ip address
Ruby
21
star
8

ec2dream

Build and Manage Cloud Servers - Agile DevOps for the Cloud
Ruby
20
star
9

ansible_repo

ansible_repo for testing ansible using test kitchen
Ruby
17
star
10

kitchen-cloudformation

A Test Kitchen Driver for Amazon AWS CloudFormation.
Ruby
16
star
11

kitchen-verifier-awspec

test kitchen verifier for awspec
Ruby
16
star
12

knife-cfn

CloudFormation Support for Chef's Knife Command
Ruby
10
star
13

terraform-aws-centralised-logs

Centralised logging using AWS elasticsearch service, lambda and cloudwatch logs
HCL
6
star
14

terraform-aws-autospotting

Automatically convert your existing AutoScaling groups to significantly cheaper spot instances with minimal(often zero) configuration changes
HCL
6
star
15

terraform-aws-elb-logs-to-elasticsearch

Send ELB logs from S3 bucket to ElasticSearch using AWS Lambda
HCL
5
star
16

puppet-pm2

Puppet Module to deploy a nodejs application using PM2
Shell
4
star
17

terraform-aws-alb-logs-to-elasticsearch

Send ALB logs from S3 bucket to ElasticSearch using AWS Lambda
HCL
4
star
18

aws-alb-logs-to-elasticsearch

JavaScript
4
star
19

cloudwatch-monitoring

Install Amazon AWS Cloud Watch Monitoring Scripts
Ruby
4
star
20

cross-account-managed-prometheus

HCL
4
star
21

ansible_ansiblespec_repo

ansible repo for testing ansible using test kitchen
Ruby
4
star
22

terraform-aws-adclient

Create Active Directory Client instance to manage AWS Directory Service
HCL
3
star
23

terraform-aws-microsoftad

Create a Microsoft Active Directory AWS Directory Service
HCL
2
star
24

terraform-aws-populate_nlb_tg_with_rds

populate network load balancer target group with RDS IP address. This can be in fact any DNS resolvable to an instance like redshift DNS.
Python
2
star
25

terraform-aws-simplead

Create a Simple Active Directory AWS Directory Service
HCL
2
star
26

terraform-aws-lambda-es-cleanup

AWS Elasticsearch Lambda Curator
HCL
2
star
27

kitchen-softlayer

test kitchen driver for softlayer
Ruby
2
star
28

flux-examples

Flux V2 kubernetes examples
2
star
29

puppet-cloudwatch_monitoring

reports custom metric data about Linux performance to Amazon CloudWatch
Shell
1
star
30

ansible_windows_repo

ansible repo for windows support
1
star
31

lambda_aws_scheduler

DEPRECATED: See terraform-aws-lambda-scheduler
Python
1
star
32

ansible_exec_repo

Ansible Repository to demonstrate using kitchen-ansible with exec driver
Ruby
1
star
33

puppet_docker_repo

Puppet Repository to demonstrate using kitchen-puppet with Docker
Puppet
1
star
34

terraform-aws-ssm-adjoin

SSM Document to join Instance to AD Domain of AWS Directory Service
HCL
1
star
35

serverspec-runners

Runner scripts for kitchen-verifier-serverspec
Ruby
1
star
36

kitchen-gzip

Test Kitchen gzip transport plugin to speed up tests.
1
star
37

ansible_awx_repo

Ansible Repository to demonstrate using kitchen-ansible with Ansible AWX and vagrant driver
1
star
38

puppet_repo

Puppet Repo with serverspec acceptance tests via ssh to a server.
Puppet
1
star
39

cloudformation_repo

kitchen-cloudformation example repository
1
star
40

puppet_vagrant_repo

Puppet Repository to demonstrate using kitchen-puppet with Vagrant
Puppet
1
star
41

chef-nano

test-kitchen with chef and Windows Nano Server
Ruby
1
star
42

aws_helper

Aws Helper for an instance - Allows functions on EBS volumes, snapshots, IP addresses and more
Ruby
1
star
43

terraform-list-backends

List Status of Terraform States in Backend AWS S3
Go
1
star
44

ansible_vagrant_repo

Ansible Repository to demonstrate using kitchen-ansible with Vagrant
Ruby
1
star
45

aws-cfn-scheduler

Creates and deletes cloudformation stacks using a JSON based schedule
Python
1
star
46

puppet-aws_scheduler

Install aws scheduler - stop and start server instances according to schedule
HTML
1
star