Skip to content

pgeraghty/ansible-wrapper-ruby

Repository files navigation

Ansible Wrapper

Gem Version Build Status Coverage Status Code Climate Documentation

A lightweight Ruby wrapper around Ansible that allows for ad-hoc commands and playbook execution. The primary purpose is to support easy streaming output.

Requirements

Ensure Ansible is installed and available to shell commands i.e. in PATH. Tested with Ansible versions 2.0.2 thru 2.9.6 and Ruby 2.1+, but please create an issue if you use a version that fails.

Installation

Add this line to your application's Gemfile:

gem 'ansible-wrapper'

And then execute:

$ bundle

Or install it yourself as:

$ gem install ansible-wrapper

Usage

Ad-hoc commands

Ansible::AdHoc.run 'all -i localhost, --list-hosts'
Ansible::AdHoc.run 'all -m shell -a "echo Test" -i localhost,'

Playbooks

Ansible::Playbook.run '-i localhost, spec/fixtures/mock_playbook.yml'
Ansible::Playbook.stream('-i localhost, spec/fixtures/mock_playbook.yml') # defaults to standard output
Ansible::Playbook.stream('-i localhost, spec/fixtures/mock_playbook.yml') { |line_of_output| puts line_of_output }

Shortcuts

To enable shortcuts:

Ansible.enable_shortcuts!

You can then access Ansible via the A alias and use the following syntax:

A['all -i localhost, --list-hosts'] # alias for Ansible::AdHoc.run
A << '-i localhost, spec/fixtures/mock_playbook.yml' # alias for Ansible::Playbook.stream

Examples

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake rspec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/pgeraghty/ansible-wrapper-ruby.

License

The gem is available as open source under the terms of the MIT License.