playbook ---> 变量
1、使用变量:简化playbook项目的一个维护,使用一个固定的字符串表示一个不固定的值
2、定义变量:
1) 通过playbook文件中的play进行定义,通过vars来定义变量
2) 通过inventory主机清单进行变量定义
3) 通过执行playbook时使用-e参数指定变量
定义变量:
1、在playbook的文件中开头通过vars关键字进行变量定义
vars:
- v1: value1
- v2: value2
- v3: value3
使用变量安装软件的yml:
版本1:
[root@proxy variables]# cat install_variable1.yml
- hosts: webservers
vars:
- web_packages: httpd
- ftp_packages: vsftpd tasks:
- name: Install {{ web_packages }} Pacages
yum: name={{ web_packages }} state=present - name: Install {{ ftp_packages }} Packages
yum: name={{ ftp_packages }} state=present
版本2:
[root@proxy variables]# cat install_variables2.yml
- hosts: webservers
vars:
- web_packages: httpd
- ftp_packages: vsftpd tasks:
- name: Install Rpm Packages "{{ web_packages }}" "{{ ftp_packages }}"
yum:
name:
- "{{ web_packages }}"
- "{{ ftp_packages }}"
state: present
使用变量卸载软件的yml:
版本1:
[root@proxy variables]# cat uninstall_variable1.yml
- hosts: webservers
vars:
- web_packages: httpd
- ftp_packages: vsftpd tasks:
- name: Install httpd Pacages
yum: name={{ web_packages }} state=absent - name: Install {{ ftp_packages }} Packages
yum: name=vsftpd state=absent
版本2:
[root@proxy variables]# cat uninstall_variables2.yml
- hosts: webservers
vars:
- web_packages: httpd
- ftp_packages: vsftpd tasks:
- name: Install Rpm Packages "{{ web_packages }}" "{{ ftp_packages }}"
yum:
name:
- "{{ web_packages }}"
- "{{ ftp_packages }}"
state: absent
2、通过定义一个变量文件,然后使用playbook进行调用
1) 准备一个变量的文件,建议使用yml格式。内容:vars_name: value
[root@proxy variables]# cat vars.yml
web_packages: httpd
ftp_packages: vsftpd
2) 准备playbook进行调用
安装脚本:
[root@proxy variables]# cat install_vars3.yml
- hosts: webservers
vars_files: ./vars.yml
tasks:
- name: Install rpm Packages "{{ web_packages }}" "{{ ftp_packages }}"
yum:
name:
- "{{ web_packages }}"
- "{{ ftp_packages }}"
state: present
卸载脚本:
[root@proxy variables]# cat uninstall_vars3.yml
- hosts: webservers
vars_files: ./vars.yml
tasks:
- name: Install rpm Packages "{{ web_packages }}" "{{ ftp_packages }}"
yum:
name:
- "{{ web_packages }}"
- "{{ ftp_packages }}"
state: absent
3、在inventory主机清单中定义变量,但是要注意:主机变量优先于主机组变量。
host_vars 特殊的变量目录,针对单个主机进行变量定义
group_vars 特殊的变量目录,针对inventory主机清单中的组进行变量定义,对A组定义的变量 B组无法调用
group_vars/all 特定的变量文件,可以对多有的主机组定义变量。
在当前目录中定义一个hosts文件
[root@proxy variables]# cat hosts
[remotes]
192.168.200.100
192.168.200.200[remotes:vars]
filename=ansible_createfile
在远程主机上创建一个文件:
[root@proxy variables]# cat vars2.yml
- hosts: remotes
remote_user: root
tasks:
- name: create a file {{ filename }} in remote /tmp
file: path=/tmp/{{ filename }} state=touch
调用:ansible-playbook vars2.yml -i hosts
在远程主机上删除一个文件:
[root@proxy variables]# cat vars3.yml
- hosts: remotes
remote_user: root
tasks:
- name: delete a file {{ filename }} in remote /tmp
file: path=/tmp/{{ filename }} state=absent
调用:ansible-playbook vars3.yml -i hosts
官方推荐在项目目录下创建两个变量的目录host_vars group_vars:
host_vars/主机名命名的文件
group_vars/组名命名的文件
在当前目录下新建一个group_vars目录,在这个目录下新建两个文件:
[root@proxy variables]# ls group_vars/
db1 db2
[root@proxy variables]# cat group_vars/db1
web_packages: httpd
ftp_packages: vsftpd
[root@proxy variables]# cat group_vars/db2
web_packages: wget
ftp_packages: tree
在/etc/ansible/hosts文件末尾添加以下行:
[db1]
192.168.200.100 ansible_user=root[db2]
192.168.200.200 ansible_user=root
编写两个yml文件:
[root@proxy variables]# cat install_db1.yml
- hosts: db1
remote_user: root
tasks:
- name: Install rpm Packages "{{ web_packages }}" "{{ ftp_packages }}"
yum:
name:
- "{{ web_packages }}"
- "{{ ftp_packages }}"
state: present[root@proxy variables]# cat install_db2.yml
- hosts: db2
remote_user: root
tasks:
- name: Install rpm Packages "{{ web_packages }}" "{{ ftp_packages }}"
yum:
name:
- "{{ web_packages }}"
- "{{ ftp_packages }}"
state: present
执行ansible-playbook install_db1.yml:db1对应的远程主机上安装httpd vsftpd
执行ansible-playbook install_db2.yml:db2对应的远程主机上安装wget tree
注意:默认情况下,group_vars目录中文件名与hosts清单中的组名保持一致,
比如在group_vars目录下创建了db1组的变量,其它组是无法使用db1组的变量
系统提供了一个特殊组,all,只需要在group_vars目录下建立一个all文件,编写好变量,所以组都可以使用。
host_vars目录下创建一个文件,文件名与inventory清单中的主机名称要保持完全一致,如果是ip地址,则创建相同
ip地址的文件即可。
3、通过执行playbook时使用-e参数指定
ot@proxy variables]# cat install_db.yml
- hosts: "{{ hosts }}"
remote_user: root
tasks:
- name: Install rpm Packages "{{ web_packages }}" "{{ ftp_packages }}"
yum:
name:
- "{{ web_packages }}"
- "{{ ftp_packages }}"
state: present ansible-playbook install_db.yml -e "hosts=db1"
ansible-playbook install_db.yml -e "hosts=db2"
ansible变量优先级:
外置传参>playbook(vars_files>vars)
>inventory(host_vars>group_vars/group_name>grou_vars/all)