1、通过Inventory文件定义主机和主机组变量
Ansible默认的Inventory文件是INI格式。直接开始测试,先看定义的host文件:
[root@zero01 inventory]# cat host_playbook
192.168.79.129 key=129
192.168.79.130 key=130
192.168.79.131 key=131
192.168.79.132 key=132
[nginx]
192.168.79.1[29:32]
[nginx:vars]
ansible_python_interpreter=/usr/bin/python2.6
接下来,编写一个playbook文件验证变量的引用是否正确,内容如下:
[root@zero01 playbook]# cat variable.yaml
---
- hosts: all
gather_facts: False
tasks:
- name: diplay Host Variable from hostfile
debug: msg="The {{ inventory_hostname }} Value is {{ key }}"
运行playbook(不指定绝对路径要在playbook文件下运行),如下所示:
[root@zero01 playbook]# ansible-playbook variable.yaml
PLAY [all] *******************************************************************************************************************
TASK [diplay Host Variable from hostfile] *******************************************************************************************************************
ok: [192.168.79.129] => {
"msg": "The 192.168.79.129 Value is 129"
}
ok: [192.168.79.130] => {
"msg": "The 192.168.79.130 Value is 130"
}
ok: [192.168.79.131] => {
"msg": "The 192.168.79.131 Value is 131"
}
ok: [192.168.79.132] => {
"msg": "The 192.168.79.132 Value is 132"
}
PLAY RECAP *******************************************************************************************************************
192.168.79.129 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.130 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.131 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.132 : ok=1 changed=0 unreachable=0 failed=0
可以看到,每台主机都引用了自己的主机变量key;下面开始测试主机组变量,先注释掉每台主机的变量定义,再给nginx组定义一个主机组变量,变量名为key且值为zero_gg(此处如果要修改变量名,要对应修改playbook),如下所示:
[root@zero01 inventory]# cat host_playbook
#192.168.79.129 key=129
#192.168.79.130 key=130
#192.168.79.131 key=131
#192.168.79.132 key=132
[nginx]
192.168.79.1[29:32]
[nginx:vars]
ansible_python_interpreter=/usr/bin/python2.6
key=zero_gg
运行playbook文件:
[root@zero01 playbook]# ansible-playbook variable.yaml
PLAY [all] *******************************************************************************************************************
TASK [diplay Host Variable from hostfile] *******************************************************************************************************************
ok: [192.168.79.129] => {
"msg": "The 192.168.79.129 Value is zero_gg"
}
ok: [192.168.79.130] => {
"msg": "The 192.168.79.130 Value is zero_gg"
}
ok: [192.168.79.131] => {
"msg": "The 192.168.79.131 Value is zero_gg"
}
ok: [192.168.79.132] => {
"msg": "The 192.168.79.132 Value is zero_gg"
}
PLAY RECAP *******************************************************************************************************************
192.168.79.129 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.130 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.131 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.132 : ok=1 changed=0 unreachable=0 failed=0
可以看到,主机组变量针对组内所有的主机都生效。注意:如果主机同时定义了主机变量和主机组变量,名字相同时,主机变量生效,主机组变量不生效;名字不同时,都可以调用。
2、通过/etc/ansible/下的文件定义主机和主机组变量
使用yum安装Ansible默认配置文件是在/etc/ansible/目录下,可在该目录下,创建文件夹host_vars定义主机变量,创建group_vars定义主机组变量(此情况要求yaml文件不再/etc/ansible/的子目录下);如果采用其他的安装方式,在playbook文件当前目录下创建两个目录即可。如下:
[root@zero01 ansible]# tree
.
├── ansible.cfg
├── hosts
├── inventory
│ ├── host
│ └── host_playbook
├── playbook
│ ├── group_vars
│ │ └── nginx
│ ├── host_vars
│ │ ├── 192.168.79.129
│ │ ├── 192.168.79.130
│ │ ├── 192.168.79.131
│ │ └── 192.168.79.132
│ ├── nginx.yaml
│ ├── tasks
│ │ └── main.yaml
│ ├── variable.retry
│ └── variable.yaml
└── roles
[root@zero01 inventory]# cat host
192.168.79.129
192.168.79.130
192.168.79.131
192.168.79.132
[nginx]
192.168.79.1[29:32]
分别在group_vars、host_vars下创建主机组变量文件和主机变量文件,内容如下:
[root@zero01 ansible]# cat group_vars/nginx
---
key: zero_gg
[root@zero01 ansible]# head host_vars/*
==> host_vars/192.168.79.129 <==
---
key: 192.168.79.129
==> host_vars/192.168.79.130 <==
---
key: 192.168.79.130
==> host_vars/192.168.79.131 <==
---
key: 192.168.79.131
==> host_vars/192.168.79.132 <==
---
key: 192.168.79.132
运行playbook文件,结果如下:
[root@zero01 playbook]# ansible-playbook variable.yaml
PLAY [all] *******************************************************************************************************************
TASK [diplay Host Variable from hostfile] *******************************************************************************************************************
ok: [192.168.79.129] => {
"msg": "The 192.168.79.129 Value is 192.168.79.129"
}
ok: [192.168.79.130] => {
"msg": "The 192.168.79.130 Value is 192.168.79.130"
}
ok: [192.168.79.131] => {
"msg": "The 192.168.79.131 Value is 192.168.79.131"
}
ok: [192.168.79.132] => {
"msg": "The 192.168.79.132 Value is 192.168.79.132"
}
PLAY RECAP *******************************************************************************************************************
192.168.79.129 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.130 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.131 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.132 : ok=1 changed=0 unreachable=0 failed=0
由上可验证,当主机变量和主机组变量同时存在且名字相同时,只有主机变量生效,主机组变量不生效;
下面验证group_vars/下的nginx组的主机组变量,如下:
[root@zero01 playbook]# rm -rf host_vars/*
[root@zero01 playbook]# ansible-playbook variable.yaml
PLAY [all] *******************************************************************************************************************
TASK [diplay Host Variable from hostfile] *******************************************************************************************************************
ok: [192.168.79.129] => {
"msg": "The 192.168.79.129 Value is zero_gg"
}
ok: [192.168.79.130] => {
"msg": "The 192.168.79.130 Value is zero_gg"
}
ok: [192.168.79.131] => {
"msg": "The 192.168.79.131 Value is zero_gg"
}
ok: [192.168.79.132] => {
"msg": "The 192.168.79.132 Value is zero_gg"
}
PLAY RECAP *******************************************************************************************************************
192.168.79.129 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.130 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.131 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.132 : ok=1 changed=0 unreachable=0 failed=0
3、通过ansible-playbook命令行传入
通过ansible-playbook命令行传参的方式定义变量,默认传进去的都是全局变量,如下:
[root@zero01 playbook]# ansible-playbook variable.yaml -e "key=ZERO"
PLAY [all] *******************************************************************************************************************
TASK [diplay Host Variable from hostfile] *******************************************************************************************************************
ok: [192.168.79.129] => {
"msg": "The 192.168.79.129 Value is ZERO"
}
ok: [192.168.79.130] => {
"msg": "The 192.168.79.130 Value is ZERO"
}
ok: [192.168.79.131] => {
"msg": "The 192.168.79.131 Value is ZERO"
}
ok: [192.168.79.132] => {
"msg": "The 192.168.79.132 Value is ZERO"
}
PLAY RECAP *******************************************************************************************************************
192.168.79.129 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.130 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.131 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.132 : ok=1 changed=0 unreachable=0 failed=0
这种方式同时支持传入多个变量,还支持指定文件的方式传入变量,变量的文件内容支持两种格式:YAML和JSON
YAML:
[root@zero01 tmp]# cat var.yaml
---
key: YAML
[root@zero01 playbook]# ansible-playbook variable.yaml -e "@/tmp/var.yaml"
PLAY [all] *******************************************************************************************************************
TASK [diplay Host Variable from hostfile] *******************************************************************************************************************
ok: [192.168.79.129] => {
"msg": "The 192.168.79.129 Value is YAML"
}
ok: [192.168.79.130] => {
"msg": "The 192.168.79.130 Value is YAML"
}
ok: [192.168.79.131] => {
"msg": "The 192.168.79.131 Value is YAML"
}
ok: [192.168.79.132] => {
"msg": "The 192.168.79.132 Value is YAML"
}
PLAY RECAP *******************************************************************************************************************
192.168.79.129 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.130 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.131 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.132 : ok=1 changed=0 unreachable=0 failed=0
JSON:
[root@zero01 tmp]# cat var.json
{"key": "JSON"}
[root@zero01 playbook]# ansible-playbook variable.yaml -e "@/tmp/var.json"
PLAY [all] *******************************************************************************************************************
TASK [diplay Host Variable from hostfile] *******************************************************************************************************************
ok: [192.168.79.129] => {
"msg": "The 192.168.79.129 Value is JSON"
}
ok: [192.168.79.130] => {
"msg": "The 192.168.79.130 Value is JSON"
}
ok: [192.168.79.131] => {
"msg": "The 192.168.79.131 Value is JSON"
}
ok: [192.168.79.132] => {
"msg": "The 192.168.79.132 Value is JSON"
}
PLAY RECAP *******************************************************************************************************************
192.168.79.129 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.130 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.131 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.132 : ok=1 changed=0 unreachable=0 failed=0
4、在playbook文件内使用vars
[root@zero01 playbook]# vi variable.yaml
---
- hosts: all
gather_facts: False
vars:
key: Ansible
tasks:
- name: diplay Host Variable from hostfile
debug: msg="The {{ inventory_hostname }} Value is {{ key }}"
直接运行,如下:
[root@zero01 playbook]# ansible-playbook variable.yaml
PLAY [all] *******************************************************************************************************************
TASK [diplay Host Variable from hostfile] *******************************************************************************************************************
ok: [192.168.79.129] => {
"msg": "The 192.168.79.129 Value is Ansible"
}
ok: [192.168.79.130] => {
"msg": "The 192.168.79.130 Value is Ansible"
}
ok: [192.168.79.131] => {
"msg": "The 192.168.79.131 Value is Ansible"
}
ok: [192.168.79.132] => {
"msg": "The 192.168.79.132 Value is Ansible"
}
PLAY RECAP *******************************************************************************************************************
192.168.79.129 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.130 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.131 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.132 : ok=1 changed=0 unreachable=0 failed=0
如上,playbook文件中定义的变量对所有主机都有效,可理解为主机组变量。
5、在playbook文件内使用vars_files
[root@zero01 playbook]# cat variable.yaml
---
- hosts: all
gather_facts: False
vars_files:
- /tmp/var.yaml
tasks:
- name: diplay Host Variable from hostfile
debug: msg="The {{ inventory_hostname }} Value is {{ key }}"
vars_files默认搜索当前路径,如果文件不在当前目录,需要指出绝对路径。
[root@zero01 playbook]# ansible-playbook variable.yaml
PLAY [all] *******************************************************************************************************************
TASK [diplay Host Variable from hostfile] *******************************************************************************************************************
ok: [192.168.79.129] => {
"msg": "The 192.168.79.129 Value is YAML"
}
ok: [192.168.79.130] => {
"msg": "The 192.168.79.130 Value is YAML"
}
ok: [192.168.79.131] => {
"msg": "The 192.168.79.131 Value is YAML"
}
ok: [192.168.79.132] => {
"msg": "The 192.168.79.132 Value is YAML"
}
PLAY RECAP *******************************************************************************************************************
192.168.79.129 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.130 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.131 : ok=1 changed=0 unreachable=0 failed=0
192.168.79.132 : ok=1 changed=0 unreachable=0 failed=0
6、使用register内的变量
Ansible playbook内task之间还可以互相传递数据,比如我们总共有两个tasks,其中第2个task是否执行是需要判断第1个task运行后的结果,这个时候我们就得在task之间传递数据,需要把第1个task执行的结果传递给第2个task。
Ansible task之间传递数据使用register方式
[root@zero01 playbook]# cat register.yaml
---
- hosts: all
gather_facts: False
tasks:
- name: register variable
shell: hostname
register: info
- name: display variable
debug: msg="The variable is {{ info }}"
这里把第1个task执行hostname的结果register给info这个变量,然后第2个task把这个结果使用debug模板打印出来,
如下:
[root@zero01 playbook]# ansible-playbook register.yaml -l 192.168.79.129
PLAY [all] *******************************************************************************************************************
TASK [register variable] *******************************************************************************************************************
changed: [192.168.79.129]
TASK [display variable] *******************************************************************************************************************
ok: [192.168.79.129] => {
"msg": "The variable is {'stderr_lines': [], u'changed': True, u'end': u'2017-12-23 17:46:05.777637', 'failed': False, u'stdout': u'zero02', u'cmd': u'hostname', u'rc': 0, u'start': u'2017-12-23 17:46:05.768962', u'stderr': u'', u'delta': u'0:00:00.008675', 'stdout_lines': [u'zero02']}"
}
PLAY RECAP *******************************************************************************************************************
192.168.79.129 : ok=2 changed=1 unreachable=0 failed=0
info的结果是一段Python字典数据,存储着很多信息,包括执行时间状态变化输出等信息。从字典中,取出想要的值
[root@zero01 playbook]# cat register.yaml
---
- hosts: all
gather_facts: False
tasks:
- name: register variable
shell: hostname
register: info
- name: display variable
debug: msg="The variable is {{ info['stdout'] }}"
修改register.yaml文件内容,info['stdout']是一个标准的Python语言在字典中取值的用法,执行playbook,如下所示:
[root@zero01 playbook]# ansible-playbook register.yaml -l 192.168.79.129
PLAY [all] *******************************************************************************************************************
TASK [register variable] *******************************************************************************************************************
changed: [192.168.79.129]
TASK [display variable] *******************************************************************************************************************
ok: [192.168.79.129] => {
"msg": "The variable is zero02"
}
PLAY RECAP *******************************************************************************************************************
192.168.79.129 : ok=2 changed=1 unreachable=0 failed=0
7、使用vars_prompt传入
Ansible支持在运行playbook的时,通过交互式的方式给定义好的参数传入参数值,只需在playbook中定义vars_prompt的变量名和交互式提示内容即可。
Ansible可以对输入的变量值进行加密处理,比如采用SHA512和MD5算法加密。需要注意:如果要对变量值进行加密,ansible机器上要安装passlib python库
[root@zero01 playbook]# cat prompt.yaml
---
- hosts: all
gather_facts: False
vars_prompt:
- name: "one"
prompt: "Please input one value"
private: no
- name: "two"
prompt: "Please input two value"
default: 'good'
private: yes
tasks:
- name: display one value
debug: msg="one value is {{ one }}"
- name: display two value
debug: msg="two value is {{ two }}"
one为非私有变量,two为私有变量,private的作用是交互模式下是否显示输入的变量值。
[root@zero01 playbook]# ansible-playbook prompt.yaml -l 192.168.79.129
Please input one value: Ansible
Please input two value [good]:
PLAY [all] *******************************************************************************************************************
TASK [display one value] *******************************************************************************************************************
ok: [192.168.79.129] => {
"msg": "one value is Ansible"
}
TASK [display two value] *******************************************************************************************************************
ok: [192.168.79.129] => {
"msg": "two value is zero"
}
PLAY RECAP *******************************************************************************************************************
192.168.79.129 : ok=2 changed=0 unreachable=0 failed=0
这里总结了7中常用的定义变量的方式,以及如何去引用。欢迎大家,实践指正,谢谢!
zero_gg:开明睿智,大气谦和...