Share
Embed
Automate with Ansible Basic (3e)
# 現代 IT 人一定要知道的 Ansible 自動化組態技巧 (3/e)
Ansible basic training (3/e) [1] for TOSSUG meetup [2].
[1]: 1st: https://speakerdeck.com/chusiang/xian-dai-it-ren-ding-yao-zhi-dao-de-ansible-zi-dong-hua-zu-tai-ji-qiao
[2]: http://tossug.kktix.cc/events/b3ebffa1-363c69
Chu-Siang Lai
March 22, 2017
Transcript
50 slides
-
[ chusiang@tossug ~ ] $ cat .profile # Author: 㲺Ռᗼ
/ chusiang (at) drx.tw # Blog: http://note.drx.tw # Modified: 2017-03-22 13:44 3/e -
橕ෝ౯ • 㲺Ռᗼ (@chusiang_lai)牐 • 1 ଙ犥Ӥጱ Ansible ֵአ妿涢牐 •
ፓ獮姘虁ጱ Roles 磪物 • gitbook • php7 (php-fpm) • switch-apt-mirror • testlink • vim-and-vi-mode • zabbix-agent 2 -
Outline I. 匍դ IT ՈฎՋ讕牫 II. 疩獈ᛔ㵕玕奲眲ጱঅ蒂ฎՋ讕牫 III. Ansible ฎՋ讕牫
IV. ெ讕蟂ᗟ Ansible 絑ह牫 7 -
Outline I. 匍դ IT ՈฎՋ讕牫 II. 疩獈ᛔ㵕玕奲眲ጱঅ蒂ฎՋ讕牫 III. Ansible ฎՋ讕牫
IV. ெ讕蟂ᗟ Ansible 絑ह牫 V. ெ讕砺֢ Ansible牫 8 -
Outline I. 匍դ IT ՈฎՋ讕牫 II. 疩獈ᛔ㵕玕奲眲ጱঅ蒂ฎՋ讕牫 III. Ansible ฎՋ讕牫
IV. ெ讕蟂ᗟ Ansible 絑ह牫 V. ெ讕砺֢ Ansible牫 VI. Q & A 9 -
DevOps ฎՋ讕牫 Agile Tour ݣӾ by ྋ紪 x Mina @
2017.01.14 11 -
匍դ IT ՈฎՋ讕牫 12 犥獮ጱ IT Ո 匍դጱ IT Ո
ℂ蕕秚ک礍ᒊ 襑聻揲碍ੜ碻 (hr) 犋አ 30 獤 (min) Ӥ紑 ྯॠළළ瞲犤牏蕕蕕秚瑊牧 ଉଉ盛ԧ硬螂Ջ讕 䌃 code 吚者翄 䌃 code ᓕ秚瑊 ӥ紑 䌃犋ਠጱૡ֢෭懿 䒻ᛔ૩䌃ૡٍ (傶ԧӥ紑) -
疩獈 Ansible ᛔ㵕玕奲眲牧 ౯㮉ݢ犥仂๐率Ӿ䥁碻樌 牏介手च器ୌ戔牏褔犵क़ 觓檺牧ๅݢ犥虏樄咳牏介手 ྋୗ絑ह篷翿矑敍牐 瑽粙㬵რ - Ansible
as Automation Glue 14 -
"ૡՈ" 奲眲 ᛔ㵕玕奲眲 ᯿蕦奲眲ጱՈێ౮ ṛ 犵 Ո傶०藮觓檺 ṛ 犵 ݢ介手
櫞 ฃ 秇奲玕 櫞 ฃ ӥ紑 櫞 ฃ 疩獈ᛔ㵕玕奲眲ጱঅ蒂ฎՋ讕牫 15 -
Ansible ฎ蜱ଙ㬵Ꭳݷଶ犋 䥁Ӥ܋ጱ DevOps ᛔ㵕玕 敟誢牧櫒簁ᛔ 2013 ଙ獺缏ᛗ 犡犋ک 4
ଙ牧֕ኧෝٌ䟖አ 篷դቘ纷ୗጱ礍䯤牧蟂ᗟ 覄ၚ牧纷ୗ嘨ฃ捝牧ࢩᘒ 蜫蝧౮傶ݑ喠ፓጱ DevOps ૡٍ牐 iThome - http://goo.gl/yJbWtz 18 -
Ansible ฎՋ讕牫 • 膏 Puppet, SaltStack, Chef 㪔ڜٌࢥጱᛔ㵕玕奲眲戔ਧ ૡٍ (Infrastructure
as Code)牧ٌ墋㻌ฃአጱ粬虏ՈӞ አ疰眢Ӥ牧ࣁ DevOps ኴ犖㬟磪Ӟଅԏ瑿牐 • ֵአ Push 礍䯤牧ݝ襑 Python SSH ܨݢ砺֢牧犋አ氃 क़蕕 Angent牐 • Python 檋籧ጱ奲眲戔ਧૡٍ牐 19 -
ெ讕ਞ蕕 Ansible牫 • ݝ襑ࣁ Control Machine ਞ蕕 Ansible牪Managed node 㳷ᥝ磪
Python 2.5+ SSH牐 22 # Debian & Ubuntu (apt). $ sudo apt-get install ansible # RHEL & CentOS (yum). $ sudo yum install ansible # Mac OS X (homebrew). $ brew install ansible # Python (pip). $ sudo pip install ansible -
ெ讕戔ਧ Ansible牫 • 萞ኧ ansible.cfg 㬵戔ਧ inventory (host file) 䲆礯᪠䕩牏
Managed node (ᤩ矒ᒒ) ֵአᘏݷ圸牏SSH ᰂ槄 … 缛牐 23 $ vim ansible.cfg [defaults] # 瞲ਧ inventory 䲆礯᪠䕩牐 hostfile = hosts # 螐ᒒֵአᘏݷ圸 remote_user = docker #private_key_file = ~/.ssh/id_rsa # host_key_checking: 犋扇㺔ے獈 ssh ᰂ槄 host_key_checking = False -
inventory ฎՋ讕牫 • Ԇᥝአ㬵ਧ嬝 Managed node (ᤩ矒ᒒ) Ԇ秚֖࣎膏ᗭ奲牧 犖ݢአ㬵戔ਧ ssh
蝫娄虻懱牐 24 $ vim hosts # ansible_ssh_host: 螐ᒒ SSH Ԇ秚֖࣎牐 # ansible_ssh_port: 螐ᒒ SSH ओݗ (Port)牐 # ansible_ssh_user: 螐ᒒ SSH ֵአᘏݷ圸牐 # ansible_ssh_private_key_file: 秚 SSH ᐺ槄䲆᪠䕩牐 # ansible_ssh_pass: 螐ᒒ SSH ੂ嘨 (ୌ捍硬አᐺ槄)牐 [dev] ansible-demo ansible_ssh_host=127.0.0.1 ansible_ssh_pass=pwd [test] ansible-test ansible_ssh_host=172.10.10.1 ansible_ssh_port=2222 [prod] ansible-prod ansible_ssh_host=10.10.10.1 ansible_ssh_user=deploy -
Ad-Hoc command ฎՋ讕牫 • 墋Ꭸ (屷碻) ጱ瞲犤牧膏Ӟ膢ጱ command line 砺֢秇ୗ
襊ݶ牧Ӟ稞ݝ蝚螂Ӟᤈ瞲犤蝱ᤈ砺֢牐 27 # Ӟ膢ጱ command line $ ping ansible-demo.local PING localhost (127.0.0.1): 56 data bytes 64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.037 ms --- localhost ping statistics --- 1 packets transmitted, 1 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 0.037/0.037/0.037/0.000 ms $ echo Hello World Hello World -
Ad-Hoc command ฎՋ讕牫 • -m 盅ጱݱ殻㷢碍藶㷢ᘍ Ansible ਥොጱ Module Index
կ牐 28 # ansible <Ԇ秚ݷ圸> -m <ݱ殻㷢碍> $ ansible all -m ping ansible-demo.local | SUCCESS => { "changed": false, "ping": "pong" } $ ansible all -m command -a "echo Hello World" ansible-demo.local | SUCCESS | rc=0 >> Hello World -
Playbooks ฎՋ讕牫 • 穉 Shell Script ๅٍ奾䯤玕ጱ脻 承牧ฎӞ棎蟂ᗟጱঅᇔ牐 • ֵአ
YAML ໒ୗ牧䌃 code 疰 ইݶ䌃կ牧墋㻌ฃ捝牐 • 蝢ଉ䨝磪 Play, Task, Module牐 • ݢֵአ Jinja2 (template 羬翄) 蔭螈ୗ牧㪔ඪൔ虋碍牏ڣ䥁ୗ 牏蝅瑹缛承ဩ牐 Baby Playbook Onesie - http://goo.gl/GKJvXn 29 -
Playbooks ฎՋ讕牫 • Ӟ犩 Playbook ݢ犥磪ग़㮆 Play ग़㮆 Tasks牐
• 種ֺአکԧ Play*1, Task*3 Module*3 (command, apt, lineinfile)牐 30 $ vim example.yml --- - name: This is a Super-basic playbook. hosts: all tasks: - name: Hello World command: echo "Hello World" - name: Install Vim & Emacs become: yes apt: name={{ item }} state=present with_items: - vim - emacs # ݄݄牧emacs 蚎牐 - name: use vi-mode in readline become: yes lineinfile: dest=/etc/inputrc line="set editing-mode vi" -
Playbooks ฎՋ讕牫 • Ӟ犩 Playbook ݢ犥磪ग़㮆 Play ग़㮆 Tasks牐
• 種ֺአکԧ Play*1, Task*3 Module*3 (command, apt, lineinfile)牐 31 $ vim example.yml --- - name: This is a Super-basic playbook. hosts: all tasks: - name: Hello World command: echo "Hello World" - name: Install Vim & Emacs become: yes apt: name={{ item }} state=present with_items: - vim - emacs # ݄݄牧emacs 蚎牐 - name: use vi-mode in readline become: yes lineinfile: dest=/etc/inputrc line="set editing-mode vi" Play -
Playbooks ฎՋ讕牫 • Ӟ犩 Playbook ݢ犥磪ग़㮆 Play ग़㮆 Tasks牐
• 種ֺአکԧ Play*1, Task*3 Module*3 (command, apt, lineinfile)牐 32 $ vim example.yml --- - name: This is a Super-basic playbook. hosts: all tasks: - name: Hello World command: echo "Hello World" - name: Install Vim & Emacs become: yes apt: name={{ item }} state=present with_items: - vim - emacs # ݄݄牧emacs 蚎牐 - name: use vi-mode in readline become: yes lineinfile: dest=/etc/inputrc line="set editing-mode vi" Task 1 Task 2 Task 3 -
Playbooks ฎՋ讕牫 • Ӟ犩 Playbook ݢ犥磪ग़㮆 Play ग़㮆 Tasks牐
• 種ֺአکԧ Play*1, Task*3 Module*3 (command, apt, lineinfile)牐 33 $ vim example.yml --- - name: This is a Super-basic playbook. hosts: all tasks: - name: Hello World command: echo "Hello World" - name: Install Vim & Emacs become: yes apt: name={{ item }} state=present with_items: - vim - emacs # ݄݄牧emacs 蚎牐 - name: use vi-mode in readline become: yes lineinfile: dest=/etc/inputrc line="set editing-mode vi" Module -
Playbooks ฎՋ讕牫 • 䁆ᤈ playbook牐 34 $ ansible-playbook example.yml PLAY
[This is a Super-basic playbook.] ***************************************** TASK [setup] ******************************************************************* ok: [ansible-demo.local] TASK [Hello World] ************************************************************* changed: [ansible-demo.local] TASK [Install Vim & Emacs] ***************************************************** changed: [ansible-demo.local] => (item=[u'vim', u'emacs']) TASK [use vi-mode in readline] ************************************************* changed: [ansible-demo.local] PLAY RECAP ********************************************************************* ansible-demo.local : ok=4 changed=3 unreachable=0 failed=0 -
Playbooks ฎՋ讕牫 • 䁆ᤈ playbook牐 35 $ ansible-playbook example.yml PLAY
[This is a Super-basic playbook.] ***************************************** TASK [setup] ******************************************************************* ok: [ansible-demo.local] TASK [Hello World] ************************************************************* changed: [ansible-demo.local] TASK [Install Vim & Emacs] ***************************************************** changed: [ansible-demo.local] => (item=[u'vim', u'emacs']) TASK [use vi-mode in readline] ************************************************* changed: [ansible-demo.local] PLAY RECAP ********************************************************************* ansible-demo.local : ok=4 changed=3 unreachable=0 failed=0 Setup 者奾 (Recap) -
娞聜絑ह傶 Control Machine (Ubuntu 14.04) + Managed node*2 (Debian 8,
CentOS 6)牐 蝚螂 Ansible Jupyter 砺矒 Managed node 42 -
㷢ᘍ糽 • Ansible Docs - http://docs.ansible.com/ansible/intro_installation.html • ̽Ansible: Up and
Running̾- https://www.ansible.com/ansible-book • 犊獤楮ഩൎ Ansible ໐ஞ薪盢 (7:15) | Software Architecture School - http://goo.gl/nhykzE • Ansible 䋿䜗拻璤 - http://get.soft-arch.net/ansible/ • 襎脲ኴጱ褲琔物奲眲ᓕቘ | 敟誢礍䯤ɾᔵ承 - http://school.soft-arch.net/blog/90699/ metaphor-in-cm • 亮藳 Ansible by sakana / Max - https://goo.gl/e9RwhE • ̿Ansible ᛔ㵕玕奲眲ᓕቘ䋿䜗拻璤̀膏̿ᛔ౯䋿匍̀| 㲺Ռጱ執懿 - http://goo.gl/5gs1q9 • ̽匍դ IT ՈӞਧᥝᎣ螇ጱ Ansible ᛔ㵕玕奲眲ದૣ̾羬ڜᒍ - https://goo.gl/EOjs4I 44 Free -
瑽粙㬵რ • Blasts Off Space Rocket From Cosmodrom In The
Clouds, Polygonal Stock Illustration | dreamstime - http://goo.gl/6FAuiQ • 㾴疑瑿ቘ褾扮 - http://www.ngtaiwan.com • Using cloud-init and uvtool to initialize cloud instances | Rui - https://goo.gl/CbdvTH • Books icon (PSD) | GraphicsFuel - http://www.graphicsfuel.com/2012/07/books- icon-psd/ • Avatar, business, company, group, manager, people, users icon | Icon search engine - https://goo.gl/Hm6ScX • A Galaxy Just Appeared Out of Nowhere - http://chirpnews.com/2016/04/17/new- galaxy-appeared/ 45