ansible變量類型

0.背景

很少有人注意到ansible 變量類型的問題,最近工作的時候偶然遇到when條件判斷不生效的問題,才注意到了變量數據類型的問題。
查閱了好多資料後才知道,原來ansible定義變量的時候,還像python一樣有動態數據類型的概念。

1 變量定義

變量可以在play中使用vars關鍵字定義,例如:

---
- hosts: "{{ hosts_group }}"
  remote_user: root
  vars:
    name1: robin
    hosts_group: "localhost"
    date1: 2020-02-02
    age1: 21

變量可以在group_vars/all中定義,例如:

name1: robin
age1: 21

變量可以在ansible腳本中,使用register定義,例如:

- name: register a var name1
  shell: "echo robin"
  regitster: name1

使用register定義的變量,其實是將運行結果作爲一個字典賦值給這個變量。

變量可以在ansible腳本中,使用set_fact定義,例如:

 - set_fact:
      env_name: 'china'
   when: ex_env_name=='zhonguo'

使用set_fact定義變量,類似普通編程語言定義變量的方式,例如:env_name=‘china’

2 變量引用

可以使用雙引號加花括號引用變量,例如:

- name: show the var name1
  debug:
     msg: "{{ name1 }}"

這裏可以使用python的字符串處理函數

3 變量類型

age1是一個int類型的變量,例如:age1: 21
age2是一個string類型的變量,例如:age2: ‘21’
married和married2是一個布爾類型的變量,例如:married: True 或 married2: true
married3是一個string類型的變量,例如:married3: ‘true’
ages 是一個列表類型的變量,例如:ages=[10,20,30]

4 變量類型轉換

age1|string 可以變int轉換爲string,然後進行比較運算
age2|int 可以把string轉爲爲int,然後進行比較運算
married|string 可以把布爾類型轉換爲string,然後進行比較運算。
married2|string 可以把布爾類型轉換爲string,然後進行比較運算。

---

- hosts: "{{ hosts_group }}"
  remote_user: root
  vars:
    name1: robin
    hosts_group: "localhost"
    date1: 2020-02-02
    age1: 21
    age2: '21'
    married: True
    married2: true
    married3: 'true'
    ages=[10,20,30]


  tasks:
    - name: def age1 as int
      debug:
        msg: "age1 is {{ age1 }} as int"
      when: age1 == 21

    - name: def age2 as string
      debug:
        msg: "age2 is {{ age2 }} as string"
      when: age2 == '21'

    - name: def age1 as int to string
      debug:
        msg: "age1 is {{ age1 }}"
      when: age1|string == '21'

    - name: def age2 as string to int
      debug:
        msg: "age2 is {{ age2 }}"
      when: age2|int == 21 and age2|int >= 18

    - name: change to string and compare age1 and age2
      debug:
        msg: "{{ age1}} {{ age2 }}"
      when: age1|string + age2|string == '2121'

    - name: change to int and compare age1 and age2
      debug:
        msg: "{{ age1}} {{ age2 }}"
      when: age1|int - age2|int == 0

    - name: show the married or not
      debug:
        msg: "married is {{ married }}"
      when: married|string == 'True'

    - name: show the married2 or not
      debug:
        msg: "married2 is {{ married2 }}"
      when: married2|string == 'True'

    - name: show the married3 or not
      debug:
        msg: "married3 is {{ married3 }}"
      when: married3|string == 'true'

    - name: def age1 as list
      debug:
        msg: 'age1 is {{ item }} as int'
      with_items: "{{ ages }}"

4.1 運行該示例

ansible-playbook test-var.yml

PLAY [localhost] ****************************************************************************************************************************************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************************************************************************************************************************************
ok: [localhost]

TASK [def age1 as int] **********************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "age1 is 21 as int"
}

TASK [def age2 as string] *******************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "age2 is 21 as string"
}

TASK [def age1 as int to string] ************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "age1 is 21"
}

TASK [def age2 as string to int] ************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "age2 is 21"
}

TASK [change to string and compare age1 and age2] *******************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "21 21"
}

TASK [change to int and compare age1 and age2] **********************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "21 21"
}

TASK [show the married or not] **************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "married is True"
}

TASK [show the married2 or not] *************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "married2 is True"
}

TASK [show the married3 or not] *************************************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "married3 is true"
}


PLAY RECAP **********************************************************************************************************************************************************************************************************************************
localhost                  : ok=7    changed=0    unreachable=0    failed=0

TASK [def age1 as int] ***********************************************************************************************************************************************************************
ok: [localhost] => (item=10) => {
    "msg": "age1 is 10 as int"
}
ok: [localhost] => (item=20) => {
    "msg": "age1 is 20 as int"
}
ok: [localhost] => (item=30) => {
    "msg": "age1 is 30 as int"
}

參考官方文檔:https://docs.ansible.com/ansible/latest/user_guide/playbooks_variables.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章