HackMD
  • Prime
    Prime  Full-text search on all paid plans
    Search anywhere and reach everything in a Workspace with Prime plan.
    Got it
      • Create new note
      • Create a note from template
    • Prime  Full-text search on all paid plans
      Prime  Full-text search on all paid plans
      Search anywhere and reach everything in a Workspace with Prime plan.
      Got it
      • Sharing Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • More (Comment, Invitee)
      • Publishing
        Everyone on the web can find and read all notes of this public team.
        After the note is published, everyone on the web can find and read this note.
        See all published notes on profile page.
      • Commenting Enable
        Disabled Forbidden Owners Signed-in users Everyone
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Invitee
      • No invitee
      • Options
      • Versions and GitHub Sync
      • Transfer ownership
      • Delete this note
      • Template
      • Save as template
      • Insert from template
      • Export
      • Dropbox
      • Google Drive
      • Gist
      • Import
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
      • Download
      • Markdown
      • HTML
      • Raw HTML
    Menu Sharing Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Versions and GitHub Sync Transfer ownership Delete this note
    Export
    Dropbox Google Drive Gist
    Import
    Dropbox Google Drive Gist Clipboard
    Download
    Markdown HTML Raw HTML
    Back
    Sharing
    Sharing Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    Note Permission
    Read
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    Write
    Only me
    • Only me
    • Signed-in users
    • Everyone
    Only me Signed-in users Everyone
    More (Comment, Invitee)
    Publishing
    Everyone on the web can find and read all notes of this public team.
    After the note is published, everyone on the web can find and read this note.
    See all published notes on profile page.
    More (Comment, Invitee)
    Commenting Enable
    Disabled Forbidden Owners Signed-in users Everyone
    Permission
    Owners
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Invitee
    No invitee
       owned this note    owned this note      
    Published Linked with GitHub
    Like BookmarkBookmarked
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    --- tags: ansible --- # Ansible 介紹 ![](https://i.imgur.com/vYkwoNj.png) ## 什麼是 **Ansible** #### &emsp; **Ansible** 是以 Python 程式語言所開發的自動化`組態管理工具`,目標是實現基礎建設即程式碼(Infrastructure as Code)的目標,協助開發者快速部署出一致的工作環境。此外, Ansible 可以用於部署應用程式以及幫助開發者導入持續整合的作業流程。 ##### 摘至 [Red Hat 併購 DevOps 新秀 Ansible | iThome](https://https://www.ithome.com.tw/news/99354) 一文。![](https://i.imgur.com/x7e56Y8.jpg) #### 我認為 Ansbile 是什麼 ? 1. 它是一個很方便,且快速的自動化部屬工具。 2. 不用在一台一台的幫host或 vm 安裝少的工具。 3. 只需要透過 ssh 和 python 就可以操作。 4. 業界最常見且使用較平凡的工具。 #### 優點: ● 以YAML的格式編寫,語法清楚容易上手。 ● 只需要透過 ssh 與遠端機器做溝通。 ● 不需要中間代理伺服器。 ● Client 端不需要額外安裝任何軟體。 #### 優點: ● 執行時需要等待一段時間。 ● 必須事先知道 「被控制IP」。 *** ## Ansible 是怎麼運行 ? #### &emsp; 在 Ansible 的世界裡,我們會透過 inventory 檔案來定義有哪些 Managed node (被控端),並藉由 SSH 和 Python 進行溝通。 ![](https://i.imgur.com/aKJyMMH.png) <!-- Control Machine 指的是我們主要會在上面操作 Ansible 的機器,凍仁喜歡用主控端來形容它。它可以是我們平時用的電腦、手機 1 或機房裡的某一台機器,也可以把它想成是一般 Lab 練習裡的 Workstation。 Managed node 則是被 Ansible 操縱的機器,凍仁喜歡用被控端來形容它。在很多的 Lab 練習裡會用 Server 來稱呼它。 --> #### 安裝 ansible ```bash= yum install -y epel-release.noarch yum install -y ansible ``` #### 準備 VM ```shell= vim /vmdsik/shell/create_vm_setting.sh #!/bin/bash vmnames="ans1 ans2 ans3 ans4" ...... virsh define ${xmldir}/${vm}.xml done sh /vmdsik/shell/create_vm_setting.sh virsh start ans? ``` <!-- ```shell= #!/bin/bash vmnames="ans1 ans2 ans3 ans4" [ -f '/vmdisk/hosts' ] && rm -f /vmdisk/hosts for vmname in ${vmnames} do if virsh list --name --state-running |grep ${vmname} &> /dev/unll ;then # 檢查vm是否運行 mac=`virsh dumpxml ${vmname} |grep 'mac address' |sed -e "s/.*mac address='\(.*\)'.*/\1/"` ip=`arp -n |grep "${mac}" |awk '{print $1}'` echo "${vmname} ansible_host=${ip} ansible_port=22" >> /vmdisk/hosts fi done ``` --> #### 設定 [inventory file](https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html) <!-- 我們可以先來看一下 ansible.cfg 設定檔 --> ```bash= cp /etc/ansible/hosts /etc/ansible/hosts.def vim /etc/ansible/hosts # ansible_host: 遠端 ssh 主機位置 # ansible_port: 遠端 ssh port # ansible_user: 遠端 ssh 使用者 # ansible_ssh_private_key_file: 本機 ssh 私鑰檔路徑 # ansible_ssh_pass: 遠端 ssh 密碼 (建建議改私鑰) localhost ansible_host=127.0.0.1 ansible_port=22 [vm] ans1 ansible_host=192.168.19.1 ansible_port=22 ans2 ansible_host=192.168.19.2 ansible_port=22 ans3 ansible_host=192.168.19.3 ansible_port=22 ans4 ansible_host=192.168.19.4 ansible_port=22 ``` ###### 測試 ```bash= ansible all -m ping ``` *** ## Ansible 怎麼 「 玩 」 #### 我們可以用 Ad-Hoc command 和 Playbook 兩種方式來操作 Ansible ![](https://i.imgur.com/UmBGX64.png) #### &emsp; 前者是透過一次次簡短的指令來操作,而後者則是把要執行的任務先編寫好,之後再一次執行。兩者的關係就好像 Linux Shell 裡打指令執行和寫成 Shell Script 再執行。 ## Playbook 是什麼 ? #### Playbook 就字面上的意思為「劇本」。我們可以透過事先寫好的劇本 (Playbook) 來讓各個 Managed Node 進行指定的動作 (Plays) 和任務 (Tasks)。 #### 簡單的說就是,Playbooks 就是 Ansible 的腳本(script)。 ![](https://i.imgur.com/ba8KjdU.jpg) <br/> ## [**YAML**](https://zh.wikipedia.org/wiki/YAML) 是什麼 ? #### 為什麼用 yaml語法 來編些 ? 因為它像 xml 和 json 一樣,但是又比這些語法要來的讓人容易閱讀。 #### **yaml** 的語法結構非常簡單清楚,因為它本身的結構主要依賴階層及縮排,就像 python 。 #### &emsp;&emsp; **plabooks yaml** 的基礎規則: #### &emsp;&emsp;&emsp; 1. 大小寫區分。 <br/> &emsp;&emsp;&emsp; 2. 已縮排表示階層關係。 <br/> &emsp;&emsp;&emsp; 3. 縮排不可以使用 [tab] 鍵縮排,只允需空白鍵。 <br/> &emsp;&emsp;&emsp; 4. 以 --- 為開頭 (三個 - ) ![](https://i.imgur.com/nqyFJ7j.gif) ```yaml= vim /vmdisk/playbook/cluster.yml --- - name: Ansible cluster hosts: vm tasks: - name: Use yum module yum: name: "*" state: latest - yum: name: "{{ packages }}" state: installed vars: packages: - net-tools - bash-completion - vim-enhanced - setroubleshoot - policycoreutils-python-2.5-29.el7.x86_64 ``` ## **參考資料** #### 現代 IT 人一定要知道的 Ansible 自動化組態技巧 <br/> https://chusiang.gitbooks.io/automate-with-ansible/content/02.what-is-the-ansible.html #### 七分鐘掌握 Ansible 核心觀念 <br/> https://school.soft-arch.net/courses/ansible/lectures/655359 #### Docker:使用Shell或Ansible <br/> https://medium.com/@Joachim8675309/docker-using-shell-or-ansible-7cdceb646d3 #### 利用Ansible部署运行Apache(http)的Docker容器 <br/> https://www.linuxidc.com/Linux/2017-11/148296.htm *** ##### Variables <br/> https://chusiang.github.io/ansible-docs-translate/playbooks_variables.html ##### Inventory <br/> https://docs.ansible.com/ansible/latest/user_guide/intro_inventory.html ##### Playbooks <br/> https://docs.ansible.com/ansible/latest/user_guide/playbooks.html ##### Ansible的配置檔案 <br/> https://chusiang.github.io/ansible-docs-translate/intro_configuration.html ##### Ansible 基本操作 <br/> https://hackmd.io/udcE6KeDRM-6DfzVDu6i3Q?view

    Import from clipboard

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lost their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template is not available.


    Upgrade

    All
    • All
    • Team
    No template found.

    Create custom template


    Upgrade

    Delete template

    Do you really want to delete this template?

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Tutorials

    Book Mode Tutorial

    Slide Mode Tutorial

    YAML Metadata

    Contacts

    Facebook

    Twitter

    Feedback

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions

    Versions and GitHub Sync

    Sign in to link this note to GitHub Learn more
    This note is not linked with GitHub Learn more
     
    Add badge Pull Push GitHub Link Settings
    Upgrade now

    Version named by    

    More Less
    • Edit
    • Delete

    Note content is identical to the latest version.
    Compare with
      Choose a version
      No search result
      Version not found

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub

        Please sign in to GitHub and install the HackMD app on your GitHub repo. Learn more

         Sign in to GitHub

        HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Available push count

        Upgrade

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Upgrade

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully