Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ansible Module と Plugin ざっくり入門/cirasu_ansible_t...
Search
Yohei Kawahara
March 30, 2017
0
9.5k
Ansible Module と Plugin ざっくり入門/cirasu_ansible_tettei_nyumon_0
CIRASU Ansible 徹底入門輪読会 #0 で話す予定の資料。(勉強会開催までの間で修正する予定)
Yohei Kawahara
March 30, 2017
Tweet
Share
More Decks by Yohei Kawahara
See All by Yohei Kawahara
20191002_CircleCIMeetupFukuoka#2
inokappa
1
820
kix_009_20190429
inokappa
0
820
2019-04-18 CircleCI Meetup Fukuoka #1
inokappa
2
8k
開発で使える AWS の API 操作を模倣する (OSS) ツールを探してみた/JAWS-UG Kagoshima Vol.8
inokappa
1
1.2k
隙間時間を使って exercism.io を始め隊 | リモートで勉強会 #2
inokappa
0
410
Congratulations fukuokarb#100
inokappa
0
3.9k
moto を 'もっと' 使おう/PyFukuoka#4
inokappa
0
4.5k
github-enterprise-user-kai-desuyone
inokappa
0
100
Elixir and AWS Tutorial
inokappa
0
550
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
33
3k
The MySQL Ecosystem @ GitHub 2015
samlambert
250
12k
Fontdeck: Realign not Redesign
paulrobertlloyd
82
5.3k
Mobile First: as difficult as doing things right
swwweet
222
9k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Automating Front-end Workflow
addyosmani
1366
200k
Documentation Writing (for coders)
carmenintech
66
4.5k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
For a Future-Friendly Web
brad_frost
175
9.4k
Into the Great Unknown - MozCon
thekraken
33
1.5k
We Have a Design System, Now What?
morganepeng
51
7.3k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Transcript
Ansible ͷ Module ͱ Plugin ͬ͘͟Γೖ CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
1 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
͡Ίʹ 2 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
͋Μͨɺ୭Ͷʁ • ݪ ༸ฏ • ΞΠϨοτגࣜձࣾ cloudpack ࣄۀ෦ • τΡΠολʔ
: @inokappa • ϑΣΠεϒοΫ : inokappa • ͖ͳതଟห : Α͔Ζ͏Μ Ansible ެࢲڞʹ͋Μ·Γͬͯ·ͤ Μ...͍͢·ͤΜ. 3 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
CIRASU ͬͯͳΜͶʁ ԬͰ Infrastructure as code Configuration as code
ɺSite Reliability Engineering (SRE)ɺDevOps ͳͲΠϯϑϥٕज़ऀΛऔΓר ٕ͘ज़ɾӡ༻ʹ͍ͭͯͷใΛ ΏΔʙ͘ ڞ༗ɾษڧ͢Δίϛϡχ ςΟͰ͢. 4 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
ࠓͷ͠ͳΜͶʁ Ansible పఈೖΛಡΜͰ͍ͯ... • Module ͷཧղ͢Δ͜ͱͰ Ansible ͷཧղ͕ਂ·Γͦ͏... • Plugin
ͬͯ Module ͷԿ͕ҧ͏Μʁ ͱ͍͏ࢥ͍͕ϑπϑπͱ༙͍͖ͯͨͷͰɺModule ͱ Plugin ʹ͍ͭ ͯগ͚ͩ͠۷ΓԼ͛ͯΈ·ͨ͠. 5 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
ͰɺͲΜͳ͠Λͬ͢ͱʁ • Module ͬͯʁ • Module ͷछྨ / Module ͷ࣮
/ Module ͷݴޠผ࣮ྫ • Plugin ͬͯʁ • For example Ansible Plugin / Plugin ͷ࣮ • ·ͱΊ • ͕࣌ؒ͋ͬͨΒ͓·͚ 6 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
ຊࢿྉͰར༻ͨ͠ڥ ຊࢿྉͰར༻ͨ͠ Python ٴͼ Ansible όʔδϣϯҎԼͷ௨Γ. $ python --version Python
2.7.13 $ ansible --version ansible 2.2.1.0 config file = /path/to/.ansible.cfg configured module search path = Default w/o overrides 7 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Module ͬͯʁ 8 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Module ͬͯʁ(1) • Ansible పఈೖͷݴ༿Λഈआ͢Δͱ...Ansible ͔Β࣮ߦ͞ΕΔί ϚϯυͷΑ͏ͳͷ • 750 छྨͷϞδϡʔϧ͕ଘࡏ͍ͯ͠Δ(2017
01 ݄࣌) • ansible-doc -l ͰϞδϡʔϧͷҰཡΛ֬ೝ͢Δ͜ͱ͕ग़དྷΔ • ansible-doc ${Ϟδϡʔϧ໊} ͰϞδϡʔϧͷৄࡉΛ֬ೝ͢Δ ͜ͱ͕ग़དྷΔ 9 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Module ͬͯʁ(2) - name: Install Nginx yum: # Yum Module
name: nginx state: present - name: Start nginx service: # Service Module name: nginx state: started 10 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Module ͷछྨ(1) Github ϦϙδτϦͷ lib/ansible/modules/ ͰҎԼͷΑ͏ʹΧςΰ ϥΠζ͞Ε͍ͯΔ. • cloud /
clustering / commands / crypto / database • files / identity / inventory / messaging / monitoring 11 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Module ͷछྨ(2) • network / notification / packaging / remote_management
/ source_control • storage / system / utilities / web_infrastructure / windows 12 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Module ͷ࣮ 13 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
ཁ݅ • ݴޠΛΘͳ͍͕ɺϑΝΠϧ I/O ͱඪ४ग़ྗͷग़ྗ͕ඞཁ • Python Ͱ࣮͢Δ߹ϔϧύʔΫϥε͕ఏڙ͞Ε͍ͯΔ • Module
ͷ࣮ߦ݁Ռ JSON ϑΥʔϚοτͰग़ྗ͢Δඞཁ͕͋Δ 14 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Ϟδϡʔϧͷઃஔ • ΧϨϯτσΟϨΫτϦͷ library σΟϨΫτϦ • ڥม ANSIBLE_LIBRARY Ͱࢦఆ͞Εͨύε •
ansible.cfg Ͱࢦఆ͞Εͨύε • --module-path Ͱࢦఆ͞Εͨύε 15 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
ऴྃεςʔλε(1) • ޭ {"changed": true} • มߋແ͠ {"changed": false} •
ࣦഊ {"failed" : true} 16 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
ऴྃεςʔλε(2) 17 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Ҿॲཧ(ansible ίϚϯυ) key=value ϑΥʔϚοτͰϞδϡʔϧʹҾΛ͢. ansible -M . localhost -a 'key1=value1
key2=value2' 18 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Ҿॲཧ(Playbook) Playbook ͰҎԼͷΑ͏ʹҾΛ͢. - my_module: key1: 'value1' key2: 'value2' 19
CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Ҿॲཧ(ݴޠຖͷॲཧ) Python Ҏ֎Ͱ࣮͢Δ߹ɺࣗલͰ args ϑΝΠϧΛಡΈࠐΜͰղ ੳͯ͠εΫϦϓτʹͯ͋͛͠Δඞཁ͕͋Δ. #!/usr/bin/env bash source $(dirname
$0)/args echo "{\"key1\":\"${key1}\",\"key2\":\"${key2}\"}" 20 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Ҿॲཧ(ݴޠຖͷॲཧ) Python Ͱ࣮͢Δ߹ʹ AnsibleModule ΫϥεΛར༻͢Δ͜ͱ ͰɺଞͷݴޠͱൺΔͱॲཧָ͕ʹͳΔ. def main(): ... module
= AnsibleModule( argument_spec=fields, supports_check_mode=True ) response = { "key1": module.params['key1'], "key2": module.params['key2'] } 21 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Module ͷݴޠผ࣮ྫ /tmp/ ҎԼʹҾͰࢦఆͨ͠ϑΝΠϧ໊ͷϑΝΠϧΛ࡞͢Δྫ 22 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Bash de ࣮ #!/usr/bin/env bash source $(dirname $0)/args if [
! -f "/tmp/${file_name}" ];then touch /tmp/${file_name} if [ $? == "0" ];then echo '{"changed": true}' else echo '{"failed": true}' fi else echo '{"changed": false}' fi 23 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Ruby de ࣮ #!/usr/bin/env ruby require 'json' args_values = ''
File.readlines(File.dirname(__FILE__) + '/args').each { |line| args_values = line.split(' ') } args_values.each { |value| v = value.split('=') ; ENV[v[0]] = v[1] } if File.exist?('/tmp/' + ENV['file_name']) puts JSON.generate({ "changed" => false }) else File.open('/tmp/' + ENV['file_name'], 'w').close() if File.exist?('/tmp/' + ENV['file_name']) puts JSON.generate({ "changed" => true }) else puts JSON.generate({ "failed" => true }) end end 24 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Python de ࣮ #!/usr/bin/python import os def main(): fields =
{ "file_name": { "required": True, "type": "str" } } module = AnsibleModule(argument_spec=fields, supports_check_mode=True) if os.path.exists('/tmp/' + module.params['file_name']): module.exit_json(changed=False) else: open('/tmp/' + module.params['file_name'], 'w').close() if os.path.exists('/tmp/' + module.params['file_name']): module.exit_json(changed=True) else: module.fail_json(msg='Error.') from ansible.module_utils.basic import * if __name__ == '__main__': main() 25 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Playbook --- - hosts: localhost tasks: - name: Bash Example
bash_module: file_name: bash_file - name: Ruby Example ruby_module: file_name: ruby_file - name: Python Example python_module: file_name: python_file 26 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Plugin ͬͯʁ 27 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Plugin ͬͯʁ(1) • Ansible ͷڍಈΛαϙʔτ͢Δπʔϧ܈(ͱ͍͏ೝࣝˡྑ͍දݱ͕ ݟ͔ͭΒͳ͔ͬͨ) 28 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ
#0
Plugin ͬͯʁ(2) • ྫ͑ɺPlaybook ࣮ߦ࣌ͷҎԼͷΑ͏ͳग़ྗ Callback Plugin ͷ default.py ͕୲͍ͬͯΔ
29 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Plugin ͷछྨ(1) Plugin ҎԼͷΑ͏ͳछྨ͕͋Δ. • Action plugins • Cache plugins
• Callback plugins • Connection plugins 30 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Plugin ͷछྨ(2) • Filters plugins • Lookup plugins • Strategy
plugins • Shell plugins • Test plugins • Vars plugins 31 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
For example Ansible Plugin 32 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Connetcion Plugins ֤छΠϯϕϯτϦϗετͱ௨৴Λߦ͏ҝͷϓϥάΠϯ. • SSH • Docker • LXC /
LXD • WinRM • etc... 33 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Callback Plugins ֤छΠϕϯτΛϑοΫͯ͠ผͷॲཧΛߦΘͤΔϓϥάΠϯ. • Mail • Syslog(syslog_json) • Slack •
Hipchat • etc... 34 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Plugin ͷ࣮ Callback Plugin ͷ߹ 35 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
ཁ݅ • Callback Plugin ʹݶΒͣ Python Ͱ࣮͢Δඞཁ͕͋Δ • Callback ΫϥεΛܧঝͯ͠৽͍͠ΫϥεΛ࡞͢Δ
... from ansible.plugins.callback import CallbackBase class CallbackModule(CallbackBase): """ ansible CloudWatch Logs callback plugin ansible.cfg: ... 36 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Plugin ͷઃஔ(1) • ansible.cfg Ͱࢦఆͨ͠ callback_plugins σΟϨΫτϦʹϓ ϥάΠϯίʔυΛઃஔ͢Δ • Role
Ͱݺͼग़͢͜ͱग़དྷΔ [default] callback_plugins = ~/path/to/plugins callback_whitelist = cloudwatch_logs 37 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Plugin ͷઃஔ(2) • callback_whitelist ʹར༻͍ͨ͠ϓϥάΠϯ໊Λࢦఆ͢Δ(σ ϑΥϧτͰຆͲͷϓϥάΠϯར༻ग़དྷͳ͍ঢ়ଶʹͳ͍ͬͯ Δ) • Ϟδϡʔϧ໊ͷӳࣈॱʹಡΈࠐ·ΕΔ 38
CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
ؾʹͳΔ • CALLBACK_VERSION ͱ CALLBACK_NAME ͷఆ͕ٛແ͍ͱ Ansible 2.0 Ҏ্ͩͱਖ਼ৗʹಈ࡞͠ͳ͍ͱυΩϡϝϯτʹॻ͔ΕΕ͍ͯΔ ͕ɺखݩͰࢼͨ͠Βఆ͕ٛແͯ͘ಈ͍ͯ͠·ͬͨ...ͳΜͰ
Ζ... • ansible.cfg ͷ callback_whitelist ʹར༻͍ͨ͠ϓϥάΠϯ໊ Λఆٛ͢Δඞཁ͕͋Δͱॻ͔Ε͍ͯΔ͕ɺखݩͰࢼͨ͠Βఆٛ ͕ແͯ͘ಈ͍ͯ͠·ͬͨ...ͳΜͰΖ... 39 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
·ͱΊ 40 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Module • Β Playbook Λॻ͘͜ͱͰ Module Λૢ࡞͍ͯͨ͜͠ͱʹͳ Δ • Module
ͷڍಈΛཧղ͢Δ͜ͱ Playbook ͷڍಈΛཧղ͢Δۙ ಓʹͳΔ • ࣮ʹݴޠΘͳ͍͚Ͳ Python ͩͱָʹ࣮ग़དྷΔ(ͱࢥ͏) • ႈੑ Module ͷͱͳΔ 41 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Plugin • Ansible Pluggable ͳ࣮ʹͳ͍ͬͯΔΜͩͳ͋ • Plugin Λཧղ͢ΔͱɺAnsible ΛΑΓ͍͜ͳ͢͜ͱ͕ग़དྷΔΑ
͏ʹͳΔͱࢥ͏ • ҙࣝͤͣʹར༻͍ͯ͠Δ߹͋Δ(Callback Plugin ͷ default.py ͱ͔) • ࣮ Python ͷΈ 42 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
͕࣌ؒ͋Ε͓·͚ 43 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Datadog Tags module Datadog Ͱࢹ͍ͯ͠Δ Host ʹλάΛ༩ͨ͠Γɺআͨ͠Γ͢ ΔϞδϡʔϧ. • https:/
/github.com/inokappa/ansible-sandbox/blob/master/library/ datadog_tags.py 44 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
CloudWatch Logs plugin Playbook ͷ࣮ߦ݁ՌΛ CloudWatch Logs ʹૹ৴͢Δ callback plugin.
• https:/ /github.com/inokappa/ansible-sandbox/blob/master/ plugins/cloudwatch_logs.py 45 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
Playbook - hosts: localhost tasks: - name: Test datadog_tags(present) datadog_tags:
state: present host: myhost tags: 'aa,bb,cc,dd' api_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx app_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx - name: Test datadog_tags(absent) datadog_tags: state: absent host: myhost api_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx app_key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 46 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
ࢀߟࢿྉ • Ansible పఈೖ ! • Building A Simple Module
! • Developing Plugins ! • AnsibleͷϞδϡʔϧ։ൃʢجૅฤʣ ! • ॳΊͯͷAnsibleʢ10ষɿΧελϜϞδϡʔϧʣ ! • Datadog API Reference " 47 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0
͓ΘΓ 48 CIRASUʢ͠Β͢ʣAnsible పఈೖ ྠಡձ #0