一、变量规则
1、变量的名称由字母、下划线和数字组成,但是只能以字母开头
2、变量不能是关键字
二、变量类型
- 全局变量
- 剧本变量
- 资产变量
全局变量
1、传递key=value的形式
root@zsh-virtual-machine:~# ansible all -i localhost, -m debug -a "msg='my name is {{ name }}'" -e "name=alonzo"
[WARNING]: Found variable using reserved name: name
localhost | SUCCESS => {
"msg": "my name is alonzo"
}
2、使用yaml或者json的形式(都必须是字典格式)
#json形式
root@zsh-virtual-machine:~# cat test.json
{"name":"alonzo","age":"18"}
root@zsh-virtual-machine:~# ansible all -i localhost, -m debug -a "msg='my name is {{ name }}, age is {{ age }}'" -e "@./test.json"
[WARNING]: Found variable using reserved name: name
localhost | SUCCESS => {
"msg": "my name is alonzo, age is 18"
}
#yaml形式
root@zsh-virtual-machine:~# cat test.yaml
---
name: alonzo
age: 18
root@zsh-virtual-machine:~# ansible all -i localhost, -m debug -a "msg='my name is {{ name }}, age is {{ age }}'" -e "@./test.yaml"
[WARNING]: Found variable using reserved name: name
localhost | SUCCESS => {
"msg": "my name is alonzo, age is 18"
}
剧本变量
此变量和PlayBook有关,定义在playbook中的,他们的定义方式有多种,这里介绍两种最常见的方式
1、使用key=value的形式
root@zsh-virtual-machine:~# cat test_var.yaml
---
- name: test playbook vars
hosts: all
remote_user: root
vars:
user: alonzo
home: /home/alonzo
tasks:
- name: create the user {{ user }}
user:
name: "{{ user }}"
home: "{{ home }}"
2、使用yaml或者json的形式
root@zsh-virtual-machine:~# cat test_var1.yaml
---
- name: test playbook vars
hosts: all
remote_user: root
vars_files:
- /root/users.yaml
tasks:
- name: create the user {{ user }}
user:
name: "{{ user }}"
home: "{{ home }}"
root@zsh-virtual-machine:~# cat users.yaml
---
user: alonzo1
home: /home/alonzo1
注意:在playbook中变量的使用需要加双引号,否则会被错误解析成字典
资产变量
1、主机变量
root@zsh-virtual-machine:~# cat hosts
[server]
192.168.100.128 user=alonzo
root@zsh-virtual-machine:~# ansible all -i hosts -m debug -a "msg='name is {{ user }}'"
192.168.100.128 | SUCCESS => {
"msg": "name is alonzo"
}
2、主机组变量
root@zsh-virtual-machine:~# cat hosts
[servers]
192.168.100.128 user=alonzo
192.168.100.130
[servers:vars]
home="/home/alonzo"
root@zsh-virtual-machine:~# ansible all -i hosts -m "debug" -a "msg='home is {{ home }}'"
192.168.100.128 | SUCCESS => {
"msg": "home is /home/alonzo"
}
192.168.100.130 | SUCCESS => {
"msg": "home is /home/alonzo"
}
注意:主机变量的优先级大于主机组变量
3、变量的继承
root@zsh-virtual-machine:~# cat hosts
[servers]
192.168.100.128
192.168.100.130
[allservers]
[allservers:children]
servers
[allservers:vars]
user=alonzo
home="/home/alonzo"
root@zsh-virtual-machine:~# ansible all -i hosts -m "debug" -a "msg='name is {{ user }}, home is {{ home }}'"
192.168.100.128 | SUCCESS => {
"msg": "name is alonzo, home is /home/alonzo"
}
192.168.100.130 | SUCCESS => {
"msg": "name is alonzo, home is /home/alonzo"
}
4、Inventory内置变量的说明
内置变量基本上都是以ansible_为前缀
ansible_ssh_host #ssh的主机,要连接的主机与之前设定的主机的别名不同可用此变量
ansible_ssh_port #ssh的端口
ansible_ssh_user #ssh的用户名
ansible_ssh_pass #ssh密码
ansible_sudo_pass #sudo密码
ansible_sudo_exe #sudo命令路径
ansible_ssh_private_key_file #ssh使用的私钥文件
ansible_python_interpreter #目标主机python的路径
Facts变量
手动收集Facts变量
root@zsh-virtual-machine:~# ansible all -i localhost, -c local -m setup
localhost | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.100.129",
"172.17.0.1"
],
"ansible_all_ipv6_addresses": [
"fe80::ab41:46ea:3769:3734",
"fe80::42:e6ff:fe0c:6607",
"fe80::ac74:1ff:fe93:c02d"
],
"ansible_apparmor": {
"status": "enabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "02/27/2020",
"ansible_bios_version": "6.00",
"ansible_cmdline": {
"BOOT_IMAGE": "/boot/vmlinuz-5.4.0-52-generic",
"quiet": true,
"ro": true,
"root": "UUID=2adfa333-b010-4ccd-b3ad-41f5717705ef",
"splash": true
},
"ansible_date_time": {
"date": "2020-11-04",
"day": "04",
"epoch": "1604495082",
"hour": "21",
"iso8601": "2020-11-04T13:04:42Z",
"iso8601_basic": "20201104T210442626327",
"iso8601_basic_short": "20201104T210442",
"iso8601_micro": "2020-11-04T13:04:42.626327Z",
"minute": "04",
"month": "11",
"second": "42",
"time": "21:04:42",
"tz": "CST",
"tz_offset": "+0800",
"weekday": "星期三",
"weekday_number": "3",
"weeknumber": "44",
"year": "2020"
},
"ansible_default_ipv4": {
"address": "192.168.100.129",
"alias": "ens33",
"broadcast": "192.168.100.255",
"gateway": "192.168.100.2",
"interface": "ens33",
"macaddress": "00:0c:29:b3:15:4c",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.100.0",
"type": "ether"
},
···
···
过滤Facts变量
- 获取服务器的内存情况
root@zsh-virtual-machine:~# ansible all -i localhost, -m setup -a "filter=*memory*" -c local
localhost | SUCCESS => {
"ansible_facts": {
"ansible_memory_mb": {
"nocache": {
"free": 809,
"used": 3099
},
"real": {
"free": 132,
"total": 3908,
"used": 3776
},
"swap": {
"cached": 7,
"free": 1804,
"total": 2047,
"used": 243
}
},
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
- 获取服务器的挂载情况
root@zsh-virtual-machine:~# ansible all -i localhost, -m setup -a "filter='*mount*'" -c local
localhost | SUCCESS => {
"ansible_facts": {
"ansible_mounts": [
{
"block_available": 0,
"block_size": 131072,
"block_total": 248,
"block_used": 248,
"device": "/dev/loop6",
"fstype": "squashfs",
"inode_available": 0,
"inode_total": 472,
"inode_used": 472,
"mount": "/snap/snapd/9721",
"options": "ro,nodev,relatime",
"size_available": 0,
"size_total": 32505856,
"uuid": "N/A"
},
{
"block_available": 0,
"block_size": 131072,
"block_total": 443,
"block_used": 443,
"device": "/dev/loop0",
"fstype": "squashfs",
"inode_available": 0,
"inode_total": 10779,
"inode_used": 10779,
"mount": "/snap/core18/1932",
"options": "ro,nodev,relatime",
"size_available": 0,
"size_total": 58064896,
"uuid": "N/A"
},
{
"block_available": 0,
"block_size": 131072,
"block_total": 3,
"block_used": 3,
"device": "/dev/loop4",
"fstype": "squashfs",
"inode_available": 0,
"inode_total": 230,
"inode_used": 230,
"mount": "/snap/gnome-characters/570",
"options": "ro,nodev,relatime",
"size_available": 0,
"size_total": 393216,
"uuid": "N/A"
},
{
"block_available": 0,
"block_size": 131072,
"block_total": 497,
"block_used": 497,
"device": "/dev/loop3",
"fstype": "squashfs",
"inode_available": 0,
"inode_total": 62342,
"inode_used": 62342,
"mount": "/snap/gtk-common-themes/1506",
"options": "ro,nodev,relatime",
"size_available": 0,
"size_total": 65142784,
"uuid": "N/A"
},
{
"block_available": 0,
"block_size": 131072,
"block_total": 18,
"block_used": 18,
"device": "/dev/loop8",
"fstype": "squashfs",
"inode_available": 0,
"inode_total": 784,
"inode_used": 784,
"mount": "/snap/gnome-system-monitor/148",
"options": "ro,nodev,relatime",
"size_available": 0,
"size_total": 2359296,
"uuid": "N/A"
},
{
"block_available": 0,
"block_size": 131072,
"block_total": 1744,
"block_used": 1744,
"device": "/dev/loop5",
"fstype": "squashfs",
"inode_available": 0,
"inode_total": 18513,
"inode_used": 18513,
"mount": "/snap/gnome-3-34-1804/60",
"options": "ro,nodev,relatime",
"size_available": 0,
"size_total": 228589568,
"uuid": "N/A"
},
{
"block_available": 70483722,
"block_size": 4096,
"block_total": 77146320,
"block_used": 6662598,
"device": "/dev/sda1",
"fstype": "ext4",
"inode_available": 19431972,
"inode_total": 19660800,
"inode_used": 228828,
"mount": "/",
"options": "rw,relatime,errors=remount-ro",
"size_available": 288701325312,
"size_total": 315991326720,
"uuid": "2adfa333-b010-4ccd-b3ad-41f5717705ef"
},
{
"block_available": 0,
"block_size": 131072,
"block_total": 20,
"block_used": 20,
"device": "/dev/loop2",
"fstype": "squashfs",
"inode_available": 0,
"inode_total": 1384,
"inode_used": 1384,
"mount": "/snap/gnome-calculator/826",
"options": "ro,nodev,relatime",
"size_available": 0,
"size_total": 2621440,
"uuid": "N/A"
},
{
"block_available": 0,
"block_size": 131072,
"block_total": 443,
"block_used": 443,
"device": "/dev/loop12",
"fstype": "squashfs",
"inode_available": 0,
"inode_total": 10775,
"inode_used": 10775,
"mount": "/snap/core18/1885",
"options": "ro,nodev,relatime",
"size_available": 0,
"size_total": 58064896,
"uuid": "N/A"
},
{
"block_available": 0,
"block_size": 131072,
"block_total": 240,
"block_used": 240,
"device": "/dev/loop11",
"fstype": "squashfs",
"inode_available": 0,
"inode_total": 463,
"inode_used": 463,
"mount": "/snap/snapd/8542",
"options": "ro,nodev,relatime",
"size_available": 0,
"size_total": 31457280,
"uuid": "N/A"
},
{
"block_available": 0,
"block_size": 131072,
"block_total": 3,
"block_used": 3,
"device": "/dev/loop10",
"fstype": "squashfs",
"inode_available": 0,
"inode_total": 230,
"inode_used": 230,
"mount": "/snap/gnome-characters/550",
"options": "ro,nodev,relatime",
"size_available": 0,
"size_total": 393216,
"uuid": "N/A"
},
{
"block_available": 0,
"block_size": 131072,
"block_total": 2045,
"block_used": 2045,
"device": "/dev/loop9",
"fstype": "squashfs",
"inode_available": 0,
"inode_total": 24339,
"inode_used": 24339,
"mount": "/snap/gnome-3-34-1804/36",
"options": "ro,nodev,relatime",
"size_available": 0,
"size_total": 268042240,
"uuid": "N/A"
},
{
"block_available": 0,
"block_size": 131072,
"block_total": 20,
"block_used": 20,
"device": "/dev/loop1",
"fstype": "squashfs",
"inode_available": 0,
"inode_total": 1351,
"inode_used": 1351,
"mount": "/snap/gnome-calculator/748",
"options": "ro,nodev,relatime",
"size_available": 0,
"size_total": 2621440,
"uuid": "N/A"
},
{
"block_available": 0,
"block_size": 131072,
"block_total": 8,
"block_used": 8,
"device": "/dev/loop7",
"fstype": "squashfs",
"inode_available": 0,
"inode_total": 355,
"inode_used": 355,
"mount": "/snap/gnome-logs/100",
"options": "ro,nodev,relatime",
"size_available": 0,
"size_total": 1048576,
"uuid": "N/A"
}
],
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
- 在playbook中使用Facts变量
root@zsh-virtual-machine:~# cat fact.yaml
---
- name: print facts vars
hosts: all
remote_user: root
tasks:
- name: print ip
debug:
msg: "The default IPV4 is {{ ansible_default_ipv4.address }}"
root@zsh-virtual-machine:~# ansible-playbook -i hosts fact.yaml
PLAY [print facts vars] **********************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************
ok: [192.168.100.130]
ok: [192.168.100.128]
TASK [print ip] ******************************************************************************************************************
ok: [192.168.100.128] => {
"msg": "The default IPV4 is 192.168.100.128"
}
ok: [192.168.100.130] => {
"msg": "The default IPV4 is 192.168.100.130"
}
PLAY RECAP ***********************************************************************************************************************
192.168.100.128 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.100.130 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
- 关闭playbook中facts变量获取
root@zsh-virtual-machine:~# cat fact.yaml
---
- name: print facts vars
hosts: all
remote_user: root
gather_facts: no
tasks:
- name: test
shell: ls /root
root@zsh-virtual-machine:~# ansible-playbook -i hosts fact.yaml
PLAY [print facts vars] **********************************************************************************************************
TASK [test] **********************************************************************************************************************
changed: [192.168.100.130]
changed: [192.168.100.128]
PLAY RECAP ***********************************************************************************************************************
192.168.100.128 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.100.130 : ok=1 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
注册变量
用于保存一个task任务的执行结果,以便于做进一步的处理
注册变量在playbook中通过register关键字去实现
root@zsh-virtual-machine:~# cat fact.yaml
---
- name: print facts vars
hosts: all
remote_user: root
gather_facts: no
tasks:
- name: test
shell: ls /root
register: check
- name: print check
debug: var=check.stdout
root@zsh-virtual-machine:~# ansible-playbook -i hosts fact.yaml
PLAY [print facts vars] **********************************************************************************************************
TASK [test] **********************************************************************************************************************
changed: [192.168.100.130]
changed: [192.168.100.128]
TASK [print check] ***************************************************************************************************************
ok: [192.168.100.128] => {
"check.stdout": "aaa.txt\ninstall_docker.sh"
}
ok: [192.168.100.130] => {
"check.stdout": "aaa.txt"
}
PLAY RECAP ***********************************************************************************************************************
192.168.100.128 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
192.168.100.130 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
变量优先级
全局变量 > 剧本变量 > 资产变量