Upgrade to Pro — share decks privately, control downloads, hide ads and more …

infra-auto-2014-10-07

rrreeeyyy
October 07, 2014

 infra-auto-2014-10-07

ITインフラ 業務自動化現状確認会

rrreeeyyy

October 07, 2014
Tweet

More Decks by rrreeeyyy

Other Decks in Technology

Transcript

  1. Itamae ࢖ͬͯΈͨ YOSHIKAWA Ryota (@rrreeeyyy) 1 ը૾ͷ͓ण࢘͸৽॓۠ͷ "Ეؙ" ͱ͍͏͓ళͷ෺Ͱ͢ɻඒຯ͔ͬͨ͠ !

    ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 1
  2. ࣗݾ঺հ 4 ٢઒ ཽଠ (Twitter: @rrreeeyyy → ) 4 ϋʔτϏʔπ(ձ৔)ͱ͍͏ձࣾͷ৽ଔͰ͢

    4 ͱݴ͍ͭͭۈଓ 4 ೥໨͙Β͍ʹͳΓ·͢2 4 ۀ຿ͷ൒෼͸ @koemu ͞Μͱಉ͡෦ॺͰࣗಈԽΞϨίϨ 4 ΋͏൒෼͸ MSP ͷۀ຿Ͱαʔόͷ؂ࢹɾ؅ཧ 2 ֶੜελοϑͱͯ͠ೖࣾͨ͠ͷ͕ 18 ࡀ (େֶ 1 ೥ੜͷ 10 ݄) ͙Β͍Ͱɺࠓ೥ֶ෦Λଔۀ͠·ͨ͠ɻ ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 2
  3. Provisioning Tools ݴ͑Δ͔ͳ 4 Chef 4 Ansible 4 Salticid 4

    Puppet 4 SaltStack 4 : ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 6
  4. Կ͕ى͜Δ͔ 4 Provisioning Tool 4 Ansible (Python) 4 Testing Tool

    4 Serverspec (Ruby) ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 12
  5. ! 99 ݴޠ͕౷Ұ͞Ε͓ͯΒͣɺvirtualenv, bundler ౳ͷπʔϧΛ֮͑ͨΓ ansible ίϚϯυ΍ bundle exec rake

    spec ౳ͷίϚϯυΛ͍֮͑ͯΔ࣌ͷإ ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 13
  6. Itamae 4 https://github.com/ryotarai/itamae (@ryot_a_rai) 4 https://speakerdeck.com/ryotarai/itamae-infra-as- code-xian-zhuang-que-ren-hui 4 Infrataster ࡞ͬͯΒͬ͠ΌΔํ

    4 ઌिͷ Infra as Code ݱঢ়֬ೝձͰϓϨθϯ͞Εͨ (ׂͱલ͔Β͋ͬͨͷͰউखʹ࢖ͬͯͨ) ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 15
  7. Կ͕͍͍ͷ 4 ΤʔδΣϯτϨε 4 γϯϓϧ 4 Ruby DSL (not YAML!)

    like a Chef 8 4 specinfra(serverspec ͷ core)ͷ্Ͱ੒ཱ 4 Ϩγϐ͕ gem Ͱ؅ཧͰ͖Δ (Berkshelf ཁΒͣ) 8 Not compatible. ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 16
  8. Կ͕͍͍ͷ 4 ႈ౳ 4 --dry-run 4 node attributes ͸ json

    Ͱ؅ཧ 9 4 : 9 yaml Ͱ΋؅ཧ͍ͨ͠ؾ࣋ͪ͸͋Δ(ग़དྷΔͷ͔ͳʁ) ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 17
  9. ී௨ͷྫ package 'httpd' do action :install end template '/etc/httpd/httpd.conf' do

    source 'httpd.conf.erb' variables(maxclients: 256) end itamae execute -j node.json -h 192.168.0.1 -p 22 -u yoshikawa -i ~/.ssh/id_rsa recipe.rb ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 18
  10. خ͍͠ࣄ 4 Ruby DSL ͕ॻ͖΍͍͢10 4 ςετͱϓϩϏδϣχϯάͰݴޠ͕ἧ͏ 4 ྫ͑͹ Rake

    λεΫͰ ɹϓϩϏδϣχϯάˠςετ͕Ұؾʹग़དྷΔ 10 ݸਓͷײ૝Ͱ͢ɻ๻͸ͨ·ͨ· Ruby ͕޷͖ͳͷͰɻ ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 20
  11. { "db-01": { "roles": ["common", "db"], "private_key": "~/.ssh/yoshikawa-test_rsa", "ssh_port": 22,

    "server_id": 1 }, "web-01": { "roles": ["web", "common"], "domain": "rrreeeyyy.com" } } ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 22
  12. require 'rake' require 'json' require 'rspec/core/rake_task' properties_file = 'properties.json' properties

    = JSON.parse(File.read(properties_file)) properties.keys.each do |key| desc "Run provision to #{key}" namespace :itamae do desc "Run itamae to #{key}" task key.split('.')[0] do command = "bundle exec itamae ssh" command << " -h #{key}" command << " -i #{properties[key]['private_key']}" command << " -p #{properties[key]['ssh_port']}" command << " -j #{properties_file}" properties[key]['roles'].each {|role| command << " provision/#{role}/#{role}.rb"} puts command system command end end namespace :serverspec do desc "Run serverspec to #{key}" RSpec::Core::RakeTask.new(key.split('.')[0].to_sym) do |t| ENV['TARGET_HOST'] = key t.pattern = 'spec/{' + properties[key]['roles'].join(',') + '}/*_spec.rb' end end end 11 https://gist.github.com/rrreeeyyy/84efc2907814c56a2b4a ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 24
  13. ݺͼग़͠ํ bundle exec rake itamae:db-01 ࣍ͷΑ͏ʹ͢ΔͱҰؾʹͰ͖Δ bundle exec rake itamae:db-01

    serverspec:db-01 ※ ΋ͪΖΜ Rakefile ಺ͰҰؾʹ΍ΔλεΫΛఆٛͯ͠΋Α͍ ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 25
  14. # ls Gemfile Rakefile provision vendor Gemfile.lock properties.json spec #

    cd provision && tree ├── common │ ├── common.rb │ ├── recipes │ │ ├── dstat.rb │ │ └── ntp.rb │ └── templates │ └── ntp.conf.erb ├── db │ ├── db.rb │ ├── recipes │ │ └── mysql.rb │ └── templates │ └── my.cnf.erb : ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 27
  15. # db/db.rb include_recipe "recipes/mysql.rb" include_recipe "..." : # db/recipes/mysql.rb package

    'mysql' do action :install end template '/etc/my.cnf' do action :create source '../templates/my.cnf.erb' variables(host: ENV['TARGET_HOST'], server_id: node[ENV['TARGET_HOST']]['server_id']) end ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 28
  16. ·ͱΊ 4 Itamae ࢖ͬͯΈͯͨ 4 RubyDSLɾAgentlessɾSimpleɾlight... Ͱྑ͍πʔϧ 4 Itamae ͱ

    Serverspec ΛҰؾʹ࣮ߦ͢Δ΍Γํ 4 Itamae ͷσΟϨΫτϦߏ଄ྫΛ঺հ ITΠϯϑϥ ۀ຿ࣗಈԽݱঢ়֬ೝձ (2014/10/07) - YOSHIKAWA Ryota (@rrreeeyyy) 30