Class: BarkingIguana::Compound::Ansible::InventoryParser

Inherits:
Object
  • Object
show all
Defined in:
lib/barking_iguana/compound/ansible/inventory_parser.rb

Class Method Summary collapse

Class Method Details

.get_inventory_param(line) ⇒ Object

param ansible_ssh_port=22 return: hash



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/barking_iguana/compound/ansible/inventory_parser.rb', line 136

def self.get_inventory_param(line)
  host = Hash.new
  # 初期値
  host['name'] = line
  host['port'] = 22
  if line.include?(":") # 192.168.0.1:22
    host['uri']  = line.split(":")[0]
    host['port'] = line.split(":")[1].to_i
    return host
  end
  # 192.168.0.1 ansible_ssh_port=22
  line.split.each{|v|
    unless v.include?("=")
      host['uri'] = v
    else
      key,value = v.split("=")
      host['port'] = value.to_i if key == "ansible_ssh_port" or key == "ansible_port"
      host['private_key'] = value if key == "ansible_ssh_private_key_file"
      host['user'] = value if key == "ansible_ssh_user" or key == "ansible_user"
      host['uri'] = value if key == "ansible_ssh_host" or key == "ansible_host"
      host['pass'] = value if key == "ansible_ssh_pass"
      host['connection'] = value if key == "ansible_connection"
    end
  }
  return host
end

.get_parent(hash, search, k) ⇒ Object

param hash href=""192.168.0.103"">server“=>, ”databases“=>, “pg:children”=>[“server”, “databases”] param search “:children” param k “pg:children” return href=""192.168.0.103"">server“=>, ”databases“=>, “pg”=>[“192.168.0.103”, “192.168.0.104”]



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/barking_iguana/compound/ansible/inventory_parser.rb', line 55

def self.get_parent(hash,search,k)
  k_parent = k.gsub(search,'')
  arry = Array.new
  hash["#{k}"].each{|group|
    next if hash["#{group}"].nil?
    arry = arry + hash["#{group}"]
  }
  h = Hash.new
  h["#{k_parent}"] = arry
  return h
end

.get_variables(host, group_idx, hosts = nil) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/barking_iguana/compound/ansible/inventory_parser.rb', line 5

def self.get_variables(host, group_idx, hosts=nil)
  vars = {}
  p = self.get_properties

  # roles default
  p[group_idx]['roles'].each do |role|
    vars = load_vars_file(vars ,"roles/#{role}/defaults/main.yml")
  end

  # all group
  vars = load_vars_file(vars ,'group_vars/all', true)

  # each group vars
  if p[group_idx].has_key?('group')
    vars = load_vars_file(vars ,"group_vars/#{p[group_idx]['group']}", true)
  end

  # each host vars
  vars = load_vars_file(vars ,"host_vars/#{host}", true)

  # site vars
  if p[group_idx].has_key?('vars')
    vars = merge_variables(vars, p[group_idx]['vars'])
  end

  # roles vars
  p[group_idx]['roles'].each do |role|
    vars = load_vars_file(vars ,"roles/#{role}/vars/main.yml")
  end

  # multiple host and children dependencies group vars
  unless hosts.nil? || p[group_idx]["hosts_childrens"].nil?
    hosts_childrens = p[group_idx]["hosts_childrens"]
    next_find_target = hosts
    while(!next_find_target.nil? && hosts_childrens.size > 0)
      vars = load_vars_file(vars ,"group_vars/#{next_find_target}", true)
      group_vars_file = find_group_vars_file(hosts_childrens,next_find_target)
      next_find_target = group_vars_file
      hosts_childrens.delete(group_vars_file)
    end
  end

  return vars

end

.load_targets(file) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/barking_iguana/compound/ansible/inventory_parser.rb', line 67

def self.load_targets(file)
  f = File.open(file).read
  groups = Hash.new
  group = ''
  hosts = Hash.new
  hosts.default = Hash.new
  f.each_line{|line|
    line = line.chomp
    # skip
    next if line.start_with?('#') #comment
    next if line.empty? == true   #null

    # get group
    if line.start_with?('[') && line.end_with?(']')
      group = line.gsub('[','').gsub(']','')
      groups["#{group}"] = Array.new
      next
    end

    # get host
    host_name = line.split[0]
    if group.empty? == false
      if groups.has_key?(line)
        groups["#{group}"] << line
        next
      elsif host_name.include?("[") && host_name.include?("]")
        # www[01:50].example.com
        # db-[a:f].example.com
        hostlist_expression(line,":").each{|h|
          host = hosts[h.split[0]]
          groups["#{group}"] << get_inventory_param(h).merge(host)
        }
        next
      else
        # 1つのみ、かつ:を含まない場合
        # 192.168.0.1
        # 192.168.0.1 ansible_ssh_host=127.0.0.1 ...
        host = hosts[host_name]
        groups["#{group}"] << get_inventory_param(line).merge(host)
        next
      end
    else
      if host_name.include?("[") && host_name.include?("]")
        hostlist_expression(line, ":").each{|h|
          hosts[h.split[0]] = get_inventory_param(h)
        }
      else
        hosts[host_name] = get_inventory_param(line)
      end
    end
  }

  # parse children [group:children]
  search = Regexp.new(":children".to_s)
  groups.keys.each{|k|
    unless (k =~ search).nil?
      # get group parent & merge parent
      groups.merge!(get_parent(groups,search,k))
      # delete group children
      if groups.has_key?("#{k}") && groups.has_key?("#{k.gsub(search,'')}")
        groups.delete("#{k}")
      end
    end
  }
  return groups
end