ansible_module 0.9.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 25454d518dee3e7062ee9c41cf9af20707e5bacd
4
+ data.tar.gz: b332710c5fe099b3867b6a30f2e84070575230e2
5
+ SHA512:
6
+ metadata.gz: 005c884025fcfaf5cb9b0a5902d835fa72dd4bf3aa198fd092dec44aadc29a217861cbec8f3e161d2cb3dc51b61311829fb112eeb9b6b6952a30cc67424014c8
7
+ data.tar.gz: a275f040175cb221ee1373dc037c6f7260338d715d3a80bc7f0175310aa792634f9c22413e4d48c61a0084ca35ba5a10635a7b6a3efc732d9317805c84e3c292
@@ -0,0 +1,7 @@
1
+ CHANGELOG
2
+ =========
3
+
4
+ ## 0.9.0 (2014-08-27)
5
+
6
+ * Implement basic instance methods: main, run, exit_json, fail_json
7
+ * Implement basic class methods: instance, params
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 Tsutomu Kuroda
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,62 @@
1
+ AnsibleModule
2
+ =============
3
+
4
+ AnsibleModule is a Ruby class that provides basic functionalities as an [Ansible](http://ansible.com) module.
5
+
6
+ It is distributed as a gem package under the MIT license.
7
+
8
+ Installation
9
+ ------------
10
+
11
+ Add the following to your Ansible playbook:
12
+
13
+ ```yaml
14
+ - hosts: web-servers
15
+ tasks:
16
+ - name: Install ansible_module gem
17
+ gem: name=ansible_module user_install=false state=present
18
+ ```
19
+
20
+ Note that you should install a Ruby (2.0 or later) on your hosts.
21
+
22
+ Synopsis
23
+ --------
24
+
25
+ Create the following Ruby script as `library/calc`:
26
+
27
+ ```ruby
28
+ require 'ansible_module'
29
+
30
+ class Calc < AnsibleModule
31
+ attribute :x, Integer
32
+ attribute :y, Integer, default: 100
33
+
34
+ validates :x, presence: true, numericality: { only_integer: true }
35
+ validates :y, numericality: { only_integer: true }
36
+
37
+ def main
38
+ sum = x + y
39
+
40
+ exit_json(x: x, y: y, sum: sum, changed: true)
41
+ end
42
+ end
43
+
44
+ Calc.instance.run
45
+ ```
46
+
47
+ Add the following to your Ansible playbook:
48
+
49
+ ```yaml
50
+ - hosts: web-servers
51
+ tasks:
52
+ - name: Make a calculation
53
+ calc: x=50 y=50
54
+ register: result
55
+ - debug: >
56
+ msg="sum = {{ result['sum'] }}"
57
+ ```
58
+
59
+ License
60
+ -------
61
+
62
+ AnsibleModule is distributed under the MIT license. ([MIT-LICENCE](MIT-LICENCE))
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.9.0
@@ -0,0 +1,59 @@
1
+ require 'shellwords'
2
+ require 'json'
3
+ require 'virtus'
4
+ require 'active_support/all'
5
+ require 'active_model'
6
+
7
+ class AnsibleModule
8
+ include Virtus.model
9
+ include ActiveModel::Validations
10
+
11
+ def main
12
+ raise "Not implemented."
13
+ end
14
+
15
+ def run
16
+ if valid?
17
+ main
18
+ else
19
+ fail_json(msg: errors.full_messages.join(' '))
20
+ end
21
+ rescue StandardError => e
22
+ fail_json(msg: "Failed: #{e.to_s}")
23
+ end
24
+
25
+ private
26
+
27
+ def exit_json(hash)
28
+ hash = ActiveSupport::HashWithIndifferentAccess.new(hash)
29
+ print JSON.dump(hash)
30
+ exit 0
31
+ end
32
+
33
+ def fail_json(hash)
34
+ hash = ActiveSupport::HashWithIndifferentAccess.new(hash)
35
+ hash[:failed] = true
36
+ hash[:msg] ||= "No error message."
37
+ print JSON.dump(hash)
38
+ exit 1
39
+ end
40
+
41
+ class << self
42
+ def instance
43
+ @instance ||= new(params)
44
+ @instance
45
+ end
46
+
47
+ def params
48
+ return @params if @params
49
+ @params = ActiveSupport::HashWithIndifferentAccess.new
50
+ File.open(ARGV[0]) do |fh|
51
+ fh.read().shellsplit.each do |word|
52
+ (key, value) = word.split('=', 2)
53
+ @params[key] = value
54
+ end
55
+ end
56
+ @params
57
+ end
58
+ end
59
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ansible_module
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.0
5
+ platform: ruby
6
+ authors:
7
+ - Tsutomu KURODA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: json
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.8.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.8.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: virtus
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.3
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.3
41
+ - !ruby/object:Gem::Dependency
42
+ name: activesupport
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 4.1.5
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 4.1.5
55
+ - !ruby/object:Gem::Dependency
56
+ name: activemodel
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 4.1.5
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 4.1.5
69
+ description: AnsibleModule is a Ruby class that provides basic functionalities as
70
+ an Ansible module.
71
+ email: t-kuroda@oiax.jp
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - README.md
77
+ - CHANGELOG.md
78
+ - MIT-LICENSE
79
+ - VERSION
80
+ - lib/ansible_module.rb
81
+ homepage: https://github.com/kuroda/ansible_module
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - '>='
92
+ - !ruby/object:Gem::Version
93
+ version: 2.0.0
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.1.10
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: AnsibleModule class for Ruby language.
105
+ test_files: []