Ansible 使用 Template 系統(tǒng)

2020-09-04 10:43 更新

template module 是凍仁常用的檔案模組 (Files Modules) 之一,先前在「Ansible 常用的 Ansible Module」一章時(shí),也曾提到可以用它和變數(shù) (Variables) 來操作檔案。


automate_with_ansible_practice-19.jpg

圖片來源:http://www.freeiconspng.com/free-images/txt-file-icon-1203


我們只需事先定義變數(shù)和模板 (Templates),即可用它動(dòng)態(tài)產(chǎn)生遠(yuǎn)端的 Shell Scripts、設(shè)定檔 (Configure) 等。換句話說,我們可以用一份 template 來產(chǎn)生開發(fā) (Development)、測(cè)試 (Test) 和正式環(huán)境 (Production) 等不同的環(huán)境設(shè)定。

舉例說明

說了那么多,還是讓凍仁舉個(gè)小例子說明吧!

  1. 建立 template 檔案。
    $ vi hello_world.txt.j2 Hello "{{ dynamic_word }}" ↑ ↑ ↑ 
  •  由于 Ansible 是借由 Jinja2 來寫作 template 系統(tǒng),所以請(qǐng)使用 *.j2  的副檔名。
  • 上面的"{{ dynamic_word }}"代表我們?cè)诖?template 里使用了名為 dynamic_word 的變數(shù)。

2.建立 playbook,并加入變數(shù)。 

$ vi template_demo.yml 
--- 
- name: Play the template module 
    hosts: localhost 
    vars: 
        dynamic_word: "World"

 tasks: 
    - name: 
      generation the hello_world.txt file
        template: src: hello_world.txt.j2 
        dest: /tmp/hello_world.txt 

    - name: show file context 
        command: cat /tmp/hello_world.txt
         register: result 

- name: print stdout 
    debug:
         msg: "" 

# vim:ft=ansible :
  • 在第 6 行,我們幫 dynamic_word 變數(shù)設(shè)了一個(gè)預(yù)設(shè)值 World。
  • 在第 9 行的第 1 個(gè) task 里,我們使用了 template module,并指定了檔案的來源 (src) 和目的地 (dest)。
  • 之后的 2 個(gè) tasks 則是把 template module 產(chǎn)生出來的檔案給印出來

3.執(zhí)行 playbook。
                  2016-12-14-ansible-template1.gif

  • 直接執(zhí)行 playbook。 
    $ ansible-playbook template_demo.yml

    • 通過 -e 參數(shù)將 dynamic_word 覆寫成 ansible。
      $ ansible-playbook template_demo.yml -e "dynamic_word=ansible"
    • 通過 -e 參數(shù)將 dynamic_word 覆寫成 Day14。
      $ ansible-playbook template_demo.yml -e "dynamic_word=Day14"

    怎么讓 Playbooks 切換不同的環(huán)境?

    1. 在 Playbooks 里除了用 vars 來宣告變數(shù)以外,還可以用 vars_files 來 include 其它的變數(shù)檔案。
      $ vi template_demo2.yml 
      --- 
      - name: Play the template module 
          hosts: localhost 
          vars: 
              env: "development"
      
       vars_files: 
          - vars/.yml 
      
      tasks: 
          - name: generation the hello_world.txt file
               template:
                    src: hello_world.txt.j2 
                    dest: /tmp/hello_world.txt 
      
          - name: show file context
               command: cat /tmp/hello_world.txt 
              register: result 
      
          - name: print stdout 
              debug: 
                  msg: "" 
      
      # vim:ft=ansible :
    2. 建立 vars/development.ymlvars/test.yml 和 vars/production.yml 檔案,接下來將依不同的環(huán)境 include 不同的變數(shù)檔案 (vars files),這樣就可以用同一份 playbook 切換環(huán)境了!


    • Development

    $ vi vars/development.yml 
    dynamic_word: "development"  

    • Test

    $ vi vars/test.yml 
    dynamic_word: "test"

    •  Production

    $ vi vars/production.yml 
    dynamic_word: "production"
    
     

       3.執(zhí)行playbook,并通過 -e 切換各個(gè)環(huán)境。

                          

    2016-12-14-ansible-template2.gif


    后話

    template 系統(tǒng)是實(shí)務(wù)上很常見的手法之一,藉由它我們可以很輕鬆的讓開發(fā)、測(cè)試和正式環(huán)境無縫接軌。

    現(xiàn)在是不是有寫 code 管機(jī)器的感覺了呢?(笑)

    相關(guān)連結(jié)


    以上內(nèi)容是否對(duì)您有幫助:
    在線筆記
    App下載
    App下載

    掃描二維碼

    下載編程獅App

    公眾號(hào)
    微信公眾號(hào)

    編程獅公眾號(hào)