ansible-playbook 批量部署lnmp環境

冰冷的燃燒著發表於2020-12-20

編寫palybook 檔案

命令:

vim lnmp.yml 

playbook檔案內容為

cat lnmp.yml 

---
- hosts: webservers
  vars:
    ngx_port: 80
    ngx_name: www.bd.com
  tasks:
    - name: "關閉防火牆"
      systemd: name=firewalld state=stopped
    - name: "關閉selinux"
      shell: setenforce 0
    - name: "安裝lnmp"
      yum: name={{ item }}  state=latest
      with_items:
        - mariadb
        - mariadb-server
        - php
        - php-mysql
        - php-fpm
        - gcc
        - pcre-devel
        - openssl-devel
    - name: "推送並解壓nginx原始碼包"
      unarchive: src=nginx-1.18.0.tar.gz  dest=/root/
    - name: "檢測編譯安裝nginx"
      shell: cd /root/nginx-1.18.0 && ./configure && make && make install
    - name: "開啟nginx服務"
      shell: /usr/local/nginx/sbin/nginx || true
    - name: "開啟php-fpm"
      systemd: name=php-fpm state=started enabled=yes
    - name: "推送PHP測試頁面"
      copy: src=index.php  dest=/usr/local/nginx/html/index.php
    - name: "推送nginx配置檔案"
      template: src=nginx.conf.j2  dest=/usr/local/nginx/conf/nginx.conf
      tags: restart_nginx
      notify: reload_nginx

  handlers:
    - name: reload_nginx
      shell: /usr/local/nginx/sbin/nginx -s reload

PHP測試頁面的內容為

[root@localhost]# cat index.php

<?php
phpinfo();
?>

nginx模板檔案的內容為

[root@localhost 1220]# cat nginx.conf.j2 | grep -v “#”

worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    server {
        listen       {{ ngx_port }}; //呼叫變數
        server_name  {{ ngx_name }}; //呼叫變數
        location / {
            root   html;
            index  index.php;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
        location ~ \.php$ {
            root           html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

使用playbook部署lnmp

[root@localhost ]# ansible-playbook  lnmp.yml

瀏覽器訪問測試

在這裡插入圖片描述

在這裡插入圖片描述

相關文章