Upgrade to PRO for Only $50/Year—Limited-Time Offer! 🔥

20190720_Better Practice_ Day 2 Operaition with...

20190720_Better Practice_ Day 2 Operaition with Ansible

#ansible

Phil Huang

July 20, 2019
Tweet

More Decks by Phil Huang

Other Decks in Technology

Transcript

  1. Best Better Practice: Day 2 Operation with Ansible Phil Huang

    黃秉鈞 <[email protected]> SDN x Cloud Native Meetup #18, Taiwan, July. 20, 2019
  2. # whois Phil Huang 黃秉鈞 • 社群斜槓 青年 青壯年 ◦

    SDNDS-TW ◦ Cloud Native Taiwan User Group (CNTUG) • 任職於 Red Hat 擔任 Solution Architect ◦ Ansible IT Automation ◦ OpenShift Container Platform ◦ Software-Defined Networking (SDN) ◦ Network Function Virtualization (NFV) • Blog ◦ https://blog.pichuang.com.tw Ref: https://www.linkedin.com/in/phil-huang-09b09895/
  3. A: 堡壘機 (Bastion Host / Jump Server ) • 單一管理入口

    • 權限控管窗口 • 雜 Code 放置所 Ref: 認識 Bastion Host 部署管理機 - Yi Yang
  4. 關於 SSH 連線 - ProxyCommand Ref: 認識 Bastion Host 部署管理機

    - Yi Yang Host 10.10.10.* ProxyCommand ssh -W %h:%p bastion.pichuang.local IdentityFile ~/.ssh/private_key.pem Host bastion.pichuang.local Hostname bastion.pichuang.local User pichuang IdentityFile ~/.ssh/private_key.pem StrictHostKeyChecking no
  5. 到底 ansible.cfg 放在哪裡好? A. ANSIBLE_CONFIG (environment variable if set) B.

    ansible.cfg (in the current directory) C. ~/.ansible.cfg (in the home directory) D. /etc/ansible/ansible.cfg Ref: https://docs.ansible.com/ansible/latest/reference_appendices/config.html#the-configuration-file 個人慣例
  6. 是否該維護自己的 ansible.cfg? Ref: https://gist.github.com/pichuang/7b1b826595aada062f37eee5ab9852a2#file-ansible-cfg-L13 • 就像對待 dotfiles (.bashrc / .vimrc

    / … ) • 常駐外掛: SSH 加速設定 • 開了不會後悔的參數 • callback_whitelist = profile_tasks • stdout_callback = debug
  7. Recap: 三個指令學一下 • 檢查環境 ◦ ansible -i hosts all -m

    ping • 同時下指令 (回想起用 tmux synchronize-panes 的畫面嗎?) ◦ ansible -i hosts all -m command -a “ls” • Copy 檔案 (回想起用 scp + for 迴圈的日子了嗎?) ◦ ansible -i hosts all -m copy -a “src=/ooxx dest=/xxoo”
  8. Ansible Command v.s. Shell v.s. Raw • Linux ◦ command

    > shell > raw • Windows ◦ win_command > win_shell > raw • Network ◦ *os_command > raw 三者參考選擇: • 避免環境變數注入問題,寫 Playbook 時,優選 command • shell 類同於一般打指令 ◦ ansible -m shell -a “xxx | ooo >> $HOME” • raw 在受控機器環境嚴苛狀況下再考慮用 (例如 router)
  9. 啥時該寫 Ansible Playbook? • 短時間內需要反覆下同樣的指令 • 每日例行公事 • Debugging •

    分享經驗給其他人 ◦ ansible-galaxy (o) ◦ git (o) ◦ svn (o) ◦ zip (x) Check List
  10. Q: 如何使用 ansible-playbook 指令? A. 直接下指令: ansible-playbook -i hosts ...

    B. shell script + ansible-playbook C. makefile + ansible-playbook 個人慣例
  11. A: ansible-playbook 與 {Shell Script, Makefile} 是好友 $ cat /usr/bin/upgrade.sh

    #!/bin/bash ansible-playbook -i hosts /do/not/copy/the/script/upgrade.yml $ chmod +x /usr/bin/upgrade.sh $ upgrade.sh
  12. IaC 基礎建設即代碼: Git + Ansible 上班撿到槍組合 Ref: https://about.gitlab.com/community/heroes • 版本控制

    >> 多人共用 • 地端最多人用 GitLab • 開發流程選擇 ◦ 小型副本: GitHub Flow ◦ 大型副本: GitLab Flow