变量介绍
1 |
变量提供了便捷的方式来管理Ansible playbook的每一个项目中的动态值,比如nginx-1.6.3这个软件包的版本,在其它地方或许会反复使用,那么如果讲此值设置为变量,然后再在其他的playbook中调用,会方便许多。如此一来还方便维护,减少维护的成本。 |
定义变量的方式
1 2 3 4 5 |
1.通过命令行进行变量定义 2.在playbook文件中进行变量定义 3.通过Inventory主机信息文件中进行变量定义 4.通过vars_files的方式定义变量 5.通过host_vars和group_vars定义变量 |
在playbook文件中进行变量定义
1)方式一:在模块下面定义变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
- hosts: web_group tasks: - name: ensure a list of packages installed yum: name: "{{ packages }}" vars: packages: - httpd - httpd-tools - name: remove server yum: name: "{{ packages }}" state: absent vars: packages: - httpd - httpd-tools #问题:模块下定义变量,变量只在当前动作生效,到其他动作后变量需要重新定义 |
2)方式二:在hosts下面定义变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- hosts: web_group vars: packages: - httpd - httpd-tools tasks: - name: ensure a list of packages installed yum: name: "{{ packages }}" - name: yum: name: "{{ packages }}" state: absent |
3)方式三:在hosts下面定义多个变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
- hosts: web_group vars: packages: - httpd - httpd-tools db: - mariadb-server - MySQL-python tasks: - name: install web server yum: name: "{{ packages }}" - name: install mariadb-server yum: name: "{{ db }}" #问题:变量卸载hosts下面,可以解决多个动作分别设置变量的问题,但是设置的变量只能在当前的playbook中使用,其他的playbook无法使用 |
使用vars_file定义变量
1)定义变量文件
1 2 3 4 5 |
[root@m01 ~]# vim vars.yml web: - httpd - httpd-tools db: mariadb-server |
2)palybook调用变量文件
1 2 3 4 5 6 7 8 9 10 11 12 13 |
[root@m01 ~]# vim moudle_vars.yml - hosts: web_group vars_files: /root/vars.yml tasks: - name: install web server yum: name: "{{ web }}" state: absent - name: install mariadb-server yum: name: "{{ db }}" state: absent |
3)添加变量文件
1 2 3 4 |
[root@m01 ~]# vim vars1.yml nfs: - nfs-utils - rpcbind |
4)调用多个变量文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
[root@m01 ~]# vim moudle_vars.yml - hosts: web_group vars_files: - /root/vars.yml - /root/vars1.yml tasks: - name: install web server yum: name: "{{ web }}" - name: install mariadb-server yum: name: "{{ db }}" - name: install nfs server yum: name: "{{ nfs }}" |
在Inventory中定义变量
1)主机清单中定义变量
1 2 3 4 5 6 7 8 9 10 11 |
[root@m01 ~]# vim /etc/ansible/hosts [web_group] web01 ansible_ssh_pass='1' web02 ansible_ssh_pass='1' web03 ansible_ssh_pass='1' ...... [web_group:vars] web=httpd db=mariadb-server |
2)调用变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
- hosts: web_group tasks: - name: install web server yum: name: "{{ web }}" state: absent - name: install db server yum: name: "{{ db }}" #问题: 1.主机清单定义变量,只能给某个组使用,如果所有主机都使用需要定义很多变量 2.主机清单内容很多的时候,配置变量会使文件变得太大 |
3)复合组定义变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
[root@m01 ~]# vim /etc/ansible/hosts [web_group] web01 ansible_ssh_pass='1' web02 ansible_ssh_pass='1' web03 ansible_ssh_pass='1' ... ... [nginx_group:children] web_group slb [web_group:vars] web=httpd db=mariadb-server [nginx_group:vars] web=nginx rsync=rsync |
4)调用变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
[root@m01 ~]# vim mkdir.yml - hosts: nginx_group tasks: - name: Mkdir dir file: path: /tmp/{{web}} state: directory - name: Mkdir dir file: path: /tmp/{{rsync}} state: directory #执行结果 [root@m01 ~]# ansible-playbook mkdir.yml [root@web01 ~]# ll /tmp/ total 0 drwxr-xr-x 2 root root 6 Sep 23 17:52 httpd drwxr-xr-x 2 root root 6 Sep 23 17:52 rsync [root@lb01 ~]# ll /tmp/ total 0 drwxr-xr-x 2 root root 6 Sep 23 17:52 nginx drwxr-xr-x 2 root root 6 Sep 23 17:52 rsync #结论:主机清单定义变量时,复合组定义的变量优先级低于主机组定义的 |
使用内置变量
1)直接使用内置变量
1 2 3 4 5 6 7 |
[root@m01 ~]# vim mkdir.yml - hosts: nginx_group tasks: - name: Mkdir dir file: path: /backup/{{ansible_fqdn}}_{{ansible_eth1.ipv4.address}}_{{ansible_date_time.date}} state: directory |
2)内置变量设置变量
1 2 3 4 5 6 7 8 9 10 11 |
[root@m01 ~]# vim mkdir.yml - hosts: nginx_group vars: - remote_ip: "{{ansible_eth1.ipv4.address}}" - host: "{{ansible_fqdn}}" - date: "{{ansible_date_time.date}}" tasks: - name: Mkdir dir file: path: /backup/{{remote_ip}}_{{host}}_{{date}} state: directory |
3)内置变量常用方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#编写配置文件 [root@m01 ~]# vim /root/conf/redis.conf bind {{ansible_eth1.ipv4.address}} #编写playbook [root@m01 ~]# vim redis.yml - hosts: web_group tasks: - name: Install Redis Server yum: name: redis - name: Config Redis Server template: src: /root/conf/redis.conf dest: /etc/ #执行 [root@m01 ~]# ansible-playbook redis.yml #查看结果 [root@web01 ~]# grep "^bind" /etc/redis.conf bind "172.16.1.7" [root@web02 ~]# grep "^bind" /etc/redis.conf bind "172.16.1.8" [root@web03 ~]# grep "^bind" /etc/redis.conf bind "172.16.1.9" |
通过host_vars和group_vars定义变量
1 2 3 4 |
之前的几种变量定义都不是很好用,比较好用的是在Ansible项目目录下创建两个变量目录: host_vars group_vars 切记,目录名字一定要一致,不能做任何修改。 |
1)主机组定义变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
1.创建主机组变量目录(名字一定是group_vars) [root@m01 ~]# mkdir group_vars 2.主机组目录下创建变量文件(文件名字一定是主机清单中主机组的名字) [root@m01 ~]# vim group_vars/web_group web: httpd 3.调用变量 [root@m01 ~]# vim mkdir.yml - hosts: web_group tasks: - name: Mkdir dir file: path: /tmp/{{web}} state: directory |
2)主机定义变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
1.创建主机变量目录(名字一定是host_vars) [root@m01 ~]# mkdir host_vars 2.主机目录下创建变量文件(文件名字一定是主机清单中主机的名字) [root@m01 ~]# vim host_vars/web01 web: web01_host_vars 3.调用变量 [root@m01 ~]# vim mkdir.yml - hosts: web_group tasks: - name: Mkdir dir file: path: /tmp/{{web}} state: directory 4.执行 [root@m01 ~]# ansible-playbook mkdir.yml 5.查看结果 [root@web01 ~]# ll /tmp total 0 drwxr-xr-x 2 root root 6 Sep 23 18:57 web01_host_vars [root@web02 ~]# ll /tmp total 0 drwxr-xr-x 2 root root 6 Sep 23 18:57 httpd [root@web03 ~]# ll /tmp total 0 drwxr-xr-x 2 root root 6 Sep 23 18:57 httpd |
3)测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#1.配置主机清单 [root@m01 ~]# cat /etc/ansible/hosts [web_group] web01 ansible_ssh_pass='1' web02 ansible_ssh_pass='1' web03 ansible_ssh_pass='1' [slb] lb01 ansible_ssh_pass='1' [nginx_group:children] web_group slb #2.配置变量 [root@m01 ~]# cat group_vars/nginx_group web: nginx_group [root@m01 ~]# cat group_vars/web_group web: web_group [root@m01 ~]# cat host_vars/web01 web: web01_host_vars #3.调用变量 [root@m01 ~]# vim mkdir.yml - hosts: nginx_group tasks: - name: Mkdir dir file: path: /tmp/{{web}} state: directory #4.执行 [root@m01 ~]# ansible-playbook mkdir.yml #5.查看结果 [root@lb01 ~]# ll /tmp/ drwxr-xr-x 2 root root 6 Sep 23 19:02 nginx_group [root@web01 ~]# ll /tmp total 0 drwxr-xr-x 2 root root 6 Sep 23 19:02 web01_host_vars [root@web02 ~]# ll /tmp total 0 drwxr-xr-x 2 root root 6 Sep 23 19:02 web_group [root@web03 ~]# ll /tmp total 0 drwxr-xr-x 2 root root 6 Sep 23 19:02 web_group #6.结论: 主机定义的优先级高于主机组,主机组定义的变量优先级高于复合组 |
通过命令行进行变量定义
1 |
通过命令行覆盖变量,Inventory的变量会被playbook文件中覆盖,这两种方式的变量都会被命令行直接指定变量所覆盖,使用--extra-vars或者-e设置变量 |
1)使用命令行定义单个变量
1 |
[root@m01 ~]# ansible-playbook mkdir.yml -e "web=commend" |
2)使用命令行定义多个变量
1 2 3 |
[root@m01 ~]# ansible-playbook mkdir.yml -e "web=commend" -e "db=mysql" #注意:指定的变量会根据命令行执行,没有指定的变量继续按照变量的设置执行 |