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

Ansibleで構成管理。〜たったひとつの冴えたやりかた〜

 Ansibleで構成管理。〜たったひとつの冴えたやりかた〜

5dd17e8c82e294152169cf383fb7c870?s=128

Tomohiko Isobe

January 09, 2018
Tweet

Transcript

  1. AnsibleͰߏ੒؅ཧɻ ʙͨͬͨͻͱͭͷࡓ͑ͨ΍Γ͔ͨʙ 2018.1.9 ৽ఆ൪ʂAnsible ͱZabbixͰ࣮ݱ͢Δ࣍ੈ୅ӡ༻؅ཧ ү෦ஐ඙

  2. Who am I ? • ү෦ ஐ඙ (Tomohiko Isobe) •

    Infrastructure EngineerˏΫϦχΧϧɾϓϥοτϑΥʔϜג ࣜձࣾ(2017.10-) • ౡࠜݝग़਎ • Job Hopperʢݱࡏ8ࣾ໨…ʣ • Favorites: ࢙੻ɾઓ੻Ί͙ΓɺϑΝϛϨε https://clipla.jp
  3. Ansible࢖ͬͯ·͢ΑͶ

  4. ͜Μͳײ͡?

  5. Infrastructure as Code !

  6. None
  7. ͦΕͬͯ ʮߏஙखॱʯͷ؅ཧͰ͸ʁ

  8. αʔόͷʮߏ੒ʯΛ ؅ཧ͍ͨ͠

  9. ʮߏ੒؅ཧʯͱݴ͑͹

  10. ͔ͭͯɺ͜ͷੈʹ͸ ʮύϥϝʔλγʔτʯ ͳΔ΋ͷ͕͋ͬͨ

  11. ͜Μͳͷ

  12. ೔ʑͷӡ༻

  13. ύϥϝʔλγʔτɺ ߋ৽͠ͱ͖·ͨ͠❤

  14. ͋Δ೔ͷ͜ͱ

  15. None
  16. αʔόͷઃఆ͕ɺ ύϥϝʔλγʔτͱҧ͏…

  17. ͪΌΜͱαʔόೖͬ ͯ֬ೝ͠·ͨ͠ʁ

  18. Ͳ͏͢Δ͔

  19. ΩϥοͱͻΒΊ͍ͨ

  20. ࡓ͑ͨ΍Γ͔ͨ • Playbookͱม਺Λ෼͚Δ • hash_behaviour = merge • ม਺,FactҰཡΛग़ྗͯ͠؅ཧ

  21. ࡓ͑ͨ΍Γ͔ͨ • Playbookͱม਺Λ෼͚Δ • hash_behaviour = merge • ม਺,FactҰཡΛग़ྗͯ͠؅ཧ

  22. Playbookͱม਺Λ෼͚Δ

  23. Playbookͱม਺Λ෼͚Δ • Roleʹม਺͸ॻ͔ͳ͍ʂ • ΠϯϕϯτϦϑΝΠϧʹ΋ॻ͔ͳ͍ʂ • ςϯϓϨʔτͷύϥϝʔλ΋Ͱ͖Δ͚ͩม਺ ʹʂ

  24. Roleʹม਺͸ॻ͔ͳ͍ʂ • Role͸ࡉ͔͘෼ׂͨ͠ํ͕൚༻ੑ͕ߴ·Δ • ͍͍ͪͪશ෦ͷRoleͷmain.ymlݟͯΒΜͳ͍

  25. Roleʹม਺͸ॻ͔ͳ͍ʂ • Role͸ࡉ͔͘෼ׂͨ͠ํ͕൚༻ੑ͕ߴ·Δ • ͍͍ͪͪશ෦ͷRoleͷmain.ymlݟͯΒΜͳ͍

  26. Roleʹม਺͸ॻ͔ͳ͍ʂ • Role͸ࡉ͔͘෼ׂͨ͠ํ͕൚༻ੑ͕ߴ·Δ • ͍͍ͪͪશ෦ͷRoleͷmain.ymlݟͯΒΜͳ͍ →

  27. Roleʹม਺͸ॻ͔ͳ͍ʂ • Role͸ࡉ͔͘෼ׂͨ͠ํ͕൚༻ੑ͕ߴ·Δ • ͍͍ͪͪશ෦ͷRoleͷmain.ymlݟͯΒΜͳ͍ → มߋָ͕

  28. ΠϯϕϯτϦϑΝΠϧʹ΋ॻ͔ͳ͍ʂ • ֊૚తʹॻ͚ͳ͍ͷͰɺม਺΍άϧʔϓ͕૿͑ ΔͱಡΈͮΒ͍

  29. ΠϯϕϯτϦϑΝΠϧʹ΋ॻ͔ͳ͍ʂ • ֊૚తʹॻ͚ͳ͍ͷͰɺม਺΍άϧʔϓ͕૿͑ ΔͱಡΈͮΒ͍

  30. ΠϯϕϯτϦϑΝΠϧʹ΋ॻ͔ͳ͍ʂ • ֊૚తʹॻ͚ͳ͍ͷͰɺม਺΍άϧʔϓ͕૿͑ ΔͱಡΈͮΒ͍ ͭΒ͍ʜ

  31. ςϯϓϨʔτͷύϥϝʔλ΋Ͱ͖Δ ͚ͩม਺ʹʂ • Roleͱ߹Θͤͯ൚༻ੑ͕޲্

  32. ςϯϓϨʔτͷύϥϝʔλ΋Ͱ͖Δ ͚ͩม਺ʹʂ • Roleͱ߹Θͤͯ൚༻ੑ͕޲্ logrotateͷtemplate(ൈਮ)

  33. ςϯϓϨʔτͷύϥϝʔλ΋Ͱ͖Δ ͚ͩม਺ʹʂ • Roleͱ߹Θͤͯ൚༻ੑ͕޲্ logrotateͷtemplate(ൈਮ) logrotateͷvars

  34. Playbookͱม਺Λ෼͚Δ • Roleʹม਺͸ॻ͔ͳ͍ʂ • ΠϯϕϯτϦϑΝΠϧʹ΋ॻ͔ͳ͍ʂ • ςϯϓϨʔτͷύϥϝʔλ΋Ͱ͖Δ͚ͩม਺ ʹʂ

  35. Playbookͱม਺Λ෼͚Δ • Roleʹม਺͸ॻ͔ͳ͍ʂ • ΠϯϕϯτϦϑΝΠϧʹ΋ॻ͔ͳ͍ʂ • ςϯϓϨʔτͷύϥϝʔλ΋Ͱ͖Δ͚ͩม਺ ʹʂ →ɹgroup_vars,host_varsʹॻ͘ʂ

  36. ࡓ͑ͨ΍Γ͔ͨ • Playbookͱม਺Λ෼͚Δ • hash_behaviour = merge • ม਺,FactҰཡΛग़ྗͯ͠؅ཧ

  37. hash_behaviour = merge

  38. hash_behaviour = merge • ม਺ͷϚʔδϙϦγʔ • ansible.cfgͷ[defaults]Ͱઃఆ • σϑΥϧτ͸”replace”

  39. hash_behaviour = merge • σϑΥϧτ(replace)ͷڍಈ + group_vars ༏ઌ౓ɿ௿ host_vars ༏ઌ౓ɿߴ

  40. hash_behaviour = merge • σϑΥϧτ(replace)ͷڍಈ + group_vars ༏ઌ౓ɿ௿ host_vars ༏ઌ౓ɿߴ

    → key2͕ফ͑ͨ
  41. hash_behaviour = merge • hash_behaviour=mergeͷڍಈ + group_vars ༏ઌ౓ɿ௿ host_vars ༏ઌ౓ɿߴ

  42. hash_behaviour = merge • hash_behaviour=mergeͷڍಈ + → group_vars ༏ઌ౓ɿ௿ host_vars

    ༏ઌ౓ɿߴ key1͸্ॻ͖ key3͕௥Ճ
  43. hash_behaviour = merge • γʔέϯε(Ϧετ)͸ཁૉ͕શ෦্ॻ͖͞Εͯ ͠·͏ͷͰ஫ҙ + group_vars ༏ઌ౓ɿ௿ host_vars

    ༏ઌ౓ɿߴ
  44. hash_behaviour = merge • γʔέϯε(Ϧετ)͸ཁૉ͕શ෦্ॻ͖͞Εͯ ͠·͏ͷͰ஫ҙ + → group_vars ༏ઌ౓ɿ௿

    host_vars ༏ઌ౓ɿߴ list͝ͱ্ॻ͖
  45. hash_behaviour = merge • ม਺ͷޮ཰తͳ؅ཧ͕Մೳ

  46. hash_behaviour = merge • ม਺ͷޮ཰తͳ؅ཧ͕Մೳ group_vars/all.yml(શαʔόڞ௨)

  47. hash_behaviour = merge • ม਺ͷޮ཰తͳ؅ཧ͕Մೳ group_vars/all.yml(શαʔόڞ௨) group_vars/webservers.yml(webαʔόڞ௨) +

  48. hash_behaviour = merge • ม਺ͷޮ཰తͳ؅ཧ͕Մೳ group_vars/all.yml(શαʔόڞ௨) group_vars/webservers.yml(webαʔόڞ௨) host_vars/web01.yml(web01ݻ༗ઃఆ) + +

  49. hash_behaviour = merge • ม਺ͷޮ཰తͳ؅ཧ͕Մೳ group_vars/all.yml(શαʔόڞ௨) group_vars/webservers.yml(webαʔόڞ௨) host_vars/web01.yml(web01ݻ༗ઃఆ) ࠷ऴతʹweb01ʹద༻͞ΕΔม਺ →

    + +
  50. hash_behaviour = merge • ΍Γ͗͢஫ҙ • ༧ظͤ͵্ॻ͖ • ෳ਺group_varsͷద༻ॱ •

    ม਺͕෼ࢄ͞Εͯ͠·͏
  51. ࡓ͑ͨ΍Γ͔ͨ • Playbookͱม਺Λ෼͚Δ • hash_behaviour = merge • ม਺,FactҰཡΛग़ྗͯ͠؅ཧ

  52. ม਺,FactҰཡΛग़ྗͯ͠؅ཧ

  53. ม਺,FactҰཡΛग़ྗͯ͠؅ཧ • merge͞Εͨม਺Λग़ྗ • Fact ?

  54. merge͞Εͨม਺Λग़ྗ • templateϞδϡʔϧͰɺlocalhostʹશม਺Λग़ ྗ͢Δ • શม਺Λ”my_vars(೚ҙ)”ҎԼͷ֊૚ߏ଄ʹ͢ Δʢॏཁʂʣ • {{ hostvars[inventory_hostname]["my_vars"]

    | to_nice_yaml | indent(2, True) }}
  55. merge͞Εͨม਺Λग़ྗ • Playbook ESZSVOͰ΋ग़ྗ

  56. merge͞Εͨม਺Λग़ྗ • template

  57. merge͞Εͨม਺Λग़ྗ • ͜Μͳײ͡

  58. Fact ? • SetupϞδϡʔϧʹΑΓऩू͞ΕΔର৅ϗετ ͷ؀ڥ৘ใ • ansible_*ͱͯ͠ࢀরՄೳ

  59. ͔ͤͬ͘ͳΜͰFact΋ग़ྗ͠·͔͢ • ྫ͑͹ɺΧʔωϧͷόʔδϣϯͳΒ {{ hostvars[inventory_hostname] [ansible_kernel] }} • ͍ͬͺ͍͋ΔͷͰ؅ཧ͍ͨ͠৘ใΛ͓޷ΈͰ

  60. ͔ͤͬ͘ͳΜͰFact΋ग़ྗ͠·͔͢ • ͜Μͳײ͡

  61. ࠓͷαʔόͷߏ੒ɺ ͜͏ͳͬͯ·͢❤

  62. ύϥϝʔλγʔτײʂ

  63. ͜ΕΛgitͰ؅ཧ͢Ε͹

  64. ߏ੒؅ཧʂ

  65. ·ͱΊ

  66. ࡓ͑ͨ΍Γ͔ͨ • Playbookͱม਺Λ෼͚Δ • hash_behaviour = merge • ม਺,FactҰཡΛग़ྗͯ͠؅ཧ

  67. ࡓ͑ͨ΍Γ͔ͨ • Playbookͱม਺Λ෼͚Δ • hash_behaviour = merge • ม਺,FactҰཡΛग़ྗͯ͠؅ཧ →ߏஙखॱͱύϥϝʔλΛ෼ׂ

  68. ࡓ͑ͨ΍Γ͔ͨ • Playbookͱม਺Λ෼͚Δ • hash_behaviour = merge • ม਺,FactҰཡΛग़ྗͯ͠؅ཧ →ߏஙखॱͱύϥϝʔλΛ෼ׂ

    →ύϥϝʔλΛޮ཰తʹ؅ཧ
  69. ࡓ͑ͨ΍Γ͔ͨ • Playbookͱม਺Λ෼͚Δ • hash_behaviour = merge • ม਺,FactҰཡΛग़ྗͯ͠؅ཧ ≒

    ύϥϝʔλγʔτ →ߏஙखॱͱύϥϝʔλΛ෼ׂ →ύϥϝʔλΛޮ཰తʹ؅ཧ
  70. ࡓ͑ͨ΍Γ͔ͨ • Playbookͱม਺Λ෼͚Δ • hash_behaviour = merge • ม਺,FactҰཡΛग़ྗͯ͠؅ཧ ≒

    ύϥϝʔλγʔτˠgit؅ཧ →ߏஙखॱͱύϥϝʔλΛ෼ׂ →ύϥϝʔλΛޮ཰తʹ؅ཧ
  71. એ఻ • ଞʹ΋Ansibleωλॻ͍ͯ·͢ • https://qiita.com/isobecky74

  72. Thank you !