ansible_module 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +7 -2
  3. data/README.md +138 -10
  4. data/VERSION +1 -1
  5. data/lib/ansible_module.rb +7 -1
  6. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 25454d518dee3e7062ee9c41cf9af20707e5bacd
4
- data.tar.gz: b332710c5fe099b3867b6a30f2e84070575230e2
3
+ metadata.gz: d6355d7da66ac0cb19bc372a9733fba816217b87
4
+ data.tar.gz: 4f34188771a2276f2397609b3540cabcb79c55cd
5
5
  SHA512:
6
- metadata.gz: 005c884025fcfaf5cb9b0a5902d835fa72dd4bf3aa198fd092dec44aadc29a217861cbec8f3e161d2cb3dc51b61311829fb112eeb9b6b6952a30cc67424014c8
7
- data.tar.gz: a275f040175cb221ee1373dc037c6f7260338d715d3a80bc7f0175310aa792634f9c22413e4d48c61a0084ca35ba5a10635a7b6a3efc732d9317805c84e3c292
6
+ metadata.gz: ab78c4fb84571570587976d59dfbf3a3ad9f076d2b8fe61210ecf94ba6cf81b067118dd9dc88d6ff6c5e48d29cb29a8203fc564ec0f84a20d0c5c683ece8400a
7
+ data.tar.gz: ed41b1780a6439bfff9a3fe88d0418b5d42887380814af2be2efa9ba11ef9c10270599843b6df554d90dc51a744852fb6e9a9fb0fad67cf785c26d9da59e58af
data/CHANGELOG.md CHANGED
@@ -1,7 +1,12 @@
1
1
  CHANGELOG
2
2
  =========
3
3
 
4
+ ## 0.9.1 (2014-08-28)
5
+
6
+ * Add private method `AnsibleModule#invalid_json` to improve the message on
7
+ validation failure.
8
+
4
9
  ## 0.9.0 (2014-08-27)
5
10
 
6
- * Implement basic instance methods: main, run, exit_json, fail_json
7
- * Implement basic class methods: instance, params
11
+ * Implement basic instance methods: `main`, `run`, `exit_json`, `fail_json`
12
+ * Implement basic class methods: `instance`, `params`
data/README.md CHANGED
@@ -1,30 +1,63 @@
1
1
  AnsibleModule
2
2
  =============
3
3
 
