西安网站维保公司,html5 手机网站页面实例,c 教程如何做网站,icp备案单位网站Temlates模块
jinja模板架构#xff0c;通过模板可以实现向模板文件传参(python转义)把占位符参数传到配置文件中去,生产一个目标文本文件#xff0c;传递变量到需要的配置文件当中 #xff08;web开发#xff09; nginx.conf.j2 早文件当中配置的是占位符#xff08;声明…Temlates模块
jinja模板架构通过模板可以实现向模板文件传参(python转义)把占位符参数传到配置文件中去,生产一个目标文本文件传递变量到需要的配置文件当中 web开发 nginx.conf.j2 早文件当中配置的是占位符声明的变量
/etc/ansible/hosts配置了主机的占位符名称和j2文件的占位符一致(定义参数占位符的参数声明好)
playbook当中用template模块来把参数传给目标主机的配置文件
nginx
到nginx.conf里 mv nginx.conf /opt/nginx.conf.j2
vim /etc/ansible/hosts vim nginx.yml
- hosts: allremote_user: rootvars:- package: nginx- service: nginxtasks:- name: install nginxyum: name{{package}}- name: install configure filetemplate: src/opt/nginx.conf.j2 dest/etc/nginx/nginx.confnotify:- restart nginx- name: create root_dirfile:path: /opt/nginx/htmlstate: directory- name: start nginxservice: name{{service}} enabledtrue statestartedhandlers:- name: restart nginxservice: name{{service}} staterestartedtags模块
标签模块可以在playbook当中为任务设定标签(tags)我们在运行playbook可以通过指定任务标签来实现只允许设定的标签任务
任务标签的种类
always:不管你是否指定了运行标签任务都会执行
never是否运行了指定标签该任务也不会执行
debug:调试任务
setup:收集主机信息
自定义标签
per_tasks指定标签之前的任务
post_tasks:运行指定标签之后的任务 #在目标主机上复制文件/opt/guoqi.txt #在20.0.0.14 touch guoqu,txt always #在目标20.0.0.14复制文件/opy/yy.txt #自定义标签 #第一次运行playbook 不指定标签查看文件生成情况 指定标签为自定义查看文件生成情况
Roles模块
角色
ansible层次化结构化的组织playbook 使用了rolse(角色)
可以根据层次结构自动装在变量文件task以及handiers等等
rolse分别把变量文件 任务模块以及处理器 放在单独的目录当中使用relse模块来一键调用这些文件
rolses
-----web--总目录角色
files 存放copy和script模块调用的文件
templaes 存放j2的模板文件
tasks包含任务的目录
------main.yml 校色运行的任务
handlers包含处理器的目录
------main.yml
vars存放变量目录
------main.yml
defaults:包含默认变量的目录
------main.yml
meta包含元信息的目录
------main.yml
site.yml用来调用所有的配置文件
三个服务分别是三个角色
http
mysql
php
现在/etc/ansible/roles下面创建好php http mysql的目录
分别在php http和mysql目录下建创建 在http php 和mysql目录里先进入tasks
mysql的
- name: install mysqlyum: name{{pkg}}
- name: start mysqlservice: enabledtrue name{{svc}} statestarted
php的
- name: install phpyum: name{{pkg}}
- name: start php-fpmservice: enabledtrue name{{svc}} statestarted
http的
- name: install httpdyum: name{{pkg}}
- name: start httpdservice: enabledtrue name{{svc}} statestarted 在http php 和mysql目录里先进入vars
mysql的
pkg:- mariadb- mariadb-server
svc: mariadbhttpd的
pkg: httpd
svc: httpdphp的
pkg:- php- php-fpm
svc: php-fpm在/etc/ansible/roles/site.yml
- hosts: 20.0.0.14remote_user: rootroles:- httpd- mysql- php ansible-playbook site.yml