├── Rakefile ├── .rspec ├── lib ├── ansible_tools │ └── version.rb └── ansible_tools.rb ├── .coveralls.yml ├── Gemfile ├── .travis.yml ├── .gitignore ├── spec ├── lists │ ├── init_role_true.yml │ ├── init_simple_true.yml │ ├── init_true.yml │ ├── init_role_false.yml │ ├── init_simple_false.yml │ └── init_false.yml ├── commands_spec.rb └── spec_helper.rb ├── bin └── ansible-tools ├── LICENSE.txt ├── ansible_tools.gemspec └── README.md /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --require spec_helper 3 | -------------------------------------------------------------------------------- /lib/ansible_tools/version.rb: -------------------------------------------------------------------------------- 1 | module AnsibleTools 2 | VERSION = "0.0.4.3" 3 | end 4 | -------------------------------------------------------------------------------- /.coveralls.yml: -------------------------------------------------------------------------------- 1 | service_name: travis-ci 2 | repo_token: Dus6yjl3TGYJvy2BWKuDl5CWDbv5LTqQk 3 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | # Specify your gem's dependencies in ansible_tools.gemspec 4 | gemspec 5 | 6 | gem 'coveralls', require: false 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | sudo: false 3 | rvm: 4 | - 1.9.3 5 | - 2.0.0 6 | - 2.1.10 7 | - 2.2.9 8 | - 2.3.6 9 | - 2.4.3 10 | - 2.5.0 11 | script: 12 | - rspec spec 13 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | *.rbc 3 | .bundle 4 | .config 5 | .yardoc 6 | Gemfile.lock 7 | InstalledFiles 8 | _yardoc 9 | coverage 10 | doc/ 11 | lib/bundler/man 12 | pkg 13 | rdoc 14 | spec/reports 15 | test/tmp 16 | test/version_tmp 17 | tmp 18 | -------------------------------------------------------------------------------- /spec/lists/init_role_true.yml: -------------------------------------------------------------------------------- 1 | "file": 2 | - "site.yml" 3 | - "roles/gitlab/tasks/main.yml" 4 | - "roles/gitlab/handlers/main.yml" 5 | - "roles/gitlab/vars/main.yml" 6 | "dir": 7 | - "roles" 8 | - "roles/gitlab" 9 | - "roles/gitlab/tasks" 10 | - "roles/gitlab/handlers" 11 | - "roles/gitlab/templates" 12 | - "roles/gitlab/vars" 13 | - "roles/gitlab/files" 14 | -------------------------------------------------------------------------------- /spec/lists/init_simple_true.yml: -------------------------------------------------------------------------------- 1 | "file": 2 | - "site.yml" 3 | - "roles/common/tasks/main.yml" 4 | - "roles/common/handlers/main.yml" 5 | - "roles/common/vars/main.yml" 6 | "dir": 7 | - "roles" 8 | - "roles/common" 9 | - "roles/common/tasks" 10 | - "roles/common/handlers" 11 | - "roles/common/templates" 12 | - "roles/common/vars" 13 | - "roles/common/files" 14 | -------------------------------------------------------------------------------- /spec/lists/init_true.yml: -------------------------------------------------------------------------------- 1 | "file": 2 | - "group_vars" 3 | - "host_vars" 4 | - "site.yml" 5 | - "roles/common/tasks/main.yml" 6 | - "roles/common/handlers/main.yml" 7 | - "roles/common/vars/main.yml" 8 | "dir": 9 | - "roles" 10 | - "roles/common" 11 | - "roles/common/tasks" 12 | - "roles/common/handlers" 13 | - "roles/common/templates" 14 | - "roles/common/vars" 15 | - "roles/common/files" 16 | -------------------------------------------------------------------------------- /spec/lists/init_role_false.yml: -------------------------------------------------------------------------------- 1 | "file": 2 | - "site.yml" 3 | - "roles/gitlab/tasks/main.yml" 4 | - "roles/gitlab/handlers/main.yml" 5 | - "roles/gitlab/templates/foo.conf.j2" 6 | - "roles/gitlab/vars/main.yml" 7 | - "roles/gitlab/files/bar.txt" 8 | "dir": 9 | - "roles" 10 | - "roles/gitlab" 11 | - "roles/gitlab/tasks" 12 | - "roles/gitlab/handlers" 13 | - "roles/gitlab/templates" 14 | - "roles/gitlab/vars" 15 | - "roles/gitlab/files" 16 | -------------------------------------------------------------------------------- /spec/lists/init_simple_false.yml: -------------------------------------------------------------------------------- 1 | "file": 2 | - "site.yml" 3 | - "roles/common/tasks/main.yml" 4 | - "roles/common/handlers/main.yml" 5 | - "roles/common/templates/foo.conf.j2" 6 | - "roles/common/vars/main.yml" 7 | - "roles/common/files/bar.txt" 8 | "dir": 9 | - "roles" 10 | - "roles/common" 11 | - "roles/common/tasks" 12 | - "roles/common/handlers" 13 | - "roles/common/templates" 14 | - "roles/common/vars" 15 | - "roles/common/files" 16 | -------------------------------------------------------------------------------- /spec/lists/init_false.yml: -------------------------------------------------------------------------------- 1 | "file": 2 | - "group_vars" 3 | - "host_vars" 4 | - "site.yml" 5 | - "roles/common/tasks/main.yml" 6 | - "roles/common/handlers/main.yml" 7 | - "roles/common/templates/foo.conf.j2" 8 | - "roles/common/vars/main.yml" 9 | - "roles/common/files/bar.txt" 10 | - "production" 11 | - "stage" 12 | - "group_vars/group1" 13 | - "group_vars/group2" 14 | - "host_vars/hostname1" 15 | - "host_vars/hostname2" 16 | "dir": 17 | - "roles" 18 | - "roles/common" 19 | - "roles/common/tasks" 20 | - "roles/common/handlers" 21 | - "roles/common/templates" 22 | - "roles/common/vars" 23 | - "roles/common/files" 24 | - "group_vars" 25 | - "host_vars" 26 | -------------------------------------------------------------------------------- /bin/ansible-tools: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env ruby 2 | # coding: utf-8 3 | 4 | require 'ansible_tools' 5 | require 'rubygems' 6 | require 'thor' 7 | 8 | class Cli < Thor 9 | 10 | desc "init [-s][-r][-y]", "create ansible files by BestPractice" 11 | option :simple, :type => :boolean, :aliases => '-s', :desc => "simple-mode, create only setup.yml, roles/common" 12 | option :role, :type => :string, :aliases => '-r', :desc => "create roles/ROLE" 13 | option :"yml-only", :type => :boolean, :aliases => '-y', :desc => "create directory and yml" 14 | def init 15 | if options[:simple] 16 | AnsibleTools.init_simple(options[:"yml-only"]) 17 | elsif options[:role] 18 | AnsibleTools.init_role("#{options[:role]}", options[:"yml-only"]) 19 | else 20 | AnsibleTools.init(options[:"yml-only"]) 21 | end 22 | end 23 | 24 | desc "show", "show ansible valiables in vars/main.yml" 25 | def show 26 | AnsibleTools.show 27 | end 28 | 29 | desc "version", "show version" 30 | def version 31 | puts AnsibleTools::VERSION 32 | end 33 | end 34 | 35 | Cli.start 36 | 37 | exit 0 38 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 volanja 2 | 3 | MIT License 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining 6 | a copy of this software and associated documentation files (the 7 | "Software"), to deal in the Software without restriction, including 8 | without limitation the rights to use, copy, modify, merge, publish, 9 | distribute, sublicense, and/or sell copies of the Software, and to 10 | permit persons to whom the Software is furnished to do so, subject to 11 | the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be 14 | included in all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 17 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 19 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 20 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 21 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 22 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 | -------------------------------------------------------------------------------- /ansible_tools.gemspec: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | lib = File.expand_path('../lib', __FILE__) 3 | $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) 4 | require 'ansible_tools/version' 5 | 6 | Gem::Specification.new do |spec| 7 | spec.name = "ansible_tools" 8 | spec.version = AnsibleTools::VERSION 9 | spec.authors = ["volanja"] 10 | spec.email = ["volaaanja@gmail.com"] 11 | spec.description = %q{Ansible Tools e.g. Create directory by BestPractice} 12 | spec.summary = %q{Ansible Tools e.g. Create directory by BestPractice} 13 | spec.homepage = "https://github.com/volanja" 14 | spec.license = "MIT" 15 | 16 | spec.files = `git ls-files`.split($/) 17 | spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } 18 | spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) 19 | spec.require_paths = ["lib"] 20 | 21 | spec.add_development_dependency "bundler", "~> 1.3" 22 | spec.add_development_dependency "rake" 23 | spec.add_development_dependency "rspec" 24 | 25 | spec.add_runtime_dependency "thor", "~> 0.18.1" 26 | spec.add_runtime_dependency "ruport", "~> 1.6.3" 27 | end 28 | -------------------------------------------------------------------------------- /spec/commands_spec.rb: -------------------------------------------------------------------------------- 1 | # coding: utf-8 2 | require 'ansible_tools' 3 | require 'yaml' 4 | 5 | test_dir = "tmp" 6 | 7 | describe "テスト" do 8 | before(:all) do 9 | $stdout = File.open("/dev/null", "w") #テスト実行中は標準出力は/dev/nullにする。 10 | end 11 | 12 | after(:all) do 13 | $stdout =STDOUT # テスト実行後は元に戻す 14 | end 15 | 16 | # テスト実行前 17 | before(:each) do 18 | FileUtils.mkdir_p(test_dir) unless FileTest.exist?(test_dir) 19 | Dir.chdir(test_dir) #tmp/に移動 20 | end 21 | 22 | # テスト実行後 23 | after(:each) do 24 | Dir.chdir("../") 25 | FileUtils.rm_rf(test_dir, :secure => true) 26 | end 27 | 28 | # ディレクトリの存在をチェックする。 29 | def expect_dir_exist(array) 30 | array.each{|d| 31 | expect(File.directory?(d)).to be_truthy 32 | } 33 | end 34 | # ファイルの存在をチェックする。 35 | def expect_file_exist(array) 36 | array.each{|f| 37 | expect(FileTest.exist?(f)).to be_truthy 38 | } 39 | end 40 | 41 | #YAMLファイルをロードする。 42 | def load_yaml(path) 43 | yaml = File.open( File.dirname(__FILE__) + path).read 44 | return YAML.load(yaml) 45 | end 46 | 47 | # run: ansbile-tools init 48 | it "init(yml-only = false)" do 49 | AnsibleTools.init(false) 50 | yaml = load_yaml("/lists/init_false.yml") 51 | expect_file_exist(yaml["file"]) 52 | expect_dir_exist(yaml["dir"]) 53 | end 54 | 55 | # run: ansbile-tools init -y 56 | it "init(yml-only = true)" do 57 | AnsibleTools.init(true) 58 | yaml = load_yaml("/lists/init_true.yml") 59 | expect_file_exist(yaml["file"]) 60 | expect_dir_exist(yaml["dir"]) 61 | end 62 | 63 | # run: ansbile-tools init -s 64 | it "init simple (yml-only = false)" do 65 | AnsibleTools.init_simple(false) 66 | yaml = load_yaml("/lists/init_simple_false.yml") 67 | expect_file_exist(yaml["file"]) 68 | expect_dir_exist(yaml["dir"]) 69 | end 70 | 71 | # run: ansbile-tools init -s -y 72 | it "init simple (yml-only = true)" do 73 | AnsibleTools.init_simple(true) 74 | yaml = load_yaml("/lists/init_simple_true.yml") 75 | expect_file_exist(yaml["file"]) 76 | expect_dir_exist(yaml["dir"]) 77 | end 78 | 79 | # run: ansbile-tools init -r gitlab 80 | it "init role (yml-only = false)" do 81 | AnsibleTools.init_role("gitlab", false) 82 | yaml = load_yaml("/lists/init_role_false.yml") 83 | expect_file_exist(yaml["file"]) 84 | expect_dir_exist(yaml["dir"]) 85 | end 86 | 87 | # run: ansbile-tools init -r gitlab -y 88 | it "init role (yml-only = true)" do 89 | AnsibleTools.init_role("gitlab", true) 90 | yaml = load_yaml("/lists/init_role_true.yml") 91 | expect_file_exist(yaml["file"]) 92 | expect_dir_exist(yaml["dir"]) 93 | end 94 | 95 | end 96 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # This file was generated by the `rspec --init` command. Conventionally, all 2 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 3 | # The generated `.rspec` file contains `--require spec_helper` which will cause this 4 | # file to always be loaded, without a need to explicitly require it in any files. 5 | # 6 | # Given that it is always loaded, you are encouraged to keep this file as 7 | # light-weight as possible. Requiring heavyweight dependencies from this file 8 | # will add to the boot time of your test suite on EVERY test run, even for an 9 | # individual file that may not need all of that loaded. Instead, consider making 10 | # a separate helper file that requires the additional dependencies and performs 11 | # the additional setup, and require it from the spec files that actually need it. 12 | # 13 | # The `.rspec` file also contains a few flags that are not defaults but that 14 | # users commonly want. 15 | # 16 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 17 | RSpec.configure do |config| 18 | # rspec-expectations config goes here. You can use an alternate 19 | # assertion/expectation library such as wrong or the stdlib/minitest 20 | # assertions if you prefer. 21 | config.expect_with :rspec do |expectations| 22 | # This option will default to `true` in RSpec 4. It makes the `description` 23 | # and `failure_message` of custom matchers include text for helper methods 24 | # defined using `chain`, e.g.: 25 | # be_bigger_than(2).and_smaller_than(4).description 26 | # # => "be bigger than 2 and smaller than 4" 27 | # ...rather than: 28 | # # => "be bigger than 2" 29 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 30 | end 31 | 32 | # rspec-mocks config goes here. You can use an alternate test double 33 | # library (such as bogus or mocha) by changing the `mock_with` option here. 34 | config.mock_with :rspec do |mocks| 35 | # Prevents you from mocking or stubbing a method that does not exist on 36 | # a real object. This is generally recommended, and will default to 37 | # `true` in RSpec 4. 38 | mocks.verify_partial_doubles = true 39 | end 40 | 41 | # The settings below are suggested to provide a good initial experience 42 | # with RSpec, but feel free to customize to your heart's content. 43 | =begin 44 | # These two settings work together to allow you to limit a spec run 45 | # to individual examples or groups you care about by tagging them with 46 | # `:focus` metadata. When nothing is tagged with `:focus`, all examples 47 | # get run. 48 | config.filter_run :focus 49 | config.run_all_when_everything_filtered = true 50 | 51 | # Limits the available syntax to the non-monkey patched syntax that is recommended. 52 | # For more details, see: 53 | # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax 54 | # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 55 | # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching 56 | config.disable_monkey_patching! 57 | 58 | # This setting enables warnings. It's recommended, but in some cases may 59 | # be too noisy due to issues in dependencies. 60 | config.warnings = true 61 | 62 | # Many RSpec users commonly either run the entire suite or an individual 63 | # file, and it's useful to allow more verbose output when running an 64 | # individual spec file. 65 | if config.files_to_run.one? 66 | # Use the documentation formatter for detailed output, 67 | # unless a formatter has already been configured 68 | # (e.g. via a command-line flag). 69 | config.default_formatter = 'doc' 70 | end 71 | 72 | # Print the 10 slowest examples and example groups at the 73 | # end of the spec run, to help surface which specs are running 74 | # particularly slow. 75 | config.profile_examples = 10 76 | 77 | # Run specs in random order to surface order dependencies. If you find an 78 | # order dependency and want to debug it, you can fix the order by providing 79 | # the seed, which is printed after each run. 80 | # --seed 1234 81 | config.order = :random 82 | 83 | # Seed global randomization in this process using the `--seed` CLI option. 84 | # Setting this allows you to use `--seed` to deterministically reproduce 85 | # test failures related to randomization by passing the same `--seed` value 86 | # as the one that triggered the failure. 87 | Kernel.srand config.seed 88 | =end 89 | end 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AnsibleTools 2 | 3 | Ansible Tools e.g. Create directory by BestPractice 4 | 5 | [![Gem Version](https://badge.fury.io/rb/ansible_tools.png)](http://badge.fury.io/rb/ansible_tools) 6 | [![Code Climate](https://codeclimate.com/github/volanja/ansible_tools.png)](https://codeclimate.com/github/volanja/ansible_tools) 7 | [![Build Status](https://travis-ci.org/volanja/ansible_tools.svg?branch=master)](https://travis-ci.org/volanja/ansible_tools) 8 | 9 | ## Installation 10 | 11 | Install it yourself as: 12 | ``` 13 | $ gem install ansible_tools 14 | ``` 15 | 16 | ## Usage 17 | 18 | ``` 19 | $ ansible-tools 20 | Commands: 21 | ansible-tools help [COMMAND] # Describe available commands or one specific command 22 | ansible-tools init [-s][-r][-y] # create ansible files by BestPractice 23 | ansible-tools show # show ansible valiables in vars/main.yml 24 | ansible-tools version # show version 25 | ``` 26 | 27 | ### BestPractice 28 | [Best Practices - ANSIBLEWORKS](http://www.ansibleworks.com/docs/playbooks_best_practices.html) 29 | 30 | ``` 31 | $ ansible-tools init [-y] 32 | create roles/common/tasks 33 | create roles/common/handlers 34 | create roles/common/templates 35 | create roles/common/vars 36 | create roles/common/files 37 | create group_vars 38 | create host_vars 39 | create site.yml 40 | create roles/common/tasks/main.yml 41 | create roles/common/handlers/main.yml 42 | create roles/common/templates/foo.conf.j2 #(*1) 43 | create roles/common/vars/main.yml 44 | create roles/common/files/bar.txt #(*1) 45 | create production #(*1) 46 | create stage #(*1) 47 | create group_vars/group1 #(*1) 48 | create group_vars/group2 #(*1) 49 | create host_vars/hostname1 #(*1) 50 | create host_vars/hostname2 #(*1) 51 | 52 | (*1)...if set [-y], this file is not create. 53 | ``` 54 | 55 | ### Simple 56 | 57 | ``` 58 | $ ansible-tools init -s [-y] 59 | create roles/common/tasks 60 | create roles/common/handlers 61 | create roles/common/templates 62 | create roles/common/vars 63 | create roles/common/files 64 | create site.yml 65 | create roles/common/tasks/main.yml 66 | create roles/common/handlers/main.yml 67 | create roles/common/templates/foo.conf.j2 #(*1) 68 | create roles/common/vars/main.yml 69 | create roles/common/files/bar.txt #(*1) 70 | 71 | (*1)...if set [-y], this file is not create. 72 | ``` 73 | 74 | ### Add Role 75 | 76 | ``` 77 | $ ansible-tools init -r [-y] 78 | 79 | $ ansible-tools init -r gitlab 80 | create roles/gitlab/tasks 81 | create roles/gitlab/handlers 82 | create roles/gitlab/templates 83 | create roles/gitlab/vars 84 | create roles/gitlab/files 85 | create site.yml 86 | create roles/gitlab/tasks/main.yml 87 | create roles/gitlab/handlers/main.yml 88 | create roles/gitlab/templates/foo.conf.j2 #(*1) 89 | create roles/gitlab/vars/main.yml 90 | create roles/gitlab/files/bar.txt #(*1) 91 | 92 | (*1)...if set [-y], this file is not create. 93 | ``` 94 | 95 | ### Show Variables 96 | Search file and write list 97 | file => `roles/*/vars/main.yml`, `group_vars/*.yml`, `host_vars/*.yml`, `*.yml` 98 | 99 | ``` 100 | $ ansible-tools show 101 | +----------------------------------------------------------------------------+ 102 | | File | Key | Value | 103 | +----------------------------------------------------------------------------+ 104 | | host_vars/main.yml | var1 | num1 | 105 | | group_vars/main.yml | var2 | num2 | 106 | | roles/gitlab/vars/main.yml | mysql_gitlab_password | password | 107 | | roles/gitlab/vars/main.yml | mysql_gitlab_database | gitlabhq_production | 108 | | roles/mariadb/vars/main.yml | mysql_root_password | password | 109 | | roles/Packages/vars/main.yml | www_port | 80 | 110 | | roles/ruby/vars/main.yml | ruby_ver | 2.0.0-p247 | 111 | +----------------------------------------------------------------------------+ 112 | ``` 113 | 114 | ### Show Variables 115 | show version 116 | 117 | ``` 118 | $ ansible-tools version 119 | 0.0.4 120 | ``` 121 | 122 | ## Contributing 123 | 124 | 1. Fork it 125 | 2. Create your feature branch (`git checkout -b my-new-feature`) 126 | 3. Commit your changes (`git commit -am 'Add some feature'`) 127 | 4. Push to the branch (`git push origin my-new-feature`) 128 | 5. Create new Pull Request 129 | 130 | ## History 131 | 0.0.4 Add Option init [-y] 132 | 133 | ## TODO 134 | + Make Test 135 | -------------------------------------------------------------------------------- /lib/ansible_tools.rb: -------------------------------------------------------------------------------- 1 | require "ansible_tools/version" 2 | require "fileutils" 3 | require "yaml" 4 | require 'ruport' 5 | 6 | module AnsibleTools 7 | 8 | # command ansible-tools init 9 | def self.init(yml_only) 10 | simple = yml_only ? safe_list_yml_only('common') : safe_list_simple('common') 11 | complex = safe_list_complex() 12 | # dir 13 | simple[:dir].each { |dir| safe_mkdir(dir) } 14 | complex[:dir].each { |dir| safe_mkdir(dir) } 15 | # file 16 | simple[:file].each { |file| safe_touch(file) } 17 | complex[:file].each { |file| safe_touch(file) } unless yml_only 18 | 19 | end 20 | 21 | # command ansible-tools init -s 22 | def self.init_simple(yml_only) 23 | simple = yml_only ? safe_list_yml_only('common') : safe_list_simple('common') 24 | # dir 25 | simple[:dir].each { |dir| safe_mkdir(dir) } 26 | # file 27 | simple[:file].each { |file| safe_touch(file) } 28 | end 29 | 30 | # command ansible-tools init -r 31 | def self.init_role(name, yml_only) 32 | role = yml_only ? safe_list_yml_only("#{name}") : safe_list_simple("#{name}") 33 | # dir 34 | role[:dir].each { |dir| safe_mkdir(dir) } 35 | # file 36 | role[:file].each { |file| safe_touch(file) } 37 | end 38 | 39 | def self.safe_list_simple(role) 40 | dir = Array.new 41 | dir_role = "roles/#{role}" 42 | tasks = "#{dir_role}/tasks" 43 | handlers = "#{dir_role}/handlers" 44 | templates = "#{dir_role}/templates" 45 | vars = "#{dir_role}/vars" 46 | files = "#{dir_role}/files" 47 | dir = [tasks,handlers,templates,vars,files] 48 | 49 | file = Array.new 50 | site = 'site.yml' 51 | f_task = "#{tasks}/main.yml" 52 | f_handlers = "#{handlers}/main.yml" 53 | f_templates = "#{templates}/foo.conf.j2" 54 | f_vars = "#{vars}/main.yml" 55 | f_file = "#{files}/bar.txt" 56 | file = [site,f_task,f_handlers,f_templates, f_vars, f_file] 57 | return {:dir => dir, :file => file} 58 | end 59 | 60 | def self.safe_list_yml_only(role) 61 | dir = Array.new 62 | dir_role = "roles/#{role}" 63 | tasks = "#{dir_role}/tasks" 64 | handlers = "#{dir_role}/handlers" 65 | templates = "#{dir_role}/templates" 66 | vars = "#{dir_role}/vars" 67 | files = "#{dir_role}/files" 68 | dir = [tasks,handlers,templates,vars,files] 69 | 70 | file = Array.new 71 | site = 'site.yml' 72 | f_task = "#{tasks}/main.yml" 73 | f_handlers = "#{handlers}/main.yml" 74 | f_vars = "#{vars}/main.yml" 75 | file = [site,f_task,f_handlers, f_vars] 76 | return {:dir => dir, :file => file} 77 | end 78 | 79 | 80 | def self.safe_list_complex() 81 | dir = Array.new 82 | group = 'group_vars' 83 | host = 'host_vars' 84 | dir = [group, host] 85 | file = ["production", "stage", "#{group}/group1", "#{group}/group2", "#{host}/hostname1", "#{host}/hostname2"] 86 | return {:dir => dir, :file => file} 87 | end 88 | 89 | def self.safe_mkdir(dir) 90 | unless FileTest.exist?("#{dir}") 91 | FileUtils.mkdir_p("#{dir}") 92 | TermColor.green 93 | puts "\t\tcreate\t#{dir}" 94 | TermColor.reset 95 | else 96 | TermColor.red 97 | puts "\t\texists\t#{dir}" 98 | TermColor.reset 99 | end 100 | end 101 | 102 | def self.safe_touch(file) 103 | unless File.exists? "#{file}" 104 | File.open("#{file}", 'w') do |f| 105 | #f.puts content 106 | end 107 | TermColor.green 108 | puts "\t\tcreate\t#{file}" 109 | TermColor.reset 110 | else 111 | TermColor.red 112 | puts "\t\texists\t#{file}" 113 | TermColor.reset 114 | end 115 | end 116 | 117 | # command ansible-tools show 118 | def self.show() 119 | begin 120 | if Dir.glob("**/vars/*").count == 0 121 | puts 'Not Found' 122 | exit 1 123 | end 124 | table = Ruport::Data::Table.new 125 | table.column_names = %w[File Key Value] 126 | 127 | regexp_str = Array.new 128 | regexp_str << "*.yml" # search *.yml 129 | regexp_str << "*/*.yml" # search */*.yml e.g. group_vars, host_vars 130 | regexp_str << "**/vars/*" # search roles/*/vars/*.yml 131 | regexp_str.each{|str| 132 | Dir.glob(str) {|f| 133 | next unless FileTest.file?(f) #skip directory 134 | yml = YAML.load_file(f) 135 | if yml == false 136 | puts "No Variables in #{f}" 137 | next 138 | end 139 | if str == "*.yml" 140 | yml.each{|h| 141 | if h.instance_of?(Hash) && h.has_key?("vars") 142 | h["vars"].each{|key,value| 143 | table << [f,key,value] 144 | } 145 | end 146 | } 147 | else 148 | yml.each{|key,value| 149 | table << [f,key,value] 150 | } 151 | end 152 | } 153 | } 154 | 155 | if table.count > 0 156 | puts table.to_text 157 | end 158 | rescue => e 159 | puts e 160 | fail 'Sorry. Error hanppend..' 161 | end 162 | end 163 | 164 | class TermColor 165 | class << self 166 | # 色を解除 167 | def reset ; c 0 ; end 168 | 169 | # 各色 170 | def red ; c 31; end 171 | def green ; c 32; end 172 | 173 | # カラーシーケンスの出力 174 | def c(num) 175 | print "\e[#{num.to_s}m" 176 | end 177 | end 178 | end 179 | 180 | end 181 | --------------------------------------------------------------------------------