参考文档: http://www.ansible.com.cn/docs/playbooks_variables.html#variables
合法的变量
ansible变量是有数字,字母,下划线组成并且变量始终应该以字母开头。
"foo_port"是个合法的变量名."foo5"也是. "foo-port", "foo port", "foo.port" 和 "12"则不是合法的变量名.
在Inventory文件中创建变量
例如:
定义主机变量:
bash
192.168.0.132 ansible_user="root" ansible_password="123456" ansible_port=22 ansible_ssh_common_args='-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'
这里变量使用了:
ansible_user
ansible_password
ansible_port
ansible_ssh_common_args
编写ansible-playbook 调用debug模块打印变量:
ansible-playbook 代码如下:
bash
---
- hosts: 192.168.0.132
tasks:
- name: variable test
debug:
msg:
ansible_user: "{{ ansible_user }}"
ansible_password: "{{ ansible_password }}"
ansible_port: "{{ ansible_port }}"
ansible_ssh_common_args: "{{ ansible_ssh_common_args }}"
运行效果:
bash
[root@localhost ~]# ansible-playbook playbook.yaml
PLAY [192.168.0.132] ****************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.0.132]
TASK [variable test] ****************************************************************************************************************************************
ok: [192.168.0.132] => {
"msg": {
"ansible_password": 123456,
"ansible_port": 22,
"ansible_ssh_common_args": "-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null",
"ansible_user": "root"
}
}
PLAY RECAP **************************************************************************************************************************************************
192.168.0.132 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
定义组变量:
bash
[client]
192.168.0.132
[client:vars]
`ntp_server`=ntp.aliyun.com
编写ansible-playbook 调用debug模块打印变量:
ansible-playbook 代码如下:
bash
---
- hosts: client
tasks:
- name: variable test
debug:
msg:
ntp_server: "{{ ntp_server }}"
运行效果:
bash
[root@localhost ~]# ansible-playbook playbook.yaml
PLAY [client] ***********************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.0.132]
TASK [variable test] ****************************************************************************************************************************************
ok: [192.168.0.132] => {
"msg": {
"ntp_server": "ntp.aliyun.com"
}
}
PLAY RECAP **************************************************************************************************************************************************
192.168.0.132 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在playbook中定义变量
使用 var
关键字定义变量:
定义全局变量:
bash
---
- hosts: 192.168.0.132
vars:
var1: 123456
var2: 654321
tasks:
- name: Use vars
debug:
msg:
var1: "{{ var1 }}"
var2: "{{ var2 }}"
运行效果:
bash
[root@localhost ~]# ansible-playbook playbook.yaml
PLAY [192.168.0.132] ****************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.0.132]
TASK [Use vars] *********************************************************************************************************************************************
ok: [192.168.0.132] => {
"msg": {
"var1": 123456,
"var2": 654321
}
}
PLAY RECAP **************************************************************************************************************************************************
192.168.0.132 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
创建局部变量:
bash
---
- hosts: 192.168.0.132
tasks:
- name: Local variable
vars:
Local_var1: "Local variable"
debug:
msg: "This is a local variable : {{ Local_var1 }}"
运行结果同上。。。
使用文件创建变量
使用vars_files
关键字引入外部变量文件:
变量使用 key/value 格式,一行一个
bash
[root@localhost ~]# cat vars.yml
var1: 123456
playbook文件内容如下:
bash
[root@localhost ~]# cat playbook.yaml
---
- hosts: 192.168.0.132
vars_files:
- vars.yml
tasks:
- name: Use vars from file
debug:
msg:
var1: "{{ var1 }}"
如何
vars.yml
在本地就不需要指定路径,如果路径不在本地,就需要指定路径
运行结果:
bash
[root@localhost ~]# ansible-playbook playbook.yaml
PLAY [192.168.0.132] ****************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.0.132]
TASK [Use vars from file] ***********************************************************************************************************************************
ok: [192.168.0.132] => {
"msg": {
"var1": 123456
}
}
PLAY RECAP **************************************************************************************************************************************************
192.168.0.132 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
在命令行中使用-e或--extra-vars
选项传递变量
playbook代码如下:
从命令行中传入var
变量
bash
[root@localhost ~]# cat playbook.yaml
---
- hosts: 192.168.0.132
tasks:
- name: Use vars from file
debug:
msg:
var: "{{ var }}"
bash
[root@localhost ~]# ansible-playbook playbook.yaml -e "var=value"
PLAY [192.168.0.132] ****************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.0.132]
TASK [Use vars from file] ***********************************************************************************************************************************
ok: [192.168.0.132] => {
"msg": {
"var": "value"
}
}
PLAY RECAP **************************************************************************************************************************************************
192.168.0.132 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
传入多个变量需要改动playbook代码如下:
bash
---
- hosts: 192.168.0.132
tasks:
- name: Use vars from file
debug:
msg:
var1: "{{ var1 }}"
var2: "{{ var2 }}"
bash
[root@localhost ~]# ansible-playbook playbook.yaml -e "var1=value2 var2=value2"
PLAY [192.168.0.132] ****************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************************
ok: [192.168.0.132]
TASK [Use vars from file] ***********************************************************************************************************************************
ok: [192.168.0.132] => {
"msg": {
"var1": "value2",
"var2": "value2"
}
}
PLAY RECAP **************************************************************************************************************************************************
192.168.0.132 : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
使用
--extra-vars
传输变量:传入单个变量:
ansible-playbook your-playbook.yml --extra-vars '{"var": "value"}'
传入多个变量:
ansible-playbook your-playbook.yml --extra-vars '{"var1": "value1", "var2": "value2"}'
变量的优先级
- extra vars (在命令行中使用 -e)优先级最高
- 然后是在inventory中定义的连接变量(比如ansible_ssh_user)
- 接着是大多数的其它变量(命令行转换,play中的变量,included的变量,role中的变量等)
- 然后是在inventory定义的其它变量
- 然后是由系统发现的facts
- 然后是 "role默认变量", 这个是最默认的值,很容易丧失优先权