list together nested with_items
Templates 模块
jinja模块架构,通过模板可以实现向模板文件传参(python转义)把占位符参数传到配置文件中去。
生产一个目标文本文件,传递变量到文本文件当中去。
实验:
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
yum -y install httpd
cd /etc/httpd/conf
cp httpd.conf /opt/httpd.conf.j2
cd /opt
修改httpd配置
42 Listen {{ http_port}}
95 ServerName {{server_name}}
119 DocumentRoot "{{root_dir}}"
vim /etc/ansible/hosts

hosts文件要和httpd.conf.j2文件占位符一致

httpd.conf.j2 在文件当中配置的是占位符(声明的变量)
/etc/ensible/hosts 配置了主机的占位符名称和j2文件中的占位符一致(定义参数:占位符的参数声明好)
playbook当中,template模块来把参数传给 目标的主机的配置文件。
nginx
yum安装nginx
yum -y install nginx
cp nginx.conf /opt/nginx.conf.j2
vim nginx.conf.j2




tags模块:
标签模块,可以在playbook当中为任务设定标签(tags),我们在允许playbook时可以通过指定任务标签,来实现只运行设定的标签任务。

任务标签的种类:
always:不管你是否指定了运行标签,任务都会执行
nerver:即使运行了指定标签,改任务也不会执行
debug:调试任务
setup:收集主机信息
自定义标签
per_tasks:指定标签之前的任务
post_tasks: 运行指定标签之后的任务
简单实验

运行时指定标签
ansible-playbook test2.yml --tags="debug"

ansible-playbook test2.yml --tags="nerver"

唯一特殊就是alawys,不管指定谁总会运行。
练习:
在目标touch quoqi. txt always
在目标主机复制分件/opt/wdf. txt


所有的任务默认标签就是alawys
roles模块
角色:
ansible层次化,结构化的组织playbook,使用了rolse(角色)
可以根据层次的结果,自带装载变量文件,task,以及handlers等等
rolse:分别把变量文件任务模块以及处理器,放在单独的目录当中,使用rolse模块来一键调用这些文件
roles:总目录
------web----总目录,角色
files 存放copy和script模块调用文件
templates 存放j2的模板文件
tasks 包含任务的目录
-------main.yml 角色运行的任务
handers 包含处理器的目录
-------main.yml
vars 存放变量的目录
-------main.yml
defaults 包含默认变量的目录
-------main.yml
meta 包含元信息的目录
-------main.yml
导演文件:
site.yml用来调用所有的配置文件。
实验:
安装:三个服务
http
mysql
php
cd /eyc/ansible/roles
安装之后自动有roles
创建三个服务目录

在不同的服务目录下创建不同的任务目录

在defaults,vars,tasks,meta,handers创建main.yml

在每个服务目录的tasks下的写main.yml
cd tasks/
vim main.yml



在vars目录下写main.yml
cd vars/
vim main.yml



三个角色要执行的任务和变量
编辑导演文件:
vim site.yml

开始
ansible-playbook site.yml


