Playbook分发Nginx配置文件

playbook-剧本 介绍

playbooks是 一个不同于使用Ansible命令行执行方式的模式,其功能更强大灵活。简单来说,playbook是一个非常简单的配置管理和多主机部署系统,不同于任何已经存在的模式,可作为一个适合部署复杂应用程序的基础。Playbook可以定制配置,可以按照指定的操作步骤有序执行,支持同步和异步方式。值得注意的是playbook是通过YAML格式来进行描述定义的。

核心元素

Tasks:任务,由模板定义的操作列表

Variables:变量

Templates:模板,即使用模板语法的文件

Handlers:处理器 ,当某条件满足时,触发执行的操作

Roles:角色

配置Playbook分发Nginx

服务器环境准备:

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 

image.png

验证42服务器

image.png

image.png

配置文件最好放到统一的目录下,从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



猜你喜欢

转载自blog.51cto.com/15127516/2657696