Ansible + GitLab で楽に何度もデプロイする話 #ゆるWeb札幌

Ansible + GitLab で楽に何度もデプロイする話 #ゆるWeb札幌

ゆるWeb勉強会@札幌 #8 (中止)
https://mild-web-sap.connpass.com/event/164107/

勉強会を中止にしたので、資料のみ。

13725f35541aa680ed5f85d16b85947a?s=128

Kihara, Takuya

February 15, 2020
Tweet

Transcript

  1. גࣜձࣾϊʔεσΟςʔϧ ։ൃ%JWJTJPOٕज़ਪਐ(SPVQ UBDDL໦ݪ୎໵ !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ "OTJCMF (JU-BCͰ ָʹԿ౓΋σϓϩΠ͢Δ࿩ ೥݄೔

  2. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • ໊લ ໦ݪ ୎໵ / @tacck •

    ॴଐ גࣜձࣾϊʔεσΟςʔϧ
 ։ൃDivision ٕज़ਪਐGroup / Leader • ओ࠵ • ΏΔWebษڧձ@ࡳຈ • εϚʔτεϐʔΧʔͰ༡΅͏ձ@ࡳຈ • ޷͖ͳϑΟΪϡΞεέʔτͷٕ εϓϨουɾΠʔάϧ ୭ PHP Conference Hokkaido 2019
  3. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • Ansible • GitLab CI/CD • ࿈ܞͯ͠ԿΛ΍͔ͬͨ

    ࠓ೔࿩͢͜ͱ
  4. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ ͦ΋ͦ΋

  5. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • ࣾ಺ͷҊ݅ͰɺαʔόʔҠ؅͕ඞཁͩͬͨɻ • ௒খن໛ͷอक • ͋Δاۀ಺޲͚εϚʔτϑΥϯΞϓϦͷ
 ഑෍༻αʔό

    (ΦϯϓϨ) • ࣾ಺ͷϚγϯ੔ཧʹ൐͍ɺAWS Lightsail ΁Ҡ؅ɻ • ϑΝΠϧͷ഑ஔɺ֬ೝ߲໨ͳͲ͸͢Ͱʹݻ·͍ͬͯΔͷͰɺ
 εϜʔζʹ࡞ۀ͢ΔͨΊɻ ͦ΋ͦ΋
  6. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • Ҋ݅ಛ༗ͷߏ଄ͳͲ͸ɺʮҰճʯ΍Ε͹جຊऴΘΓɻ
 → ࣗಈԽͳͲ͢ΔίετΛ෷͑ͳ͍ɻ • ͱݴ͍ͭͭɺ͜͏͍͏খن໛Ҋ݅͸
 ʮ୲౰ऀʯͷೖΕସΘΓ͕ൃੜ͠΍͍͢ɻ


    (ͦͷ࣌ʹରԠͰ͖Δਓ͕ରԠ͢ΔɺɺͳͲɻ) ͱ͸͍͑
  7. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • Ansible ʹΑΔΠϯϑϥͷίʔυԽ (IaaC) • GitLab CI/CD

    ʹΑΔσϓϩΠͷࣗಈԽ (DevOps) • ࠓޙΛݟਾ͑ͯ΍͓͖͔ͬͯͨͬͨɻ • ίʔυԽ͓ͯ͘͠ͱࣾ಺΁ͷ஌ݟల։΋͠΍͍͢ͷͰɺ
 ৭ʑͱ໾ʹཱͭɻ ͦͷͨΊ
  8. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ "OTJCMFͱ͸

  9. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ "OTJCMF https://www.ansible.com/

  10. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ "OTJCMFͷಛ௃

  11. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • Python ੡ • ΤʔδΣϯτϨε • ૢ࡞ର৅ʹɺಛผͳԿ͔ΛೖΕͳͯ͘ྑ͍ɻ


    ( SSH ͱ Python ͕͋Ε͹ྑ͍ɻ) • YAML • ઃఆ͸YAMLͰهड़Մೳɻ • ઃఆͷه๏ͦͷ΋ͷͷशಘ೉қ౓͸௿͍ɻ
 (࢖͍͍ͨػೳ͝ͱʹ֮͑Δ͜ͱ͸ɺ΋ͪΖΜ͋Γ·͢ɻ) "OTJCMFͷಛ௃
  12. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • Ϟδϡʔϧ • ϞδϡʔϧํࣜͰɺػೳ֦ு͞Ε͍ͯΔɻ • ඞཁͳΒɺࣗ෼Ͱ࡞੒΋Մೳɻ •

    ຊମ͸ Python ͕ͩɺ
 Ϟδϡʔϧ͸ Python ͡Όͳͯ͘΋OKɻ "OTJCMFͷಛ௃
  13. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • ႈ౳ੑ • ʮԿ౓࣮ߦͯ͠΋ಉ͡ঢ়ଶʹͳΔʯ͜ͱΛαϙʔτɻ • ͨͩ͠ɺϞδϡʔϧͷ࣮૷࣍ୈɻ •

    ϑΝΠϧ഑ஔ΍αʔϏεΠϯετʔϧͱ͍ͬͨ΋ͷ͸ɺ
 جຊతʹαϙʔτ͞Ε͍ͯΔɻ • ʮίϚϯυ࣮ߦʯܥͷϞδϡʔϧ͸ɺ
 αϙʔτ͞Ε͍ͯͳ͍Մೳੑ͕͋ΔͷͰ஫ҙɻ
 (Ansible ͔ΒݟͯอূͰ͖ͳ͍ͷͰɻ) "OTJCMFͷಛ௃
  14. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ "OTJCMFͷྺ࢙

  15. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • 2012೥ʹ “Ansible, Inc.” ͕։ൃ։࢝ɻ • 2013೥ʹ

    ࢲ΋࢖͍࢝Ίͨ (Β͍͠) ɻ
 https://qiita.com/tacck/items/e3669b94e6a8066ce642 • ͜ͷ࣌ظ͸ߏ੒؅ཧπʔϧͷོ੝ظɻ • “Chef” ͕ڧ͔ͬͨهԱɻ (Ruby੡) • 2015೥ʹ Red Hat ࣾʹΑΓങऩɻ • ͪͳΈʹɺ Red Hat ࣾ΋ 2018೥ʹ IBM ࣾʹΑΓങऩɻ "OTJCMFͷྺ࢙
  16. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ "OTJCMFઃఆͷجຊ

  17. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • ର৅ͱͳΔϗετͷ৘ใ (Inventory) • INIܗࣜ or YAMLܗࣜ

    (ൺֱత࠷ۙରԠ͞Εͨ) • աڈͷ৘ใΛάάΔͱɺINIܗࣜͷ৘ใ͕ͨ͘͞Μɻ • ૢ࡞಺༰ (Playbook) • ػೳ୯ҐͰ෼ׂ͢Δ Roles ܗࣜͰॻ͘ͷ͕Ұൠతɻ "OTJCMFઃఆͷجຊ
  18. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ *OWFOUPSZ [web] 192.168.100.1 [web:vars] ansible_port=22 ansible_user=centos ansible_ssh_private_key_file=hosts/testing/web_testing.pem

    [all:vars] stage=testing
  19. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ 1MBZCPPL NBJOZNM --- - hosts: all become:

    yes vars_files: - "vars/{{ stage }}.yml" roles: - setups - file_upload
  20. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ 1MBZCPPL SPMFTTFUVQTUBTLTNBJOZNM --- - name: set timezon

    Asia/Tokyo shell: timedatectl set-timezone Asia/Tokyo - name: install httpd yum: name: httpd state: latest - name: enable httpd systemd: name: httpd enabled: yes - name: restart httpd systemd: name: httpd state: restarted
  21. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ 1MBZCPPL SPMFTpMF@VQMPBEUBTLTNBJOZNM --- - name: upload index.html

    file template: src: index.html.j2 dest: "/var/www/html/index.html"
  22. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ (JU-BC$*$%ͱ͸

  23. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • GitHub Έ͍ͨͳ Git Repository αʔϏεɻ •

    ͦͷதͷػೳͱͯ͠ɺ “CI/CD” ͕͋Δɻ • ϦϙδτϦʹ͋ΔίʔυΛ࢖ͬͯɺ
 ৭ʑ΍͍͍ͬͯΑ! ͱ͍͏αʔϏεɻ (JU-BC
  24. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • ઃఆΛ YAML Ͱهड़ɻ • ίʔυͷνΣοΫɺϑϩϯτΤϯυͷϏϧυɺ
 αʔόʔ΁ͷσϓϩΠͳͲͳͲΛهड़Մೳɻ

    • ઃఆهड़ޙ͸ɺ git push ͢ΔͨͼʹࣗಈͰ࣮ߦ͞ΕΔɻ • खಈͰͷ࣮ߦɾ࠶࣮ߦ΋Մೳɻ (JU-BC$*$%
  25. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ (JU-BC$*$% HJUMBCDJZNMൈਮ stages: - deploy - testing

    .deploy_job: &deploy_job stage: deploy image: cytopia/ansible:2.9-tools script: - ansible-playbook -i hosts/${APP_ENV}/inventory main.yml deploy testing: <<: *deploy_job variables: APP_ENV: testing before_script: - chmod 755 . - cat $SSH_KEY_PEM > hosts/testing/web_testing.pem - chmod 600 hosts/testing/web_testing.pem
  26. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ σϞ -JHIUTBJMͰαʔό४උ *OWFOUPSZʹ*1ࢦఆ (JU-BC$*$%ͰσϓϩΠ IUUQTHJUIVCDPNUBDDLTBNQMFBOTJCMFNJMEXFCTBQ

  27. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ ·ͱΊ

  28. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • Ansible Λ࢖ͬͯΠϯϑϥ (αʔόͷߏ੒) ΛίʔυԽɻ • ίʔυʹͳ͍ͬͯΕ͹ɺσϓϩΠ΋ࣗಈԽՄೳɻ


    (GitLab CI/CD) • σϓϩΠ΋ࣗಈԽ͞Ε͍ͯΕ͹ɺ
 ͙͢ʹ҆ఆͨ͠؀ڥΛखʹೖΕΒΕΔ!! • ςετͩͬͯॻ͚ͪΌ͏!!! • ಈ͘΋ͷΛఏࣔͰ͖ΔͷͰɺਓʹ΋఻͑΍͍͢ɻ ·ͱΊ
  29. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ FOE

  30. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ ࡳຈͷΠϯϑϥɾΫϥ΢υܥ ษڧձͷҊ಺ ൺֱత׆ಈ͍ͯ͠Δ΋ͷ ࢲ͕ิ଍Ͱ͖ͨ΋ͷ

  31. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • https://jawsug-sapporo.doorkeeper.jp/events/102248 • ࡾʙ࢛ϲ݄ʹҰճఔ౓ • ࣍ճ: 2020೥2݄21೔(ۚ)

    +"846(ࡳຈ
  32. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • https://sapporo-aws.connpass.com/event/163725/ • ࡾʙ࢛ϲ݄ʹҰճఔ౓ 4BQQPSPBXT

  33. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • https://sapporoazuremok2.connpass.com/event/154302/ • ຖ݄Ұճ • ࣍ճ: 2020೥2݄22೔

    (౔) ࡳຈ"[VSF΋͘΋͘ձ
  34. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • https://sapporo-beginner-kubernetes.connpass.com/event/ 161411/ • ࡾϲ݄ʹҰճఔ౓ ,VCFSOFUFT4BQQPSPGPS#FHJOOFST

  35. !UBDDL ΏΔ8FCษڧձ!ࡳຈ ΏΔ8FCࡳຈ • https://ansible-users.connpass.com/event/159174/ • ·ͩҰճͰ͕͢ɺࠓճ࢖ͬͨͷͰɻ • ࢲ΋ࢀՃͯ͠LT͠·ͨ͠ɻ "OTJCMF΋͘΋͘ձ

    αʔόฤ JOࡳຈ