ansible変数タイプ
5931 ワード
背景
ansible変数タイプの問題に気づく人は少なく、最近仕事中にwhen条件判断が発効しない問題に偶然遭遇し、変数データタイプの問題に気づいた.多くの資料を調べてみると、ansibleが変数を定義するとき、pythonのように動的なデータ型の概念があることがわかりました.
直接の例:
変数の定義
age 1はintタイプの変数です.たとえば、age 1:21 age 2はstringタイプの変数です.たとえば、age 2:'21'marriedとmarried 2はブールタイプの変数です.たとえば、married:Trueまたはmarried 2:truemarried 3はstringタイプの変数です.たとえば、married 3:'true't's
変数タイプ変換
age 1|stringはintをstringに変換し、比較演算age 2|intはstringをintに変換し、比較演算married|stringはブールタイプをstringに変換して比較演算を行うことができる.married 2|stringは、ブールタイプをstringに変換し、比較演算を行うことができます.
この例を実行
公式文書参照:https://docs.ansible.com/ansi...
ansible変数タイプの問題に気づく人は少なく、最近仕事中にwhen条件判断が発効しない問題に偶然遭遇し、変数データタイプの問題に気づいた.多くの資料を調べてみると、ansibleが変数を定義するとき、pythonのように動的なデータ型の概念があることがわかりました.
直接の例:
変数の定義
age 1はintタイプの変数です.たとえば、age 1:21 age 2はstringタイプの変数です.たとえば、age 2:'21'marriedとmarried 2はブールタイプの変数です.たとえば、married:Trueまたはmarried 2:truemarried 3はstringタイプの変数です.たとえば、married 3:'true't's
変数タイプ変換
age 1|stringはintをstringに変換し、比較演算age 2|intはstringをintに変換し、比較演算married|stringはブールタイプをstringに変換して比較演算を行うことができる.married 2|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'
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
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...