4 playlook-Jinja2 filter

jinja2模板,不多解释,请看例子

[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替换

运行结果如下


[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   

posted @ 2018-07-23 11:30  温柔易淡  阅读(203)  评论(0编辑  收藏  举报