module AnsibleTools

Constants

VERSION

Public Class Methods

init(yml_only) click to toggle source

command ansible-tools init

# 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) click to toggle source

command ansible-tools init -r <rolename>

# 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) click to toggle source

command ansible-tools init -s

# 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_complex() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
show() click to toggle source

command ansible-tools show

# 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]

    regexp_str = Array.new
    regexp_str << "*.yml"     # search *.yml
    regexp_str << "*/*.yml"   # search */*.yml e.g. group_vars, host_vars
    regexp_str << "**/vars/*" # search roles/*/vars/*.yml
    regexp_str.each{|str|
      Dir.glob(str) {|f|
        next unless FileTest.file?(f) #skip directory
        yml = YAML.load_file(f)
        if yml == false
          puts "No Variables in #{f}"
          next
        end
        if str == "*.yml"
          yml.each{|h|
            if h.instance_of?(Hash) && h.has_key?("vars") 
              h["vars"].each{|key,value|
                table << [f,key,value]
              }
            end
          }
        else
          yml.each{|key,value|
            table << [f,key,value]
          }
        end
      }
    }

    if table.count > 0
      puts table.to_text
    end
  rescue => e
    puts e
    fail 'Sorry. Error hanppend..'
  end
end