Share
Embed
Ansible 入門 #01 (初心者向け) / ansible-entry
2015/09/10 「Ansible 入門 #01 (初心者向け) 」の資料です。
https://atnd.org/events/68155
ハンズオンの答え
https://github.com/uorat/ansible-handson.git
Taro Hirose
September 10, 2015
Transcript
34 slides
-
Ansibleઆ໌ձ 1. Ansibleͱ 2. Ansibleͷಛ 3. Ansibleͷ༻ޠղઆ ※͜ͷࢿྉҎԼͷΑ͏ͳAnsibleॳ৺ऀΛରͱ͍ͯ͠·͢ ✤ AnsibleΛ৮ͬͯΈ͍ͨɺಋೖ͍ͨ͠ͱߟ͍͑ͯΔํ
✤ αʔόʔߏஙͱ͔ͬͯΔํ ✤ αʔόʔσϓϩΠΛޮԽ͍ͨ͠ํ ✤ Chefਏ͍ํ ✤ Ansibleͬͯڹ͖͕ؾʹͳΔํ -
Ansibleͱ “Ansible is Simple IT Automation” SIMPLE AGENTLESS POWERFUL Automate
in hours - not weeks - with Ansible's human-readable IT automation language. Ansible uses SSH instead of agents. More efficient, more secure and less to manage. App deployment, configuration management and orchestration - all from one system. qt: http://www.ansible.com/home -
ͳͥΔͷʁ 1) ߏཧ ✤ υΩϡϝϯτ͕ݹ͍ ✤ ߋ৽໘ ✤ Snapshot, AMI͕ൿͷλϨԽ
✤ Կ͕ಈ͍͍ͯΔͷ͔͔Βͳ͍ ✤ ·ͬ͞ΒͳOS͔ΒڥߏஙͰ͖ ͳ͍ ✤ ͜ͷαʔόʮͲ͏͋Δ͖ʯͳͷ ͔ -
ͳͥΔͷʁ 2) ࣗಈԽ ✤ Ճ͢ΔϏδωε ✤ ॳಈͰ͖Δ͚ͩૣ͘ ✤ ՃՁ૿ʹूத͍ͨ͠ ✤
ఆৗۀίετͰ ✤ ΞϓϦέʔγϣϯσϓϩΠ ✤ ੬ऑੑରԠ -
Ansibleͷಛ “Ansible is Simple IT Automation” SIMPLE AGENTLESS POWERFUL Automate
in hours - not weeks - with Ansible's human-readable IT automation language. Ansible uses SSH instead of agents. More efficient, more secure and less to manage. App deployment, configuration management and orchestration - all from one system. qt: http://www.ansible.com/home -
Ansibleͷಛ “Ansible is Simple IT Automation” ✤ ఆٛYAMLʢ㲈ઃఆϑΝΠϧʣ ✤ ϓϩάϥϜͰ͢Βແ͍ͨΊֶशোน͕গͳ͍
SIMPLE Automate in hours - not weeks - with Ansible's human-readable IT automation language. --- - hosts: webservers tasks: - name: yum install nginx yum: pkg=nginx state=installed -
Ansibleͷಛ “Ansible is Simple IT Automation” ✤ agentΠϯετʔϧෆཁ ✤ ӡ༻ཧαʔόͰansible*ίϚϯυ͕͑ΕOK
✤ Python͕ೖ͍ͬͯΕಋೖՄೳ ✤ ઐ༻ͷserveragentͷӡ༻ཧෆཁ ✤ ৗற͢Δϓϩηεͳ͠ AGENTLESS Ansible uses SSH instead of agents. More efficient, more secure and less to manage. -
Ansibleͷಛ “Ansible is Simple IT Automation” ✤ ߏཧ͚ͩͰͳ͘Orchestrationͱͯ͑͠ΔͷͰɺ ͪΐͬͱͨ͠ૢ࡞ௐࠪͷࢧԉπʔϧʹͳΔ ✤
ߴػೳ͕ͩɺεϞʔϧελʔτͰ࢝ΊΔ͜ͱՄೳ POWERFUL App deployment, configuration management and orchestration - all from one system. ॊೈͳ࣮ߦํ๏ ✤ ࢦఆͨ͠λΠϛϯάͰ࣮ߦ ✤ ෳฒྻ࣮ߦ ✤ ඞཁͳॲཧ͚ͩΛone-linerͰadhocʹ࣮ߦ ✤ Pushܕ͚ͩͰͳ͘Pullܕαϙʔτ (ansible-pull) -
Ansibleͷಛ “Ansible is Simple IT Automation” ႈੑ ✤ هࡌͨ͠DSLͷఆٛͷঢ়ଶʹऩଋͯ͘͠ΕΔ ✤
͋Δૢ࡞ΛԿճߦͬͯ݁Ռಉ͡ ✤ มߋ͕ͳ͚ΕॲཧΛεΩοϓ͢Δ ҎԼ͕Πϯετʔϧ͞Ε͍ͯΔ͜ͱ ɾnginx ɾjava ɾzabbix_agentd αʔϏε͕ىಈ͍ͯ͠Δ͜ͱ ɾnginx ɾzabbix_agentd -
ओͳίϚϯυ ✤ ansible ✤ Adhocʹ࣮ߦ͢Δ ✤ ansible-playbook ✤ ࡞ͨ͠ఆٛ (playbook)
Λ࣮ߦ͢Δ # ίϚϯυ࣮ߦྫ $ ansible -m 'ping' webservers ansible-test02 | success >> { "changed": false, "ping": "pong" } # dry-run $ ansible-playbook hands-on00.yml --check --diff … PLAY RECAP ******************************************************************** ansible-test02 : ok=2 changed=0 unreachable=0 failed=0 # execute $ ansible-playbook hands-on00.yml … PLAY RECAP ******************************************************************** ansible-test02 : ok=2 changed=0 unreachable=0 failed=0 -
Inventory Host AnsibleͰཧ͢ΔϗετΛiniܗࣜͰهࡌ͢Δ ✤ ϗετͷάϧʔϐϯά͕Մೳ # ྫ: example.com αΠτΛAnsibleͰཧ͢Δ mail.example.com
[webservers] foo.example.com bar.example.com -
Playbook ࣮ߦ͢Δॲཧͷ༰ΛYAMLܗࣜͰهࡌ͢Δ ✤ ࠷ॳplaybookͱinventry host͑͋͞ΕOK --- - hosts: webservers tasks:
- name: ensure apache is at the latest version yum: pkg=httpd state=latest - name: write the apache config file template: src=templates/httpd.j2 dest=/etc/httpd.conf notify: - restart apache - name: ensure apache is running (and enable it at boot) service: name=httpd state=started enabled=yes handlers: - name: restart apache service: name=httpd state=restarted -
Task (Tasks lists) ۩ମతͳॲཧΛɺ࣮ߦ͢ΔॱʹྻͰهࡌ͢Δ ✤ جຊ name ͱ module ͷηοτΛهࡌ͢Δ
✤ ࣮ߦ݅τϦΨʔ (notify/handler) ΛՃ͑Δ͜ͱՄೳ ✤ ࣮ߦ݅: [when, changed_when, ignore_errors, always_runͳͲ] ✤ τϦΨʔ: handlers Ͱهࡌͨ͠ॲཧͷ໊લΛnotifyͰࢦఆ --- - hosts: webservers tasks: - name: ensure apache is at the latest version yum: pkg=httpd state=latest - name: write the apache config file template: src=templates/httpd.j2 dest=/etc/httpd.conf notify: - restart apache - name: ensure apache is running (and enable it at boot) service: name=httpd state=started enabled=yes handlers: - name: restart apache service: name=httpd state=restarted -
Module TasksͰࢦఆͨ͠ॲཧΛ࣮ߦ͢Δ൚༻ϥΠϒϥϦ ✤ Modules are “idempotent” (ႈੑ) ✤ Core Modules͚ͩͰඞཁͳ࡞ۀେମΧόʔͰ͖Δ
--- - hosts: webservers tasks: - name: ensure apache is at the latest version yum: pkg=httpd state=latest - name: write the apache config file template: src=templates/httpd.j2 dest=/etc/httpd.conf notify: - restart apache - name: ensure apache is running (and enable it at boot) service: name=httpd state=started enabled=yes handlers: - name: restart apache service: name=httpd state=restarted -
දతͳModule ✤ yum / apt : ύοέʔδཧ (Πϯετʔϧ, আͳͲ) ✤
service : αʔϏεૢ࡞ (ىಈ, ఀࢭ, ࠶ىಈͳͲ) ✤ file : ϑΝΠϧૢ࡞ (࡞, আ, ଐੑมߋͳͲ) ✤ copy : ϑΝΠϧΛίϐʔ ✤ template : ϑΝΠϧΛܗͱͯ͠ϑΝΠϧੜ (ޙड़) ✤ shell : ҙͷshellίϚϯυΛ࣮ߦʢ˞ႈੑࣗͰ୲อʣ ✤ ۩ମతͳૢ࡞ͷଞɺ“register” Λซ༻ͯ݅͠ذʹར༻͢Δͷ͍ํ͋Δ ެࣜυΩϡϝϯτʹModuleͷҰཡͱઆ໌͕͋ΔͷͰɺৄ͘͠ҎԼΛࢀর͍ͩ͘͞ɻ http://docs.ansible.com/ansible/modules_by_category.html -
ڞ௨Խ (Include / Role) ✤ Include: ✤ taskshanderlsΛผϑΝΠϧͱͯ͠Γग़͠ɺҰͭͷRole ͱͯ͠·ͱΊ͓ͯ͘͜ͱͰ࠶ར༻͘͢͢͠Δ ---
- hosts: webservers tasks: - include: apache.yml - include: deploy.yml site=service_A - include: deploy.yml site=service_B -
ڞ௨Խ (Include / Role) ✤ Role: ✤ task, vars, files,
templates, handlersҰࣜΛڞ௨Խ ✤ playbook͔Β roles ҰൃͰݺͼग़͠Ͱ͖Δ ✤ ෳαʔόʹద༻͢Δɺ൚Խͤ͞ΔͳͲͷ༻్Ͱ׆༻ # playbook --- - hosts: webservers roles: - common - webserver # σΟϨΫτϦߏ hosts webservers.yml roles/ common/ files/ templates/ tasks/ handlers/ vars/ defaults/ meta/ webservers/ … -
ڞ௨Խ (Template / Variable) ✤ Template: ✤ มΛbindͯ͠Ͱ͖Δ ✤ ϗετຖʹҟͳΔΑ͏ͳϑΝΠϧΛ͢Δͱ͖ʹ׆༻
✤ ॻࣜ “Jinja2” # files/etc/motd.j2 ################################################## !!! Production server {{ ansible_hostname }} !!! ################################################## __| __|_ ) _| ( / Amazon Linux AMI ___|\___|___| https://aws.amazon.com/amazon-linux-ami/2015.03-release-notes/ -
ڞ௨Խ (Template / Variable) ✤ Variable: ✤ ݅ذTemplateͰར༻͢ΔมΛఆٛ͠ݺग़Մೳ ✤ ॻࣜYAML
✤ playbook, inventry host, varsͳͲ༷ʑͳՕॴͰࢦఆՄೳ ✤ OSͷԼճΓͷใansible͕ࣗಈͰऔಘ͠ݺग़Մೳ (Facts) ✤ ར༻Մೳͳม `ansible hostname -m setup` Ͱ֬ೝՄೳ ✤ ChefͰݴ͏ ohai # Variables innodb_buffer_pool_size: 6144M innodb_log_file_size: 2048M max_connections: 1000 … $ ansible ansible-test02 -m 'setup' ansible-test02 | success >> { "ansible_facts": { "ansible_all_ipv4_addresses": [ "172.31.xx.xx" ], "ansible_all_ipv6_addresses": [], "ansible_architecture": "x86_64", "ansible_bios_date": "NA", "ansible_bios_version": "NA", "ansible_cmdline": { "KEYTABLE": "us", "LANG": "ja-JP.UTF-8", -
Installation 1. αʔόϩάΠϯ 1. αʔό (Ansible: 1, Target: 1) ͷSSHଓ
✤ ଓใݸผʹ͓͠͠·͢ 2. Ansible࣮ߦ༻ͷ伴࡞ͱ 1. Ansibleαʔό্Ͱ`ssh-keygen` Ͱ࡞ (ύεϑϨʔζͳ͠) 2. ec2-user@target:/home/ec2-user/.ssh/authorized_keys ʹ ެ։伴 (id_rsa.pub) Λcopy 3. Ansibleαʔόͷ /etc/hosts ʹ TargetαʔόΛ PrivateIP Ͱهࡌ ( `sudo vim /etc/hosts` ) 4. Ansible → Target ͷsshଓ֬ೝ 3. AnsibleΠϯετʔϧ 1. `sudo pip install ansible` 2. `sudo mkdir /etc/ansible && sudo chown ec2-user:ec2-user /etc/ansible && cd /etc/ ansible` 3. `touch hosts` 4. `wget https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg ` -
Ansibleख࢝Ί 1. Inventory HostΛهࡌ 2. AnsibleίϚϯυΛࢼ͠ଧͪ ✤ `ansible TARGET -m
'ping'` ✤ `ansible TARGET -m 'setup'` # /etc/ansible/hosts [webservers] ansible-target0x -
BestPractice Best Practices Directory Layout - Ansible Documentation production #
Inventory Hostάϧʔϓ͝ͱʹ࡞ staging # ʏ group_vars/ # άϧʔϓ༻ͷมͷ֨ೲॴ group1 # group2 # host_vars/ # ϗετ༻ͷมͷ֨ೲॴ hostname1 # hostname2 # library/ # ࣗ࡞Moduleͷ֨ೲॴ (optional) filter_plugins/ # ࣗ࡞Filter Pluginͷ֨ೲॴ (optional) site.yml # master Playbook webservers.yml # Playbook dbservers.yml # Playbook roles/ # ϩʔϧ (Role) ͷ֨ೲॴ common/ # “common” ϩʔϧ tasks/ # Taskͷ֨ೲॴ main.yml # <-- Task͕͠ଟ͘ͳΔ߹খ͚͘͞Δ handlers/ # Handlerͷ֨ೲॴ main.yml # <-- ʏ templates/ # Templateͷ֨ೲॴ ntp.conf.j2 # <------- TemplateϑΝΠϧ໊ *.j2 files/ # Fileͷ֨ೲॴ bar.txt # foo.sh # vars/ # Varsͷ֨ೲॴ main.yml # defaults/ # main.yml # VarsͷσϑΥ (group/host_varsͰ্ॻ͖Մ) meta/ # ϩʔϧͷґଘ͕ؔ͋Εఆٛ -
Hands On1(20min) 1. ӡ༻πʔϧΛΠϯετʔϧ ✤ telnet, wget, rsync, tree, tcpdump,
sysstat, dstat, vim-enhanced, git, htop 2. όφʔΛஔ ✤ /etc/motd 3. Nginx install ✤ yum install ✤ ࣗಈىಈ༗ޮ 4. Deploy ✤ page upload (దͳhtml) -
Hands On3(30min) 1. SwapfileΛ࡞ ✤ 512MBͷswapfileΛ࡞ ✤ swaponͰεϫοϓՃ ✤ boot࣌ʹࣗಈϚϯτ
✤ ※ႈੑ୲อ͢Δ͜ͱ 2. ӡ༻πʔϧΛՃ ✤ htop, glances, tmux ✤ ag (the-silver-searcher) 3. Deploy (from github) ✤ yteraoka/ansible-tutorial ✤ /var/www/ ҎԼʹdeploy ✤ NginxઃఆϑΝΠϧฤूˠrestart