Playbook分發Nginx配置文件

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 

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

相關文章
相關標籤/搜索