Ansible中的每个模块专注于某一方面的功能。虽然每个模块实现的功能都比较简单,但是,将各个模块结合起来就可以实现比较复杂的功能。在Ansible中,将各个模块组合起来的文件是一个YAML格式的配置文件。这个配置文件,在Ansible中称为Playbook

Nginx示例

---
- hosts: webservers #指定执行任务的主机,可以是一个或多个由冒号分割的主机组
vars: #定义变量
worker_processes: 4
num_cpus: 4
max_open_file: 65506
root: /data/www
remote_user: heboan #指定远程主机上执行任务的用户
sudo: yes
tasks: #定义任务,任务列表中的各任务按次序逐个在hosts指定的主机上执行,即在所有主机上完成一个任务后再开始第二个,如果中途出错,所有已执行的任务都回滚
- name: install depend packages
command: yum install -y gcc gcc-c++ openssl-devel pcre-devel
- name: unarchive nginx-1.12.2.tar.gz
unarchive: src=/etc/ansible/templates/nginx/nginx-1.12.2.tar.gz dest=/home/heboan/tools
- name: create nginx user
user: name=nginx shell=/sbin/nologin
- name: install nginx
shell: cd /home/heboan/tools/nginx-1.12.2 && ./configure --prefix=/opt/app/nginx --user=nginx --group=nginx && make && make install
- name: cp nginx.service
copy: src=/etc/ansible/templates/nginx/nginx.service dest=/usr/lib/systemd/system/nginx.service
- name: copy index.html
copy: src=/etc/ansible/templates/nginx/index.html dest=/data/www/index.html
- name: start nginx
service: name=nginx state=started
- name: write the nginx config file
template: src=/etc/ansible/templates/nginx/nginx2.conf dest=/opt/app/nginx/conf/nginx.conf #这是模板,里面可以识别上面定义的变量
notify: #执行完任务(write the nginx config file)的关联操作
- reload nginx #触发此操作,下面需要定义操作内容
handlers: #定义关联操作
- name: reload nginx #关联操作任务
service: name=nginx state=restarted
user  nginx;
worker_processes {{worker_processes}}; error_log logs/error.log;
pid logs/nginx.pid; events {
worker_connections 1024;
} worker_rlimit_nofile {{max_open_file}}; http {
include mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on;
tcp_nopush on; keepalive_timeout 65; #gzip on; server {
listen 80;
server_name localhost;
root {{root}}; location / {
} } }

nginx2.conf

