Module: AnsibleTools

Defined in:
lib/ansible_tools.rb,
lib/ansible_tools/version.rb

Defined Under Namespace

Classes: TermColor

Constant Summary collapse

VERSION =
"0.0.4.2"

Class Method Summary collapse

Class Method Details

.init(yml_only) ⇒ Object

command ansible-tools init



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ansible_tools.rb', line 9

def self.init(yml_only)
  simple = yml_only ? safe_list_yml_only('common') : safe_list_simple('common')
  complex = safe_list_complex()
  # dir
  simple[:dir].each { |dir| safe_mkdir(dir) }
  complex[:dir].each { |dir| safe_mkdir(dir) }
  # file
  simple[:file].each { |file| safe_touch(file) }
  complex[:file].each { |file| safe_touch(file) } unless yml_only

end

.init_role(name, yml_only) ⇒ Object

command ansible-tools init -r <rolename>



31
32
33
34
35
36
37
# File 'lib/ansible_tools.rb', line 31

def self.init_role(name, yml_only)
  role = yml_only ? safe_list_yml_only("#{name}") : safe_list_simple("#{name}")
  # dir
  role[:dir].each { |dir| safe_mkdir(dir) }
  # file
  role[:file].each { |file| safe_touch(file) }
end

.init_simple(yml_only) ⇒ Object

command ansible-tools init -s



22
23
24
25
26
27
28
# File 'lib/ansible_tools.rb', line 22

def self.init_simple(yml_only)
  simple = yml_only ? safe_list_yml_only('common') : safe_list_simple('common')
  # dir
  simple[:dir].each { |dir| safe_mkdir(dir) }
  # file
  simple[:file].each { |file| safe_touch(file) }
end

.safe_list_complexObject



80
81
82
83
84
85
86
87
# File 'lib/ansible_tools.rb', line 80

def self.safe_list_complex()
  dir = Array.new
  group = 'group_vars'
  host = 'host_vars'
  dir = [group, host]
  file = ["production", "stage", "#{group}/group1", "#{group}/group2", "#{host}/hostname1", "#{host}/hostname2"]
  return {:dir => dir, :file => file}
end

.safe_list_simple(role) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ansible_tools.rb', line 39

def self.safe_list_simple(role)
  dir = Array.new
  dir_role = "roles/#{role}"
  tasks = "#{dir_role}/tasks"
  handlers =  "#{dir_role}/handlers"
  templates = "#{dir_role}/templates"
  vars = "#{dir_role}/vars"
  files = "#{dir_role}/files"
  dir = [tasks,handlers,templates,vars,files]

  file = Array.new
  site = 'site.yml'
  f_task = "#{tasks}/main.yml"
  f_handlers = "#{handlers}/main.yml"
  f_templates = "#{templates}/foo.conf.j2"
  f_vars = "#{vars}/main.yml"
  f_file = "#{files}/bar.txt"
  file = [site,f_task,f_handlers,f_templates, f_vars, f_file]
  return {:dir => dir, :file => file}
end

.safe_list_yml_only(role) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/ansible_tools.rb', line 60

def self.safe_list_yml_only(role)
  dir = Array.new
  dir_role = "roles/#{role}"
  tasks = "#{dir_role}/tasks"
  handlers =  "#{dir_role}/handlers"
  templates = "#{dir_role}/templates"
  vars = "#{dir_role}/vars"
  files = "#{dir_role}/files"
  dir = [tasks,handlers,templates,vars,files]

  file = Array.new
  site = 'site.yml'
  f_task = "#{tasks}/main.yml"
  f_handlers = "#{handlers}/main.yml"
  f_vars = "#{vars}/main.yml"
  file = [site,f_task,f_handlers, f_vars]
  return {:dir => dir, :file => file}
end

.safe_mkdir(dir) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/ansible_tools.rb', line 89

def self.safe_mkdir(dir)
  unless FileTest.exist?("#{dir}")
    FileUtils.mkdir_p("#{dir}")
    TermColor.green
    puts "\t\tcreate\t#{dir}"
    TermColor.reset
  else
    TermColor.red
    puts "\t\texists\t#{dir}"
    TermColor.reset
  end
end

.safe_touch(file) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ansible_tools.rb', line 102

def self.safe_touch(file)
  unless File.exists? "#{file}"
    File.open("#{file}", 'w') do |f|
        #f.puts content
    end
    TermColor.green
    puts "\t\tcreate\t#{file}"
    TermColor.reset
  else
    TermColor.red
    puts "\t\texists\t#{file}"
    TermColor.reset
  end
end

.showObject

command ansible-tools show



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/ansible_tools.rb', line 118

def self.show()
  begin
    if Dir.glob("**/vars/*").count == 0
      puts 'Not Found'
      exit 1
    end
    table = Ruport::Data::Table.new
    table.column_names = %w[File Key Value]

    # search *.yml
    Dir.glob("*.yml") {|f|
      next unless FileTest.file?(f) #skip directory
      yml = YAML.load_file(f)
      if yml == false
        puts "No Variables in #{f}"
        next
      end
      yml.each{|h|
        h["vars"].each{|key,value|
          table << [f,key,value]
        }
      }
    }
    # search */*.yml e.g. group_vars, host_vars
    Dir.glob("*/*.yml") {|f|
      next unless FileTest.file?(f) #skip directory
      yml = YAML.load_file(f)
      if yml == false
        puts "No Variables in #{f}"
        next
      end
      yml.each{|key,value|
        table << [f,key,value]
      }
    }
    # search roles/*/vars/*.yml
    Dir.glob("**/vars/*") {|f|
      next unless FileTest.file?(f) #skip directory
      yml = YAML.load_file(f)
      if yml == false
        puts "No Variables in #{f}"
        next
      end
      yml.each{|key,value|
        table << [f,key,value]
      }
    }
    if table.count > 0
      puts table.to_text
    end
  rescue
    puts 'Sorry. Error hanppend..'
    exit 1
  end
end