playbook-剧本 介绍node
playbooks是 一个不一样于使用Ansible命令行执行方式的模式,其功能更强大灵活。简单来讲,playbook是一个很是简单的配置管理和多主机部署系统,不一样于任何已经存在的模式,可做为一个适合部署复杂应用程序的基础。Playbook能够定制配置,能够按照指定的操做步骤有序执行,支持同步和异步方式。值得注意的是playbook是经过YAML格式来进行描述定义的。nginx
核心元素web
Tasks:任务,由模板定义的操做列表服务器
Variables:变量异步
Templates:模板,即便用模板语法的文件ide
Handlers:处理器 ,当某条件知足时,触发执行的操做spa
Roles:角色命令行
配置Playbook分发Nginx3d
服务器环境准备:rest
192.168.20.41 ansible服务器
192.168.20.42 分发服务器
分发nginx_static.conf配置文件
配置nginx_install.yaml
[root@k8s-master2 install]# more nginx_install.yaml
---
- hosts: webservers
vars:
http_port: 8080
server_name: www.hahashen.com
remote_user: root
gather_facts: false
tasks:
- name: 安装nginx
yum: pkg=nginx state=latest
- name: 写入nginx配置文件
template: src=nginx_static.conf dest=/etc/nginx/nginx_static.conf
notify:
- restart nginx
- name: 确保nginx正在运行
service: name=nginx state=started enabled=yes
handlers:
- name: restart nginx
service: name=nginx state=reloaded
检查配置文件
[root@k8s-master2 install]# ansible-playbook -C nginx_install.yaml
分发配置文件并启动服务
[root@k8s-master2 install]# ansible-playbook nginx_install.yaml
验证42服务器
配置文件最好放到统一的目录下,从include vhost/*.conf
[root@k8s-node1 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful