不多有人注意到ansible 變量類型的問題,最近工做的時候偶然遇到when條件判斷不生效的問題,才注意到了變量數據類型的問題。
查閱了好多資料後才知道,原來ansible定義變量的時候,還像python同樣有動態數據類型的概念。html
age1是一個int類型的變量,例如:age1: 21
age2是一個string類型的變量,例如:age2: '21'
married和married2是一個布爾類型的變量,例如:married: True 或 married2: true
married3是一個string類型的變量,例如:married3: 'true'python
age1|string 能夠變int轉換爲string,而後進行比較運算
age2|int 能夠把string轉爲爲int,而後進行比較運算
married|string 能夠把布爾類型轉換爲string,而後進行比較運算。
married2|string 能夠把布爾類型轉換爲string,而後進行比較運算。ide
--- - 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' 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'
ansible-playbook test-var.yml
ui
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
參考官方文檔:https://docs.ansible.com/ansi...debug