tags模块作用
你写了一个很长的playbook,其中有很多的任务,这并没有什么问题,不过在实际使用这个剧本时,你可能只是想要执行其中的一部分任务而已,或者,你只想要执行其中一类任务而已,而并非想要执行整个剧本中的全部任务,这个时候我们该怎么办呢?我们可以借助tags实现这个需求。
tags模块案例操作
- hosts: 20.0.0.4
remote_user: root
tasks:
- name: copy aaa1
copy: src=/opt/aaa.txt dest=/opt/aaa1.txt
tags:
- aaa
- name: copy aaa2
copy: src=/opt/aaa.txt dest=/opt/aaa2.txt
tags:
- bbb
注:执行任务调用 aaa 标签,只会执行第一个任务,后面第二个任务不会执行
[root@master ~]# ansible-playbook aaa.yml --tags="aaa"
执行tags=aaa的结果,与执行tags=bbb的结果
注:如果tags=结果一样 会一样执行
template模板介绍
作用:可以将带有参数的配置文件传递到目标地址,可以对文件进行属组属主的修改以及备份
Jinja2:Jinja2是基于python的模板引擎,功能比较类似于于PHP的smarty,J2ee的Freemarker和velocity。它能完全支持unicode,并具有集成的沙箱执行环境,应用广泛。jinja2使用BSD授权。
template模板案例
在 master上安装服务,并将服务配置发送给 node:20.0.0.4
- hosts: node
remote_user: root
vars:
- server: httpd
tasks:
- name: install apache
yum: name={
{
server}} state=latest
- name: config file
template: src=/etc/httpd/conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
notify:
- restart httpd
- name: start httpd
service: name={
{
server}} state=started
handlers :
- name: restart httpd
service: name={
{
server}} state=restarted enabled=true
修改需要到其他节点上的配置为变量
[root@master ~]# vi httpd/httpd.conf
Listen 80 修改 Listen {
{
ip_port}} # 变量为 ip_port
#ServerName www.example.com:80 修改 ServerName {
{
yuming_port}}
MaxClients {
{
num}} # 添加最大连接数,变量为 num
vi /etc/ansible/hosts 设置值的变量
[master]
20.0.0.3
[node]
20.0.0.4 ip_port=20.0.0.4:80 yuming_port=www.aa.com:80 num=600
20.0.0.5
20.0.0.4查看
服务httpd有没有 启动没有
扫描二维码关注公众号,回复:
12886293 查看本文章

[root@node1 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
Active: active (running) since 五 2021-01-15 15:05:22 CST; 3min 8s ago
vi /etc/httpd/conf/httpd.conf
Listen 20.0.0.4:80
ServerName www.aa.com:80
MaxClients 600