变量的名字有字母、下划线和数字组成,必须以字母开头
如下变量命名为正确:
good_a ok_b
如下变量命名错误:
_aa 12bb
注意:保留的关键字不能作为变量名称
根据变量的作用范围大概的将变量分为:
但只有一个比较粗糙的划分,不能囊括Ansible中的所有变量;
全局变量,使我们使用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" }
此种变量的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 }}" # 注意:这里的双引号不能去掉
在之前的学习中学习了资产,资产共分为静态资产和动态资产。
资产变量分为主机变量和组变量,分别针对资产中的单个主机和主机组
以下资产中,定义了一个主机变量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!" }
以下资产中,定义了一组变量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" }
当主机变量和组变量在同一个资产发生重名的情况,会有什么效果?
[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" }
[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" }
内置变量几乎都是以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
Facts变量不包含在前文文中介绍的全局变量、剧本变量、及资产变量之内。
Facts变量不需要我们人为去声明变量名及赋值
它的声明和赋值完全有Ansible中的setup模块帮我们完成
它收集了有关被管理服务器的操作系统版本,服务器IP地址、主机名、磁盘的使用情况、CPU个数、内存大小等等有关被管理服务器的私有信息
在每次playbook运行的时候都会发现playbook执行前都会有一个Gathering Facts的过程。这个过程就是收集被管理服务器的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" }, ........
通过刚才的手动收集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 }