A lightweight Ruby wrapper around Ansible that allows for ad-hoc commands and playbook execution. The primary purpose is to support easy streaming output.
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.
Add this line to your application's Gemfile:
gem 'ansible-wrapper'
And then execute:
$ bundle
Or install it yourself as:
$ gem install ansible-wrapper
Ansible::AdHoc.run 'all -i localhost, --list-hosts'
Ansible::AdHoc.run 'all -m shell -a "echo Test" -i localhost,'
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 }
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
- For a streaming output example using Sinatra, see the examples/streaming folder.
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.
Bug reports and pull requests are welcome on GitHub at https://github.com/pgeraghty/ansible-wrapper-ruby.
The gem is available as open source under the terms of the MIT License.