Ansibleで構成管理。〜たったひとつの冴えたやりかた〜
by
Tomohiko Isobe
Link
Embed
Share
Beginning
This slide
Copy link URL
Copy link URL
Copy iframe embed code
Copy iframe embed code
Copy javascript embed code
Copy javascript embed code
Share
Tweet
Share
Tweet
Slide 1
Slide 1 text
AnsibleͰߏཧɻ ʙͨͬͨͻͱͭͷࡓ͑ͨΓ͔ͨʙ 2018.1.9 ৽ఆ൪ʂAnsible ͱZabbixͰ࣮ݱ͢Δ࣍ੈӡ༻ཧ ү෦ஐ
Slide 2
Slide 2 text
Who am I ? • ү෦ ஐ (Tomohiko Isobe) • Infrastructure EngineerˏΫϦχΧϧɾϓϥοτϑΥʔϜג ࣜձࣾ(2017.10-) • ౡࠜݝग़ • Job Hopperʢݱࡏ8ࣾ…ʣ • Favorites: ࢙ɾઓΊ͙ΓɺϑΝϛϨε https://clipla.jp
Slide 3
Slide 3 text
Ansibleͬͯ·͢ΑͶ
Slide 4
Slide 4 text
͜Μͳײ͡?
Slide 5
Slide 5 text
Infrastructure as Code !
Slide 6
Slide 6 text
No content
Slide 7
Slide 7 text
ͦΕͬͯ ʮߏஙखॱʯͷཧͰʁ
Slide 8
Slide 8 text
αʔόͷʮߏʯΛ ཧ͍ͨ͠
Slide 9
Slide 9 text
ʮߏཧʯͱݴ͑
Slide 10
Slide 10 text
͔ͭͯɺ͜ͷੈʹ ʮύϥϝʔλγʔτʯ ͳΔͷ͕͋ͬͨ
Slide 11
Slide 11 text
͜Μͳͷ
Slide 12
Slide 12 text
ʑͷӡ༻
Slide 13
Slide 13 text
ύϥϝʔλγʔτɺ ߋ৽͠ͱ͖·ͨ͠❤
Slide 14
Slide 14 text
͋Δͷ͜ͱ
Slide 15
Slide 15 text
No content
Slide 16
Slide 16 text
αʔόͷઃఆ͕ɺ ύϥϝʔλγʔτͱҧ͏…
Slide 17
Slide 17 text
ͪΌΜͱαʔόೖͬ ͯ֬ೝ͠·ͨ͠ʁ
Slide 18
Slide 18 text
Ͳ͏͢Δ͔
Slide 19
Slide 19 text
ΩϥοͱͻΒΊ͍ͨ
Slide 20
Slide 20 text
ࡓ͑ͨΓ͔ͨ • PlaybookͱมΛ͚Δ • hash_behaviour = merge • ม,FactҰཡΛग़ྗͯ͠ཧ
Slide 21
Slide 21 text
ࡓ͑ͨΓ͔ͨ • PlaybookͱมΛ͚Δ • hash_behaviour = merge • ม,FactҰཡΛग़ྗͯ͠ཧ
Slide 22
Slide 22 text
PlaybookͱมΛ͚Δ
Slide 23
Slide 23 text
PlaybookͱมΛ͚Δ • Roleʹมॻ͔ͳ͍ʂ • ΠϯϕϯτϦϑΝΠϧʹॻ͔ͳ͍ʂ • ςϯϓϨʔτͷύϥϝʔλͰ͖Δ͚ͩม ʹʂ
Slide 24
Slide 24 text
Roleʹมॻ͔ͳ͍ʂ • Roleࡉׂ͔ͨ͘͠ํ͕൚༻ੑ͕ߴ·Δ • ͍͍ͪͪશ෦ͷRoleͷmain.ymlݟͯΒΜͳ͍
Slide 25
Slide 25 text
Roleʹมॻ͔ͳ͍ʂ • Roleࡉׂ͔ͨ͘͠ํ͕൚༻ੑ͕ߴ·Δ • ͍͍ͪͪશ෦ͷRoleͷmain.ymlݟͯΒΜͳ͍
Slide 26
Slide 26 text
Roleʹมॻ͔ͳ͍ʂ • Roleࡉׂ͔ͨ͘͠ํ͕൚༻ੑ͕ߴ·Δ • ͍͍ͪͪશ෦ͷRoleͷmain.ymlݟͯΒΜͳ͍ →
Slide 27
Slide 27 text
Roleʹมॻ͔ͳ͍ʂ • Roleࡉׂ͔ͨ͘͠ํ͕൚༻ੑ͕ߴ·Δ • ͍͍ͪͪશ෦ͷRoleͷmain.ymlݟͯΒΜͳ͍ → มߋָ͕
Slide 28
Slide 28 text
ΠϯϕϯτϦϑΝΠϧʹॻ͔ͳ͍ʂ • ֊తʹॻ͚ͳ͍ͷͰɺมάϧʔϓ͕૿͑ ΔͱಡΈͮΒ͍
Slide 29
Slide 29 text
ΠϯϕϯτϦϑΝΠϧʹॻ͔ͳ͍ʂ • ֊తʹॻ͚ͳ͍ͷͰɺมάϧʔϓ͕૿͑ ΔͱಡΈͮΒ͍
Slide 30
Slide 30 text
ΠϯϕϯτϦϑΝΠϧʹॻ͔ͳ͍ʂ • ֊తʹॻ͚ͳ͍ͷͰɺมάϧʔϓ͕૿͑ ΔͱಡΈͮΒ͍ ͭΒ͍ʜ
Slide 31
Slide 31 text
ςϯϓϨʔτͷύϥϝʔλͰ͖Δ ͚ͩมʹʂ • Roleͱ߹Θͤͯ൚༻ੑ্͕
Slide 32
Slide 32 text
ςϯϓϨʔτͷύϥϝʔλͰ͖Δ ͚ͩมʹʂ • Roleͱ߹Θͤͯ൚༻ੑ্͕ logrotateͷtemplate(ൈਮ)
Slide 33
Slide 33 text
ςϯϓϨʔτͷύϥϝʔλͰ͖Δ ͚ͩมʹʂ • Roleͱ߹Θͤͯ൚༻ੑ্͕ logrotateͷtemplate(ൈਮ) logrotateͷvars
Slide 34
Slide 34 text
PlaybookͱมΛ͚Δ • Roleʹมॻ͔ͳ͍ʂ • ΠϯϕϯτϦϑΝΠϧʹॻ͔ͳ͍ʂ • ςϯϓϨʔτͷύϥϝʔλͰ͖Δ͚ͩม ʹʂ
Slide 35
Slide 35 text
PlaybookͱมΛ͚Δ • Roleʹมॻ͔ͳ͍ʂ • ΠϯϕϯτϦϑΝΠϧʹॻ͔ͳ͍ʂ • ςϯϓϨʔτͷύϥϝʔλͰ͖Δ͚ͩม ʹʂ →ɹgroup_vars,host_varsʹॻ͘ʂ
Slide 36
Slide 36 text
ࡓ͑ͨΓ͔ͨ • PlaybookͱมΛ͚Δ • hash_behaviour = merge • ม,FactҰཡΛग़ྗͯ͠ཧ
Slide 37
Slide 37 text
hash_behaviour = merge
Slide 38
Slide 38 text
hash_behaviour = merge • มͷϚʔδϙϦγʔ • ansible.cfgͷ[defaults]Ͱઃఆ • σϑΥϧτ”replace”
Slide 39
Slide 39 text
hash_behaviour = merge • σϑΥϧτ(replace)ͷڍಈ + group_vars ༏ઌɿ host_vars ༏ઌɿߴ
Slide 40
Slide 40 text
hash_behaviour = merge • σϑΥϧτ(replace)ͷڍಈ + group_vars ༏ઌɿ host_vars ༏ઌɿߴ → key2͕ফ͑ͨ
Slide 41
Slide 41 text
hash_behaviour = merge • hash_behaviour=mergeͷڍಈ + group_vars ༏ઌɿ host_vars ༏ઌɿߴ
Slide 42
Slide 42 text
hash_behaviour = merge • hash_behaviour=mergeͷڍಈ + → group_vars ༏ઌɿ host_vars ༏ઌɿߴ key1্ॻ͖ key3͕Ճ
Slide 43
Slide 43 text
hash_behaviour = merge • γʔέϯε(Ϧετ)ཁૉ͕શ෦্ॻ͖͞Εͯ ͠·͏ͷͰҙ + group_vars ༏ઌɿ host_vars ༏ઌɿߴ
Slide 44
Slide 44 text
hash_behaviour = merge • γʔέϯε(Ϧετ)ཁૉ͕શ෦্ॻ͖͞Εͯ ͠·͏ͷͰҙ + → group_vars ༏ઌɿ host_vars ༏ઌɿߴ list͝ͱ্ॻ͖
Slide 45
Slide 45 text
hash_behaviour = merge • มͷޮతͳཧ͕Մೳ
Slide 46
Slide 46 text
hash_behaviour = merge • มͷޮతͳཧ͕Մೳ group_vars/all.yml(શαʔόڞ௨)
Slide 47
Slide 47 text
hash_behaviour = merge • มͷޮతͳཧ͕Մೳ group_vars/all.yml(શαʔόڞ௨) group_vars/webservers.yml(webαʔόڞ௨) +
Slide 48
Slide 48 text
hash_behaviour = merge • มͷޮతͳཧ͕Մೳ group_vars/all.yml(શαʔόڞ௨) group_vars/webservers.yml(webαʔόڞ௨) host_vars/web01.yml(web01ݻ༗ઃఆ) + +
Slide 49
Slide 49 text
hash_behaviour = merge • มͷޮతͳཧ͕Մೳ group_vars/all.yml(શαʔόڞ௨) group_vars/webservers.yml(webαʔόڞ௨) host_vars/web01.yml(web01ݻ༗ઃఆ) ࠷ऴతʹweb01ʹద༻͞ΕΔม → + +
Slide 50
Slide 50 text
hash_behaviour = merge • Γ͗͢ҙ • ༧ظͤ͵্ॻ͖ • ෳgroup_varsͷద༻ॱ • ม͕ࢄ͞Εͯ͠·͏
Slide 51
Slide 51 text
ࡓ͑ͨΓ͔ͨ • PlaybookͱมΛ͚Δ • hash_behaviour = merge • ม,FactҰཡΛग़ྗͯ͠ཧ
Slide 52
Slide 52 text
ม,FactҰཡΛग़ྗͯ͠ཧ
Slide 53
Slide 53 text
ม,FactҰཡΛग़ྗͯ͠ཧ • merge͞ΕͨมΛग़ྗ • Fact ?
Slide 54
Slide 54 text
merge͞ΕͨมΛग़ྗ • templateϞδϡʔϧͰɺlocalhostʹશมΛग़ ྗ͢Δ • શมΛ”my_vars(ҙ)”ҎԼͷ֊ߏʹ͢ Δʢॏཁʂʣ • {{ hostvars[inventory_hostname]["my_vars"] | to_nice_yaml | indent(2, True) }}
Slide 55
Slide 55 text
merge͞ΕͨมΛग़ྗ • Playbook ESZSVOͰग़ྗ
Slide 56
Slide 56 text
merge͞ΕͨมΛग़ྗ • template
Slide 57
Slide 57 text
merge͞ΕͨมΛग़ྗ • ͜Μͳײ͡
Slide 58
Slide 58 text
Fact ? • SetupϞδϡʔϧʹΑΓऩू͞ΕΔରϗετ ͷڥใ • ansible_*ͱͯ͠ࢀরՄೳ
Slide 59
Slide 59 text
͔ͤͬ͘ͳΜͰFactग़ྗ͠·͔͢ • ྫ͑ɺΧʔωϧͷόʔδϣϯͳΒ {{ hostvars[inventory_hostname] [ansible_kernel] }} • ͍ͬͺ͍͋ΔͷͰཧ͍ͨ͠ใΛ͓ΈͰ
Slide 60
Slide 60 text
͔ͤͬ͘ͳΜͰFactग़ྗ͠·͔͢ • ͜Μͳײ͡
Slide 61
Slide 61 text
ࠓͷαʔόͷߏɺ ͜͏ͳͬͯ·͢❤
Slide 62
Slide 62 text
ύϥϝʔλγʔτײʂ
Slide 63
Slide 63 text
͜ΕΛgitͰཧ͢Ε
Slide 64
Slide 64 text
ߏཧʂ
Slide 65
Slide 65 text
·ͱΊ
Slide 66
Slide 66 text
ࡓ͑ͨΓ͔ͨ • PlaybookͱมΛ͚Δ • hash_behaviour = merge • ม,FactҰཡΛग़ྗͯ͠ཧ
Slide 67
Slide 67 text
ࡓ͑ͨΓ͔ͨ • PlaybookͱมΛ͚Δ • hash_behaviour = merge • ม,FactҰཡΛग़ྗͯ͠ཧ →ߏஙखॱͱύϥϝʔλΛׂ
Slide 68
Slide 68 text
ࡓ͑ͨΓ͔ͨ • PlaybookͱมΛ͚Δ • hash_behaviour = merge • ม,FactҰཡΛग़ྗͯ͠ཧ →ߏஙखॱͱύϥϝʔλΛׂ →ύϥϝʔλΛޮతʹཧ
Slide 69
Slide 69 text
ࡓ͑ͨΓ͔ͨ • PlaybookͱมΛ͚Δ • hash_behaviour = merge • ม,FactҰཡΛग़ྗͯ͠ཧ ≒ ύϥϝʔλγʔτ →ߏஙखॱͱύϥϝʔλΛׂ →ύϥϝʔλΛޮతʹཧ
Slide 70
Slide 70 text
ࡓ͑ͨΓ͔ͨ • PlaybookͱมΛ͚Δ • hash_behaviour = merge • ม,FactҰཡΛग़ྗͯ͠ཧ ≒ ύϥϝʔλγʔτˠgitཧ →ߏஙखॱͱύϥϝʔλΛׂ →ύϥϝʔλΛޮతʹཧ
Slide 71
Slide 71 text
એ • ଞʹAnsibleωλॻ͍ͯ·͢ • https://qiita.com/isobecky74
Slide 72
Slide 72 text
Thank you !