Class: AnsiblePlaybook

Inherits:
Object
  • Object
show all
Defined in:
modules/ansible/db.rb

Overview

AnsiblePlaybook model implementation

Defined Under Namespace

Classes: AnsiblePlaybookSyntaxError

Constant Summary collapse

TABLE =

DB Table name

'ansible_playbook'
FIELDS =

DB Table columns names

%w(uid gid name description body extra_data create_time)
DB =

Getting table object from DB object

$db[TABLE.to_sym]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**args) ⇒ AnsiblePlaybook

Returns a new instance of AnsiblePlaybook.

Parameters:

Options Hash (**args):

  • uid (Integer)
    • Playbooks owner user ID

  • gid (Integer)
    • Playbooks group ID

  • name (String)
    • Playbooks name

  • description (String)
    • Playbooks description(only latin supported)

  • body (String)
    • Playbooks YAML written body

  • extra_data (Hash)
    • Any data you want to specify, usually only PERMISSIONS are mandatory



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'modules/ansible/db.rb', line 38

def initialize **args
  args.to_s!
  if args['id'].nil? then
    @uid, @gid, @name, @description, @body, @extra_data = args.get(*FIELDS)
    @uid, @gid, @extra_data = @uid || 0, @gid || 0, @extra_data || {}

    @extra_data['PERMISSIONS'] = @extra_data['PERMISSIONS'] || { "PERMISSIONS" => "111000000" }

    r, msg = self.class.check_syntax(@body)
    raise RuntimeError.new(msg) unless r

    @create_time = Time.now.to_i
    allocate
  else
    begin
      @id = args['id']
      sync
    rescue NoMethodError
      raise "Object not exists"
    end
  end
  raise "Unhandlable, id is nil" if @id.nil?
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



29
30
31
# File 'modules/ansible/db.rb', line 29

def body
  @body
end

#create_timeObject

Returns the value of attribute create_time.



29
30
31
# File 'modules/ansible/db.rb', line 29

def create_time
  @create_time
end

#descriptionObject

Returns the value of attribute description.



29
30
31
# File 'modules/ansible/db.rb', line 29

def description
  @description
end

#extra_dataObject

Returns the value of attribute extra_data.



29
30
31
# File 'modules/ansible/db.rb', line 29

def extra_data
  @extra_data
end

#gidObject

Returns the value of attribute gid.



29
30
31
# File 'modules/ansible/db.rb', line 29

def gid
  @gid
end

#idObject (readonly)

Returns the value of attribute id.



28
29
30
# File 'modules/ansible/db.rb', line 28

def id
  @id
end

#nameObject

Returns the value of attribute name.



29
30
31
# File 'modules/ansible/db.rb', line 29

def name
  @name
end

#uidObject

Returns the value of attribute uid.



29
30
31
# File 'modules/ansible/db.rb', line 29

def uid
  @uid
end

Class Method Details

.check_syntax(body) ⇒ Object

Note:

Playbook should be written following next rules:

Checks Playbook Syntax

  1. It must be written in ruby-yaml parse-able YAML syntax

  2. Playbook must be array (body should start from ' - ')

  3. hosts must be equal to <%group%>

  4. Using of “local_action” key is restricted

Parameters:

  • body (String)
    • Playbooks body written in YAML



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'modules/ansible/db.rb', line 123

def self.check_syntax body
  body = YAML.load(body)
  raise AnsiblePlaybookSyntaxError.new("Playbook must be array (body should start from ' - ')") unless body.class == Array
  raise AnsiblePlaybookSyntaxError.new("hosts must be equal to <%group%>") unless body.first['hosts'] == "<%group%>"
  raise AnsiblePlaybookSyntaxError.new("key local_action isn't acceptable") if body.first.has_key? 'local_action'

  return true, ""
rescue Psych::SyntaxError => e
  return false, e.message
rescue AnsiblePlaybookSyntaxError => e
  return false, e.message
rescue => e
  return false, 'Unknown error: ' + e.message
end

.listObject

Returns all AnsiblePlaybook objects from DB



181
182
183
184
185
186
187
188
# File 'modules/ansible/db.rb', line 181

def self.list
  result = DB.all
  result.map { |pb| pb.to_s! }
  result.size.times do | i |
    result[i]['extra_data'] = JSON.parse result[i]['extra_data']
  end
  result
end

.new_with_id(id, _client = nil) ⇒ AnsiblePlaybook

OpenNebula::PoolElement-like initializer

Parameters:

Returns:



70
71
72
# File 'modules/ansible/db.rb', line 70

def self.new_with_id id, _client = nil
  self.new(id: id)
end

Instance Method Details

#deleteObject

Deletes object from DB



82
83
84
85
# File 'modules/ansible/db.rb', line 82

def delete
  DB.where(id: @id).delete
  nil
end

#run(host, vars: nil, ione: IONe.new($client)) ⇒ Object

Creates AnsiblePlaybookProcess with given args host -> => ['IP_Address:ssh_port', 'username:password'] vars -> => 'value'

Examples:

Params template:

Parameters:

  • host (Hash)
    • host or hosts, whereto Playbook will be deployed, see example

  • vars (Hash) (defaults to: nil)
    • variables for playbook, see example

  • ione (IONe) (defaults to: IONe.new($client))
    • IONe client

Raises:

  • (RuntimeError)


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'modules/ansible/db.rb', line 145

def run host, vars: nil, ione: IONe.new($client)
  r, msg = self.class.check_syntax(@body)
  raise RuntimeError.new(msg) unless r

  unless vars.nil? then
    body = YAML.safe_load @body
    body[0]['vars'].merge! vars
    @body = YAML.dump body
  end
  ione.AnsibleController({
    'host' => host,
    'services' => [
      runnable
    ]
  })
end

#runnable(vars = {}) ⇒ Object

Returns Playbook body with variables inserted

Raises:

  • (RuntimeError)


163
164
165
166
167
168
169
170
171
172
173
# File 'modules/ansible/db.rb', line 163

def runnable vars = {}
  r, msg = self.class.check_syntax(@body)
  raise RuntimeError.new(msg) unless r

  unless vars == {} then
    body = YAML.safe_load @body
    body[0]['vars'].merge! vars
    @body = YAML.dump body
  end
  return { @name => @body }
end

#syncObject

Synchronizes object from DB



75
76
77
78
79
# File 'modules/ansible/db.rb', line 75

def sync
  get_me.each do |var, value|
    instance_variable_set('@' + var, value)
  end
end

#to_hashObject

Returns AnsiblePlaybook object as Hash



176
177
178
# File 'modules/ansible/db.rb', line 176

def to_hash
  get_me
end

#updateObject

Writes object to DB

Raises:

  • (RuntimeError)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'modules/ansible/db.rb', line 88

def update
  r, msg = self.class.check_syntax(@body)
  raise RuntimeError.new(msg) unless r

  args = {}
  FIELDS.each do | var |
    next if var == 'create_time'

    value = instance_variable_get(('@' + var).to_sym)
    value = var == 'extra_data' ? JSON.generate(value) : value
    args[var.to_sym] = value.nil? ? '' : value
  end
  DB.where(id: @id).update(**args)

  nil
end

#varsObject

Lists variables from Playbook



106
107
108
109
110
111
112
113
114
# File 'modules/ansible/db.rb', line 106

def vars
  sync
  body = YAML.safe_load(@body).first
  body['vars']
rescue => e
  if e.message.split(':').first == 'TypeError' then
    raise "SyntaxError: Check if here is now hyphens at the playbook beginning. Playbook parse result should be Hash"
  end
end