一、Template模块
- Jinja是基于Python的模块引擎。Template类是Jinja的一个重要组件,可以看做是一个编译过的模板文件,用来产生目标文本,传递Python的变量给模板去替换模板中的标记。
- 是一种创建配置文件的工具,在配置文件中,有一些数据会有动态的改变(如:ip、主机名、端口、用户、页面路径等)需要使用变量来表示
- template模块就是将变量和动态文件结合起来,最终生成一个配置文件(需要动态改变)
- 文件配置使用jinjia2模板语言编写,一定是以.j2结尾
1、准备template模板文件
- 先准备一个以.j2为后缀的template模板文件,设置引用的变量
- 模板文件使用test1曾用的httpd.conf配置文件
cp nginx.conf nginx.conf.j2
vim nginx.conf.j2
listen {{nginx_port}};
server_name {{server_name}};
root {{root_dir}};
![image-20240508140631397](https://img-blog.csdnimg.cn/img_convert/0e1bae2729e76cea9482bf6cfcd65875.png)
2、修改主机清单文件
- 修改主机清单文件,使用主机变量定义一个变量名相同,而值不同的变量
vim /etc/ansible/hosts
[webservers]
192.168.10.12 nginx_port=192.168.10.12:80 server_name=www.liu.com:80 root_dir=/etc/nginx/htdocs
[dbservers]
192.168.10.13 nginx_port=192.168.10.13:80 server_name=www.yan.com:80 root_dir=/etc/nginx/htdocs
![image-20240508141610370](https://img-blog.csdnimg.cn/img_convert/a772e52d3c91c10dcde160df02ba5fa9.png)
vim /etc/hosts
192.168.10.12 www.liu.com
192.168.10.13 www.yan.com
![image-20240508150047248](https://img-blog.csdnimg.cn/img_convert/bc75fad0930316f0136b66a46d9c4ffe.png)
3、编写playbook
vim deam05.yaml
---
- name: install nginx
hosts: all
remote_user: root
vars:
- pck: nginx
- ser: nginx
tasks:
- name: install nginx pck
yum: name={{pck}} state=latest
- name: install configure file
template: src=/opt/nginx.conf.j2 dest=/etc/nginx/nginx.conf
notify:
- restart nginx
- name: create root dir
file: path=/etc/nginx/htdocs state=directory
- name: start nginx server
service: name={{ser}} enabled=true state=started
handlers:
- name: restart nginx
service: name={{ser}} state=restarted
![image-20240508144946290](https://img-blog.csdnimg.cn/img_convert/0f8569a3c09524dcef6268609d09a9ea.png)
4、执行playbook
ansible-playbook deam05.yaml
![image-20240508145305372](https://img-blog.csdnimg.cn/img_convert/c4f76a636d3b3089bee6cc1860055994.png)
5、准备测试网页
ansible 192.168.10.12 -m shell -a "echo 'this is template liu' > /etc/nginx/htdocs/index.html"
ansible 192.168.10.13 -m shell -a "echo 'this is template yan' > /etc/nginx/htdocs/index.html"
![image-20240508145613465](https://img-blog.csdnimg.cn/img_convert/91911bcaceab817c0e78aeadaa8fdea4.png)
6、访问测试
curl 192.168.10.12
curl 192.168.10.13
curl www.liu.com
curl www.yan.com
![image-20240508150318257](https://img-blog.csdnimg.cn/img_convert/ea37420b7b946e7eb911bed032a43bc3.png)
二、tags模块
- 可以在一个playbook中为某个或某些任务定义“标签”,在执行此playbook时通过ansible-playbook命令使用–tags选项能实现仅运行指定的tasks。
- playbook还提供了一个特殊的tags为always。作用就是当使用always当tags的task时,无论执行哪一个tags时,定义有always的tags都会执行。
1、编写脚本
vim deam06.yaml
---
- name: tags
hosts: webservers
remote_user: root
tasks:
- name: mkdir directory
file: path=/opt/liuyanfen/ state=directory
tags:
- always
- name: touch file
file: path=/opt/liuyanfen/test state=touch
tags:
- xx01
- name: copy hosts file
copy: src=/etc/hosts dest=/opt/hosts
tags:
- xx02
![image-20240508153631564](https://img-blog.csdnimg.cn/img_convert/e3865cdc23eaea1234d1f8ac60341c2a.png)
2、执行tags=“xx01”
ansible-playbook deam06.yaml --tags="xx01"
![image-20240508154042052](https://img-blog.csdnimg.cn/img_convert/130ef9968cdbc2a41c614b9003393b2e.png)
ansible webservers -a "ls -l /opt/liuyanfen"
![image-20240508152132843](https://img-blog.csdnimg.cn/img_convert/1278c4c94952cdf3d85bc1bc19a896b4.png)
3、执行tags=“xx02”
ansible webservers -m file -a "path=/opt/liuyanfen/ state=absent"
ansible webservers -a "ls -l /opt/liuyanfen"
![image-20240508152551208](https://img-blog.csdnimg.cn/img_convert/11098dd4e54c8322a832f0b5a42e28d5.png)
ansible-playbook deam06.yaml --tags="xx02"
![image-20240508154753705](https://img-blog.csdnimg.cn/img_convert/33a08350393aa68a4e02c91c6d8fb28d.png)
ansible webservers -a "ls -l /opt/liuyanfen"
ansible webservers -a "ls -l /opt/hosts"
![image-20240508155011682](https://img-blog.csdnimg.cn/img_convert/6a516b820d0a37264accb02638ec249f.png)