[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network.target remote-fs.target nss-lookup.target [Service]
Type=forking
PIDFile=/opt/app/nginx/logs/nginx.pid
ExecStartPre=/opt/app/nginx/sbin/nginx -t -c /opt/app/nginx/conf/nginx.conf
ExecStart=/opt/app/nginx/sbin/nginx -c /opt/app/nginx/conf/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s QUIT $MAINPID
PrivateTmp=true [Install]
WantedBy=multi-user.target

nginx.service

第5天:Ansible-Playbook的更多相关文章

  1. Ansible playbook API 开发 调用测试

    Ansible是Agentless的轻量级批量配置管理工具,由于出现的比较晚(13年)基于Ansible进行开发的相关文档较少,因此,这里通过一些小的实验,结合现有资料以及源码,探索一下Ansible ...

  2. ansible playbook实践(四)-如何调试写好的playbook文件

    有时,我们写了一个长长,功能很强悍的yaml文件,但是,我们有可能会担心,写的yaml文件是否正确,是否有漏洞危机,毕竟是要修改线上的机器,那么,有可能我们可以从以下几个检查维度来进行,确保在大规模应 ...

  3. ansible playbook批量改ssh配置文件,远程用户Permission denied

    最近手里的数百台服务器需要改/etc/ssh/sshd_config的参数,禁止root直接登陆,也就是说 [root@t0 ~]# cat /etc/ssh/sshd_config | grep R ...

  4. ansible笔记(11):初识ansible playbook(二)

    ansible笔记():初识ansible playbook(二) 有前文作为基础,如下示例是非常容易理解的: --- - hosts: test211 remote_user: root tasks ...

  5. ansible笔记(10):初识ansible playbook

    ansible笔记():初识ansible playbook 假设,我们想要在test70主机上安装nginx并启动,我们可以在ansible主机中执行如下3条命令 ansible test70 -m ...

  6. Ansible playbook 批量修改服务器密码 先普通后root用户

    fsckzy   Ansible playbook 批量修改服务器密码 客户的需求:修改所有服务器密码,密码规则为Rfv5%+主机名后3位 背景:服务器有CentOS6.7,SuSE9.10.11,r ...

  7. 写Ansible playbook添加zabbix被监控的对象

    本主题达到的效果是能通过编写Ansible Playbook,创建zabbix主机组,把被监控的对象加入到zabbix监控系统中,同时链接到对象的模板. 1.准备工作 在zabbix服务器上面,我们需 ...

  8. Ansible playbook基础组件介绍

    本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...

  9. ansible playbook基本操作

    一.ansible playbook简单使用 相当于是把模块写入到配置文件里面 vim /etc/ansible/test.yml //写入如下内容: --- - hosts: 127.0.0.1 r ...

  10. ansible入门四(Ansible playbook基础组件介绍)

    本节内容: ansible playbook介绍 ansible playbook基础组件 playbook中使用变量 一.ansible playbook介绍 playbook是由一个或多个“pla ...

随机推荐

  1. C11工具类:时间处理

    C++11提供时间管理类,包括三种类型:时间间隔duration,时钟clocks,时间点time point. 1.记录时常的duration 1.1 原型 duration表示一段时间间隔,用来记 ...

  2. zk-web

    Ref:https://github.com/qiuxiafei/zk-web zk-web是一个用clojure with noir and boostrap写的Zookeeper WEB UI管理 ...

  3. 使用Redirector插件解决googleapis公共库加载的问题【转】

    转自:http://www.cnblogs.com/kari/p/5860371.html 最近访问一些面向国外的网站总是会出现ajax.googleaips.com无法加载的情况.以下为加载stac ...

  4. FC4-i386-SRPMS

    [重点] http://archives.fedoraproject.org/pub/archive/fedora/linux/core/6/ http://archives.fedoraprojec ...

  5. MVC使用Newtonsoft无需实体类,实现JSON数据返回给前端页面使用

    //引用using Newtonsoft.Json; using Newtonsoft.Json.Linq; public ActionResult JsonSample() { ResponseRe ...

  6. http状态码+http请求方式

    一.http状态码 2开头 (请求成功)表示成功处理了请求的状态代码. 200   (成功)  服务器已成功处理了请求. 通常,这表示服务器提供了请求的网页. 201   (已创建)  请求成功并且服 ...

  7. c++设计模式系列----单例模式(Singleton模式

    单例模式是为了解决唯一对象实例问题而提出来的,许多时候整个系统只需要拥有一个全局对象,这样有利于我们协调系统整体的行为.比如在某个服务器程序中,该服务器的配置信息存放在一个文件中,这些配置数据由一个单 ...

  8. HDU 6109 数据分割 并查集,SET

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6109 题意:中文题面 解法:每次都贪心地尝试将尽量多的条件放进当前这组,遇到第一个与已有条件冲突时,就 ...

  9. swift 动态获取类, 获取命名空间

    在做swift开发中很多时候会动态加载控制器的类, 可以让app更加灵活显示界面信息 一般情况下都是服务器返回显示的控制器类name然后动态显示, 但是服务器返回的类name是string, 怎么转换 ...

  10. 微信支付之SHA256签名失败

    在接微信支付的时候,或多或少会遇到签名失败,本人接入的时候也遇了不少次: 总结如下: 1.参数没有经过ASCII排序 2.参数包含中文未经过UTF-8标准转化加密后的签名不对应(经本人测验:加密算法要 ...