一、创建目录
[root@m01 ~]# mkdir ansible_variables
[root@m01 ~]# cd ansible_variables/
二、准备ansible的配置文件
[root@m01 ansible_variables]# cp /etc/ansible/ansible.cfg .
三、定义主机清单
[root@m01 ansible_variables]# cat hosts
[web]
172.16.1.7
172.16.1.8
四、变量剧本之文件定义变量
[root@m01 ansible_variables]# cat play_1.yml
- hosts: web
vars_files: ./var.yml #当前路径下的变量文件
tasks:
- name: touch new files
file:
path: "/tmp/{{ var1 }}"
state: touch
五、变量文件定义
[root@m01 ansible_variables]# cat var.yml
var1: play_var1
七、执行
[root@m01 ansible_variables]# cat var.yml
var1: play_var1
[root@m01 ansible_variables]# ansible-playbook play_1.yml
PLAY [web] *****************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************
ok: [172.16.1.7]
ok: [172.16.1.8]
TASK [touch new files] *****************************************************************************************************
changed: [172.16.1.7]
changed: [172.16.1.8]
PLAY RECAP *****************************************************************************************************************
172.16.1.7 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.8 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
八、检查
[root@web1 ~]# ll /tmp/
总用量 0
-rw-r--r-- 1 root root 0 7月 2 08:43 play_var1
[root@web2 ~]# ll /tmp/
total 0
-rw-r--r-- 1 root root 0 Jul 2 08:43 play_var1
九、变量剧本之主机清单定义变量
1.hosts文件编写变量
[root@m01 ansible_variables]# vim hosts
[web]
172.16.1.7
172.16.1.8
[web:vars]
inventory_var=inventory_variables #添加变量信息
2.执行剧本
[root@m01 ansible_variables]# ansible-playbook play_3.yml
PLAY [web] *****************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************
ok: [172.16.1.7]
ok: [172.16.1.8]
TASK [touch new files] *****************************************************************************************************
changed: [172.16.1.8]
changed: [172.16.1.7]
PLAY RECAP *****************************************************************************************************************
172.16.1.7 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.8 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
3.检查效果
[root@web1 ~]# ll /tmp/
总用量 0
-rw-r--r-- 1 root root 0 7月 2 08:47 inventory_variables
-rw-r--r-- 1 root root 0 7月 2 08:43 play_var1
[root@web2 ~]# ll /tmp/
total 0
-rw-r--r-- 1 root root 0 Jul 2 08:47 inventory_variables
-rw-r--r-- 1 root root 0 Jul 2 08:43 play_var1
十、变量剧本之目录下创建变量文件
1.当前路径下创建group_vars目录,并新建web文件
[root@m01 ansible_variables]# cat group_vars/web
group_vars: group_vars_web
#test_var: inventory_vars_group_vars
2.执行剧本
[root@m01 ansible_variables]# ansible-playbook play_5.yml
PLAY [web] *****************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************
ok: [172.16.1.8]
ok: [172.16.1.7]
TASK [touch new files] *****************************************************************************************************
changed: [172.16.1.7]
changed: [172.16.1.8]
PLAY RECAP *****************************************************************************************************************
172.16.1.7 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.8 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
3.检查效果
[root@web1 ~]# ll /tmp/
总用量 0
-rw-r--r-- 1 root root 0 7月 2 08:56 group_vars_web
-rw-r--r-- 1 root root 0 7月 2 08:47 inventory_variables
-rw-r--r-- 1 root root 0 7月 2 08:52 play4
-rw-r--r-- 1 root root 0 7月 2 08:43 play_var1
十一、剧本变量之playbook中定义
1.定义
[root@m01 ansible_variables]# cat play_8.yml
- hosts: web
vars: #定义变量
test_name: play_var
tasks:
- name: # 引用上面定义的变量
file: path=/tmp/{{ test_name }} state=touch
2.执行
[root@m01 ansible_variables]# ansible-playbook play_8.yml
PLAY [web] *****************************************************************************************************************
TASK [Gathering Facts] *****************************************************************************************************
ok: [172.16.1.7]
ok: [172.16.1.8]
TASK [file] ****************************************************************************************************************
changed: [172.16.1.8]
changed: [172.16.1.7]
PLAY RECAP *****************************************************************************************************************
172.16.1.7 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
172.16.1.8 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
3.查看效果
[root@web1 ~]# ll /tmp
总用量 0
-rw-r--r-- 1 root root 0 7月 2 09:08 group_vars_all
-rw-r--r-- 1 root root 0 7月 2 08:47 inventory_variables
-rw-r--r-- 1 root root 0 7月 2 08:52 play4
-rw-r--r-- 1 root root 0 7月 2 10:04 play_var
-rw-r--r-- 1 root root 0 7月 2 08:43 play_var1
十二、剧本变量之传参定义变量
1.编写剧本
[root@m01 ansible_variables]# cat play_18.yml
- hosts: web
tasks:
- name: create file
file: path=/tmp/{{ new_file }} state=touch
2.命令行传参
[root@m01 ansible_variables]# ansible-playbook play_18.yml --extra-vars "new_file=new_file"
3.检测效果
[root@web1 ~]# ll /tmp
总用量 0
-rw-r--r-- 1 root root 0 7月 2 09:08 group_vars_all
-rw-r--r-- 1 root root 0 7月 2 08:47 inventory_variables
-rw-r--r-- 1 root root 0 7月 2 08:52 play4
-rw-r--r-- 1 root root 0 7月 2 10:04 play_var
-rw-r--r-- 1 root root 0 7月 2 08:43 play_var1
-rw-r--r-- 1 root root 0 7月 2 10:20 new_file
0 Comments