ansible 變數及帶值:
寫過程式的人都知道:剛開始都是先從了解變數及帶值開始因為要部署您不可能一台台去設定,所以變數及帶值是必需要了解
ansible 變數及帶值其實很簡單如下說明:
## 加變數 -> vars: "變數名"
## 放在上方為全域變數
## 取值 -> "{{ 變數名 }}"
- hosts: web5
name: play-test
vars:
hello: "hello from vars"
- name: debug test
debug:
msg: "{{hello}}"
verbosity: 0
跑跑看: 出現下方 "msg": "hello from vars"
joulong@joulong1:~/playbook$ ansible-playbook playbook3.yml -i inventory.ini
PLAY [play-test] *********************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [joulong2]
ok: [joulong3]
TASK [debug test] ********************************************************************************************************************************************************************************************
ok: [joulong2] => {
"msg": "hello from vars"
}
ok: [joulong3] => {
"msg": "hello from vars"
}
PLAY RECAP ***************************************************************************************************************************************************************************************************
joulong2 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
joulong3 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
ansible 檔案方式變數及帶值其實很簡單如下說明:
如 python import 方式相同,以下說明:
以下是新增 vars目錄及demo.yml檔案 vars\demo.yml
hello: "hello from demo.yml"
以下是 ansible playbook3.yml
- hosts: web5
name: play-test
#vars:
# hello: "hello from vars"
vars_files:
- "vars/demo.yml"
-
- name: debug test
debug:
msg: "{{hello}}"
verbosity: 0
執行以指令看看(用檔案方式更靈活了):
joulong@joulong1:~/playbook$ ansible-playbook playbook3.yml -i inventory.ini
PLAY [play-test] *********************************************************************************************************************************************************************************************
TASK [Gathering Facts] ***************************************************************************************************************************************************************************************
ok: [joulong2]
ok: [joulong3]
TASK [debug test] ********************************************************************************************************************************************************************************************
ok: [joulong2] => {
"msg": "hello from demo.yml"
}
ok: [joulong3] => {
"msg": "hello from demo.yml"
}
PLAY RECAP ***************************************************************************************************************************************************************************************************
joulong2 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
joulong3 : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0