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 (Fixnum)
    • Playbooks owner user ID

  • gid (Fixnum)
    • 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



117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'modules/ansible/db.rb', line 117

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



172
173
174
175
176
177
178
179
# File 'modules/ansible/db.rb', line 172

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:

  • id (Fixnum)
    • Playbook ID

  • client (OpenNebula::Client) (defaults to: nil)
    • not required

Returns:



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

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

Instance Method Details

#deleteObject

Deletes object from DB



80
81
82
83
# File 'modules/ansible/db.rb', line 80

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)


138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'modules/ansible/db.rb', line 138

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.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)


155
156
157
158
159
160
161
162
163
164
165
# File 'modules/ansible/db.rb', line 155

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

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

#syncObject

Synchronizes object from DB



73
74
75
76
77
# File 'modules/ansible/db.rb', line 73

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

#to_hashObject

Returns AnsiblePlaybook object as Hash



167
168
169
# File 'modules/ansible/db.rb', line 167

def to_hash
    get_me
end

#updateObject

Writes object to DB

Raises:

  • (RuntimeError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'modules/ansible/db.rb', line 85

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



101
102
103
104
105
106
107
108
109
# File 'modules/ansible/db.rb', line 101

def vars
    sync
    body = YAML.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