变量
setup模块中定义的变量
- ansible_distribution_major_version
- ansible_all_ipv4_addresses
- ansible_nodename
- ansible_processor_vcpus
- ansible_memtotal_mb
- ansible_os_family
- …
主机清单中定义的变量
[mariadbsrvs]
192.168.209.49 hostname=node3
192.168.209.59 hostname=node4
[websrvs]
192.168.209.29 hostname=node1
192.168.209.39 hostname=node2
[websrvs:vars]
suf=txt
ansible-playbook命令中定义变量
ansible-playbook -e hostname=xuepeng -e suf=com vars3.yml
在yml文件中定义变量
---
- hosts: websrvs
remote_user: root
vars: //在yml文件中定义变量
- hostname: testdir
- suf: pdf
tasks:
- name: create file
file: name=/data/{{hostname}}.{{suf}} state=touch
作为一个独立的变量yml文件
vim vars.yml
hostname: pengpeng
suf: yml
// 调用 vars.yml中的变量
---
- hosts: websrvs
remote_user: root
vars_files: vars.yml
tasks:
- name: create file
file: name=/data/{{hostname}}.{{suf}} state=touch
在角色yml文件中定义变量
- hosts: mariadbsrvs
remote_user: root
roles:
- { role: mariadb, when: ansible_distribution_major_version == "6" }
在模板配置文件中引用变量
/roles/nginx/templates/nginx.conf.j2
user {{nginx_user}};