四、Ansible-變數
一、Ansible變數命名規則
變數的名字有字母、下劃線和數字組成,必須以字母開頭
如下變數命名為正確:
good_a
ok_b
如下變數命名錯誤:
_aa
12bb
注意:保留的關鍵字不能作為變數名稱
二、變數型別
根據變數的作用範圍大概的將變數分為:
- 全域性變數
- 劇本變數
- 資產變數
但只有一個比較粗糙的劃分,不能囊括Ansible中的所有變數;
1. 全域性變數
全域性變數,使我們使用ansible或者使用ansible-playbook時,手動通過 -e 引數傳遞給Ansible的變數
通過ansible或ansible-playbook的help幫助,可以獲取具體的格式使用方法
[root@ansible-01 ~]# ansible -h | grep var path for many features including roles/ group_vars/ -e EXTRA_VARS, --extra-vars EXTRA_VARS set additional variables as key=value or YAML/JSON, if [root@ansible-01 ~]# ansible-playbook -h | grep var -e EXTRA_VARS, --extra-vars EXTRA_VARS set additional variables as key=value or YAML/JSON, if
練習
傳遞普通的key=value的形式
# [root@ansible-01 ~]# ansible all -i localhost, -m debug -a "msg='my key is {{ key }}'" -e "key=value"
傳遞一個YAML/JSON的形式(注意不管是YAML還是JSON,它們的最終格式一定是個字典)
==================JSOM格式================================= [root@ansible-01 ~]# cat a.json {"name":"mage","tpye":"school"} [root@ansible-01 ~]# ansible all -i localhost, -m debug -a "msg='name is {{ name }} type is {{ type }}'" -e @a.json [WARNING]: Found variable using reserved name: name localhost | SUCCESS => { "msg": "name is mage type is school" } =======================YAML格式============================ [root@ansible-01 ~]# cat a.yml --- name: mage type: study ... [root@ansible-01 ~]# ansible all -i localhost, -m debug -a "msg='name is {{ name }} type is {{ type }}'" -e @a.yml [WARNING]: Found variable using reserved name: name localhost | SUCCESS => { "msg": "name is mage type is study" }
2. 劇本變數
此種變數的playbook有關,定義在playbook中的,它的定義方式有多種;
通過PLAY屬性vars定義
---
- name: test play vars
hosts: all
vars:
user: xiaoge
home: /home/xiaoge
通過PLAY屬性vars_files定義
當通過vars屬性定義的變數很多時,這個play就會感覺特別臃腫,此時我們可以將變數單從play中抽離出來,形成單獨的YAML檔案。
---
- name: test play vars
hosts: all
vars_files:
- vars/users.yml
# cat vars/users.yml
---
user: xiaoge
home: /home/xiaoge
如何在PlayBook中使用這些變數
在PlayBook中使用變數時,使用 {{ 變數名 }} 來使用變數
---
- name: test my vars
hosts: all
vars:
user: xiaoge
home: /home/xiaoge
- tasks:
- name: create the user {{ user }}
user:
name: "{{ user }}"
home: "{{ home }}"
# 注意:這裡的雙引號不能去掉
3.資產變數
在之前的學習中學習了資產,資產共分為靜態資產和動態資產。
資產變數分為主機變數和組變數,分別針對資產中的單個主機和主機組
3.1 主機變數
以下資產中,定義了一個主機變數xiaoma,此變數只針對
[root@ansible-01 ~]# cat hostsandhostvars
[webservers]
172.18.0.3 user=xiaoma port=3309
172.18.0.4
驗證
# 獲取定義的變數
[root@ansible-01 ~]# ansible 172.18.0.3 -i hostsandhostvars -m debug -a "mag='{{user}} {{port}}'"
172.18.0.3 | SUCCESS => {
"user": "xiaoma"
}
# 未獲取到定義的變數值,因為xiaoma 這個變數針對172.18.0.4,主機無效
[root@ansible-01 ~]# ansible 172.18.0.4 -i hostsandhostvars -m debug -a "var=user"
172.18.0.4 | SUCCESS => {
"user": "VARIABLE IS NOT DEFINED!"
}
3.2 主機組變數
以下資產中,定義了一組變數home,此變數將針對webservers這個主機組中的所有伺服器有效
[root@ansible-01 ~]# cat hostsandhostvars
[webservers]
172.18.0.3 user=xiaoma
172.18.0.4
[webservers:vars]
home="/home/xiaoma"
驗證
# home是webservers的組變數,會針對這個組內的所有伺服器生效
[root@ansible-01 ~]# ansible webservers -i hostsandhostvars -m debug -a "var=home"
172.18.0.3 | SUCCESS => {
"home": "/home/xiaoma"
}
172.18.0.4 | SUCCESS => {
"home": "/home/xiaoma"
}
3.3 主機變數 VS 主機組變數
當主機變數和組變數在同一個資產發生重名的情況,會有什麼效果?
[root@ansible-01 ~]# cat hosts_v2
[webservers]
172.18.0.3 user=xiaoma
172.18.0.4
[webservers:vars]
user=tom
驗證
# 在資產中定義了主機變數和組變數 user,此時發現 172.18.0.3 這臺主機的主機變數 user 的優先順序更高
[root@ansible-01 ~]# ansible webservers -i hosts_v2 -m debug -a "var=user"
172.18.0.3 | SUCCESS => {
"user": "xiaoma"
}
172.18.0.4 | SUCCESS => {
"user": "tom"
}
3.4 變數的繼承
[root@ansible-01 ~]# cat hosts_v
hosts_v2 hosts_v3
[root@ansible-01 ~]# cat hosts_v3
[webservers]
172.18.0.3
[dbservers]
172.18.0.4
[allservers]
[allservers:children] # 固定寫法
dbservers
webservers
[allservers:vars] # 固定寫法
user=xiaoma
驗證
# 在資產繼承的同時,對應得變數也發生了繼承
[root@ansible-01 ~]# ansible allservers -i hosts_v3 -m debug -a "var=user"
172.18.0.4 | SUCCESS => {
"user": "xiaoma"
}
172.18.0.3 | SUCCESS => {
"user": "xiaoma"
}
[root@ansible-01 ~]# ansible dbservers -i hosts_v3 -m debug -a "var=user"
172.18.0.4 | SUCCESS => {
"user": "xiaoma"
}
[root@ansible-01 ~]# ansible webservers -i hosts_v3 -m debug -a "var=user"
172.18.0.3 | SUCCESS => {
"user": "xiaoma"
}
3.5 Inventory 內建變數的說明
內建變數幾乎都是以ansible_ 為字首。
ansible_ssh_host
將要連線的遠端主機名,與你想要設定的主機的別名不同的話,可通過此變數設定
ansible_ssh_port
ssh埠號,如果不是預設的埠號,通過此變數設定
ansible_ssh_user
預設的 ssh 使用者
ansible_ssh_pass
ssh 密碼(這種方式不安全,官方建議使用 --ask-pass 或 SSH 金鑰)
ansible_sudo_pass
sudo 密碼(這種方式不安全,官方建議使用 --ask-sudo-pass)
ansible_sudo_exe(new in version 1.8)
sudo(命令路徑,適用於1.8以上)
ansible_ssh_private_key_file
ssh 使用的私鑰檔案,適用於多個金鑰,而你不想使用 SSH 代理的情況
ansible_python_interpreter
目標主機的 python 路徑,適用於的情況:系統中的多個python,或者命令路徑不是"/usr/bin/python",比如 /usr/local/bin/python3
4. Facts 變數
Facts變數不包含在前文文中介紹的全域性變數、劇本變數、及資產變數之內。
Facts變數不需要我們人為去宣告變數名及賦值
它的宣告和賦值完全有Ansible中的setup模組幫我們完成
它收集了有關被管理伺服器的作業系統版本,伺服器IP地址、主機名、磁碟的使用情況、CPU個數、記憶體大小等等有關被管理伺服器的私有資訊
在每次playbook執行的時候都會發現playbook執行前都會有一個Gathering Facts的過程。這個過程就是收集被管理伺服器的Facts資訊過程。
4.1 手動收集Facts變數
[root@ansible-01 ~]# ansible all -i localhost, -c local -m setup
localhost | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.10.150"
],
"ansible_all_ipv6_addresses": [
"fe80::d74b:84a8:cb65:7dc5"
],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "07/29/2019",
"ansible_bios_version": "6.00",
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-1160.el7.x86_64",
"LANG": "en_US.UTF-8",
"crashkernel": "auto",
"quiet": true,
"rd.lvm.lv": "centos/swap",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/centos-root"
},
........
4.2 手動收集Facts變數
通過剛才的手動收集Facts,我們發現facts資訊量太大,需要針對性的過濾一下。
可以通過使用Facts模組中的filter引數過濾我們想要的資訊。
- 僅獲取伺服器的記憶體情況資訊
[root@ansible-01 ~]# ansible all -i localhost, -m setup -a "filter=*memory*" -c local
localhost | SUCCESS => {
"ansible_facts": {
"ansible_memory_mb": {
"nocache": {
"free": 668,
"used": 304
},
"real": {
"free": 560,
"total": 972,
"used": 412
},
"swap": {
"cached": 0,
"free": 2047,
"total": 2047,
"used": 0
}
},
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
- 獲取伺服器磁碟掛載情況
# -c local 意思是不走ssh協議
[root@ansible-01 ~]# ansible all -i localhost, -m setup -a "filter=*mount*" -c local
localhost | SUCCESS => {
"ansible_facts": {
"ansible_mounts": [
{
"block_available": 224503,
"block_size": 4096,
"block_total": 259584,
"block_used": 35081,
"device": "/dev/sda1",
"fstype": "xfs",
"inode_available": 523962,
"inode_total": 524288,
"inode_used": 326,
"mount": "/boot",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 919564288,
"size_total": 1063256064,
"uuid": "411914c3-fc4e-4eec-9daf-f2f34e5d0f66"
},
{
"block_available": 12694160,
"block_size": 4096,
"block_total": 13100800,
"block_used": 406640,
"device": "/dev/mapper/centos-root",
"fstype": "xfs",
"inode_available": 26172537,
"inode_total": 26214400,
"inode_used": 41863,
"mount": "/",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 51995279360,
"size_total": 53660876800,
"uuid": "4bba3d5a-fefd-4b9a-99a0-688ba3351d4a"
},
{
"block_available": 38506054,
"block_size": 4096,
"block_total": 38514305,
"block_used": 8251,
"device": "/dev/mapper/centos-home",
"fstype": "xfs",
"inode_available": 77066233,
"inode_total": 77066240,
"inode_used": 7,
"mount": "/home",
"options": "rw,seclabel,relatime,attr2,inode64,noquota",
"size_available": 157720797184,
"size_total": 157754593280,
"uuid": "d1fc9088-3a42-4b19-8ee7-9cf17c420a1d"
}
],
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}