4 playlook-Jinja2 filter

jinja2模板,很少解釋,請看例子shell

[root@LeoDevops jinjatwo]# cat  j1.yaml 
- hosts: u12
  gather_facts: False
  vars:
    list: [1,2,3,4,5]
    one:  "1"
    str:  "string"
    ansible: "heh"
  tasks:
    - name: run commands
      shell: df -h
      register: info
    - name: debug print filter
      debug: msg="{{ info.stdout | pprint }}"
    - name: debug conditionals filter
      debug: msg="the run commands status is changed"
      when: info|changed
    - name: debug int capitalize filter
      debug: msg="the int value {{ one|int }} the lower value is {{ str|capitalize }}"
    - name: debug default filter
      debug: msg="the variable value is {{ ansible | default('ansible is not define')  }} "
    - name: debug list max and min filter
      debug: msg="the list max value is {{ list|max }},the list min value is {{ list|min }}"
    - name: debug random filter
      debug: msg="the list random value is {{ list|random  }} and generate a random value is {{ 1000| random(1,10)  }}"
    - name: debug join filter
      debug: msg="the join filter value is {{ list|join('+')  }}"
    - name: debug replace and regex_replace filter
      debug: msg="the replace value is {{ str| replace('t','T') }} , the regex_replace_value is {{ str|regex_replace('.*tr(.*)$','\\1') }} "
  1. info.stdout 使用pprint進行格式化
  2. info的執行狀態使用changed filter 進行判斷
  3. one的值進行int轉變,而後對str的值進行capitalize格式化
  4. 對ansible變量進行判斷,若是該變量定義了就引用它的值,若是沒有定義就使用default內值
  5. 對list內取最大值和最小值
  6. 對list內的值使用random filter隨機挑選一個,而後隨機生成1000之內的數字,step是10
  7. 對list內的值使用join filter連接在一塊兒。
  8. 第八個是對str的值使用replace與regex_replace替換

運行結果以下api

[root@LeoDevops jinjatwo]# ansible-playbook j1.yaml 

PLAY [u12] ************************************************************************************************************************************************************************************

TASK [run commands] ***************************************************************************************************************************************************************************
changed: [192.168.93.137]

TASK [debug print filter] *********************************************************************************************************************************************************************
ok: [192.168.93.137] => {
    "msg": "u'Filesystem      Size  Used Avail Use% Mounted on\\n/dev/sda1        19G  3.0G   16G  17% /\\nudev            367M  4.0K  367M   1% /dev\\ntmpfs           150M  296K  150M   1% /run\\nnone            5.0M     0  5.0M   0% /run/lock\\nnone            374M     0  374M   0% /run/shm'"
}

TASK [debug conditionals filter] **************************************************************************************************************************************************************
ok: [192.168.93.137] => {
    "msg": "the run commands status is changed"
}

TASK [debug int capitalize filter] ************************************************************************************************************************************************************
ok: [192.168.93.137] => {
    "msg": "the int value 1 the lower value is String"
}

TASK [debug default filter] *******************************************************************************************************************************************************************
ok: [192.168.93.137] => {
    "msg": "the variable value is heh "
}

TASK [debug list max and min filter] **********************************************************************************************************************************************************
ok: [192.168.93.137] => {
    "msg": "the list max value is 5,the list min value is 1"
}

TASK [debug random filter] ********************************************************************************************************************************************************************
ok: [192.168.93.137] => {
    "msg": "the list random value is 1 and generate a random value is 241"
}

TASK [debug join filter] **********************************************************************************************************************************************************************
ok: [192.168.93.137] => {
    "msg": "the join filter value is 1+2+3+4+5"
}

TASK [debug replace and regex_replace filter] *************************************************************************************************************************************************
ok: [192.168.93.137] => {
    "msg": "the replace value is sTring , the regex_replace_value is ing "
}

PLAY RECAP ************************************************************************************************************************************************************************************
192.168.93.137             : ok=9    changed=1    unreachable=0    failed=0
相關文章
相關標籤/搜索