4
+ [![Gem Version](https://badge.fury.io/rb/ansible_module.svg)](http://badge.fury.io/rb/ansible_module)
5
+
4
6
  AnsibleModule is a Ruby class that provides basic functionalities as an [Ansible](http://ansible.com) module.
5
7
 
6
- It is distributed as a gem package under the MIT license.
8
+ It is distributed as a gem package under the [MIT-LICENSE](MIT-LICENSE).
7
9
 
8
10
  Installation
9
11
  ------------
10
12
 
11
- Add the following to your Ansible playbook:
13
+ ### Manual Installation
14
+
15
+ Install Ruby (2.0 or later) and `ansible_module` gem on your *remote* hosts.
16
+
17
+ The following is a typical procedure on Ubuntu Server 14.04:
18
+
19
+ ```
20
+ $ sudo add-apt-repository -y ppa:brightbox/ruby-ng
21
+ $ sudo apt-get update
22
+ $ sudo apt-get -y install ruby2.1
23
+ $ sudo gem install ansible_module
24
+ ```
25
+
26
+ ### Installation with Ansible
27
+
28
+ Create an Ansible [playbook](http://docs.ansible.com/playbooks.html) to install Ruby and `ansible_module` gem.
29
+
30
+ The following is an example for Ubuntu Server 14.04:
12
31
 
13
32
  ```yaml
14
- - hosts: web-servers
33
+ - hosts: servers
34
+ sudo: yes
15
35
  tasks:
36
+ - name: Add ppa for ruby
37
+ apt_repository: repo='ppa:brightbox/ruby-ng' state=present
38
+ - name: Install ruby 2.1
39
+ apt: name=ruby2.1 state=present
16
40
  - name: Install ansible_module gem
17
41
  gem: name=ansible_module user_install=false state=present
18
42
  ```
19
43
 
20
- Note that you should install a Ruby (2.0 or later) on your hosts.
44
+ If you named this file `ruby_environment.yml`, then run the following command on your *local* host:
45
+
46
+ ```
47
+ $ ansible-playbook -i hosts ruby_environment.yml
48
+ ```
49
+
50
+ In the above example, the `hosts` file is an [inventory](http://docs.ansible.com/intro_inventory.html) which lists up host names or IP addresses.
51
+
21
52
 
22
- Synopsis
23
- --------
53
+ Example (1)
54
+ -----------
24
55
 
25
- Create the following Ruby script as `library/calc`:
56
+ Create a file named `calc` on the `library` directory as follows:
26
57
 
27
58
  ```ruby
59
+ #!/usr/bin/ruby
60
+
28
61
  require 'ansible_module'
29
62
 
30
63
  class Calc < AnsibleModule
@@ -44,10 +77,14 @@ end
44
77
  Calc.instance.run
45
78
  ```
46
79
 
47
- Add the following to your Ansible playbook:
80
+ The values of attributes `x` and `y` are set during instantiation process by `AnsibleModule`.
81
+
82
+ Note that you can validate them with `validates` class method derived from `ActiveModel`.
83
+
84
+ Then, create a file named `calc.yml` as follows:
48
85
 
49
86
  ```yaml
50
- - hosts: web-servers
87
+ - hosts: servers
51
88
  tasks:
52
89
  - name: Make a calculation
53
90
  calc: x=50 y=50
@@ -56,7 +93,98 @@ Add the following to your Ansible playbook:
56
93
  msg="sum = {{ result['sum'] }}"
57
94
  ```
58
95
 
96
+ And run the following command on your local host:
97
+
98
+ ```
99
+ $ ansible-playbook -i hosts calc.yml
100
+ ```
101
+
102
+
103
+ Example (2)
104
+ -----------
105
+
106
+ Create a file named `mysql_change_master` on the `library` directory as follows:
107
+
108
+ ```ruby
109
+ #!/usr/bin/ruby
110
+
111
+ require 'ansible_module'
112
+
113
+ class MysqlChangeMaster < AnsibleModule
114
+ attribute :host, String
115
+ attribute :port, Integer, default: 3306
116
+ attribute :user, String
117
+ attribute :password, String
118
+ attribute :mysql_root_password, String
119
+
120
+ validates :port, inclusion: { in: 0..65535 }
121
+
122
+ def main
123
+ statement = %Q{
124
+ STOP SLAVE;
125
+ CHANGE MASTER TO
126
+ MASTER_HOST='#{host}',
127
+ MASTER_PORT=#{port},
128
+ MASTER_USER='#{user}',
129
+ MASTER_PASSWORD='#{password}',
130
+ MASTER_AUTO_POSITION=1;
131
+ START SLAVE;
132
+ }.squish
133
+
134
+ command = %Q{
135
+ /usr/bin/mysql -u root -p#{mysql_root_password} -e "#{statement}"
136
+ }.squish
137
+
138
+ system(command)
139
+
140
+ exit_json(statement: statement, changed: true)
141
+ end
142
+ end
143
+
144
+ MysqlChangeMaster.instance.run
145
+ ```
146
+
147
+ Note that you can use methods added by `ActiveSupport` like `String#squish`.
148
+
149
+ Then, create a file named `replication.yml` as follows:
150
+
151
+ ```yaml
152
+ - hosts: mysql-slave
153
+ vars_files:
154
+ - shared/secret.yml
155
+ tasks:
156
+ - name: Change master to the db1
157
+ mysql: >
158
+ host="db1"
159
+ user="repl"
160
+ password="{{ mysql_repl_password }}"
161
+ mysql_root_password="{{ mysql_root_password }}"
162
+ ```
163
+
164
+ Next, create a file named `secret.yml` on the `shared` directory as follows:
165
+
166
+ ```secret.yml
167
+ mysql_repl_password: p@ssw0rd
168
+ mysql_root_password: p@ssw0rd
169
+ ```
170
+
171
+ Note that you should replace `p@ssw0rd` with real passwords.
172
+
173
+ And run the following command on your local host:
174
+
175
+ ```
176
+ $ ansible-playbook -i hosts replication.yml
177
+ ```
178
+
179
+ You might want to encrypt the `secret.yml` with [ansible-vault](http://docs.ansible.com/playbooks_vault.html).
180
+ In that case, you must add `--ask-vault-pass` option to the above command:
181
+
182
+ ```
183
+ $ ansible-playbook -i hosts --ask-vault-pass replication.yml
184
+ ```
185
+
186
+
59
187
  License
60
188
  -------
61
189
 
62
- AnsibleModule is distributed under the MIT license. ([MIT-LICENCE](MIT-LICENCE))
190
+ AnsibleModule is distributed under the [MIT-LICENSE](MIT-LICENSE).
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.0
1
+ 0.9.1
@@ -16,7 +16,7 @@ class AnsibleModule
16
16
  if valid?
17
17
  main
18
18
  else
19
- fail_json(msg: errors.full_messages.join(' '))
19
+ invalid_json
20
20
  end
21
21
  rescue StandardError => e
22
22
  fail_json(msg: "Failed: #{e.to_s}")
@@ -38,6 +38,12 @@ class AnsibleModule
38
38
  exit 1
39
39
  end
40
40
 
41
+ def invalid_json
42
+ message = 'Invalid parameters: '
43
+ message += errors.full_messages.map { |m| "#{m}." }.join(' ')
44
+ fail_json(msg: message)
45
+ end
46
+
41
47
  class << self
42
48
  def instance
43
49
  @instance ||= new(params)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ansible_module
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tsutomu KURODA
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-27 00:00:00.000000000 Z
11
+ date: 2014-08-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json