1. 程式人生 > >ansible劇本搭建lamp/lnmp練習

ansible劇本搭建lamp/lnmp練習

為192.168.122.111配置lamp或者lnmp環境
在伺服器端安裝ansible,為192.168.122.111設定域名為kvm1,推公鑰,開啟kvm1的sshd服務。
在/etc/ansible/hosts中配置kvm1的引數,做過解析寫域名,沒做過解析寫ip
建立目錄準備寫劇本
[[email protected] ~]# mkdir /role
role目錄總結構:
[[email protected] role]# tree . -L 1
.
├── apache (apache目錄,定義安裝httpd角色)
├── mariadb (mariadb目錄,定義安裝mariadb角色)
├── nginx (nginx目錄,定義安裝nginx角色)
├── php_apache (php_apache目錄安裝與httpd對應配置的php)
├── php_nginx (php_nginx目錄安裝與nginx對應配置的php)
└── site.yaml (site.yaml檔案,存放哪個主機用哪個角色)
/role/apache目錄總結構


[[email protected] ~]#cd apache/
[[email protected] apache ]#tree . -L 1
.
├── files (files目錄,用來存放沒有變數的檔案)
├── handlers (目錄,存放配置觸發器的檔案)
├── tasks (目錄,存放劇本檔案)
├── templates (目錄,存放使用變數的檔案,一般放準備好的配置檔案)
└── vars (目錄,存放定義整個角色變數的檔案,鍵值對方式定義變數)
目錄名字為相應的模組名,將每一個功能分離出來定義成角色更有條理性。
apache結構具體內容
[
[email protected]
apache ]# tree
.
├── files
│ └── index.html (檔案,httpd測試用的網頁檔案)
├── handlers
│ └── main.yaml (檔案,配置觸發器內容)
├── tasks
│ └── main.yaml (檔案,主要劇本)
├── templates
│ └── httpd.conf.j2 (檔案,為httpd服務準備的配置檔案,其中一些用了變數)
└── vars
└── main.yaml (檔案,存放了httpd配置檔案中的變數定義)
檔案具體內容

[[email protected] apache ]# cat files/index.html
welcome to apache again

[[email protected] apache]# cat tasks/main.yaml    handlers/main.yaml  
---
- name: install httpd
  yum: name=httpd state=present

- name: copy httpd configure
  template: src=httpd.conf.j2 dest=/etc/httpd/conf/httpd.conf
  notify: reload httpd

- name: httpd test page
  copy: src=index.html dest=/var/www/html/index.html

- name: remove nginx 
  yum: name=nginx state=absent

- name: start httpd service
  service: name=httpd state=started enabled=yes

---
- name: reload httpd
  service: name=httpd state=reloaded

在tasks/main.yaml用notify呼叫了handlers/main.yaml 里名為reload httpd的任務列表,當配置檔案有變化時,則reload httpd服務(因為httpd服務與nginx共用80埠,若有nginx服務正在執行則httpd服務無法啟動,因此先解除安裝nginx再啟動httpd)。
template模組(可以獲取變數的值,copy模組不能)會自動識別templates目錄裡的以j2(Jinja2模板)結尾的檔案,因此src不用絕對路徑。
[[email protected] apache ]# cat vars/main.yaml
httpd_port: 80
將配置檔案裡的埠用變量表示,ansible中用{{ }}使用變數
[[email protected] apache ]# cat templates/httpd.conf.j2 | grep Listen
Listen {{ httpd_port }}

/role/mariadb目錄總結構

[[email protected] mariadb]# tree
.
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
│ └── my.cnf.j2 (mariadb的配置檔案)
└── vars
└── main.yaml
[[email protected] mariadb]# cat tasks/main.yaml handlers/main.yaml
在這裡插入圖片描述
在這裡插入圖片描述
[[email protected] mariadb]# cat vars/main.yaml
data_dir: /var/lib/mysql
socket_file: /var/lib/mysql/mysql.sock
[[email protected] mariadb]# cat templates/my.cnf.j2
[mysqld]
datadir= {{ data_dir }}
socket= {{ socket_file }}
symbolic-links=0
[mysqld_safe]
log-error=/var/log/mariadb/mariadb.log
pid-file=/var/run/mariadb/mariadb.pid

/role/php_apache目錄總結構
[[email protected] php_apache]# tree
.
├── files
│ └── index.php
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
│ ├── php.ini.j2
│ └── www.conf.j2
└── vars
└── main.yaml

[[email protected] php_apache]# cat tasks/main.yaml  handlers/main.yaml 
---
- name: install php
  yum: name={{ item }} state=present
  with_items:
  - php-mysql
  - php
  - php-gd
  - gd
  - php-fpm

- name: copy php.ini configure
  template: src=php.ini.j2 dest=/etc/php.ini
  notify: restart httpd

- name: copy www.conf configure
  template: src=www.conf.j2 dest=/etc/php-fpm.d/www.conf
  notify: restart httpd

- name: php test page
  copy: src=index.php dest=/var/www/html/index.php

---
- name: restart httpd
  service: name=httpd state=restarted

[[email protected] php_apache]# cat files/index.php

<?php phpinfo(); ?>

[[email protected] php_apache]# cat vars/main.yaml
user: apache
num1: 2048M
num2: 2048M
num3: 2048M
[[email protected] php_apache]# cat templates/php.ini.j2 | grep num[0-9]
memory_limit = {{ num1 }}
post_max_size = {{ num2 }}
upload_max_filesize = {{ num3 }}
安裝lamp的環境
在/role/site.yaml配置kvm1主機扮演角色如下
[[email protected] role]# pwd
/role
[[email protected] role]# cat site.yaml
在這裡插入圖片描述

lnmp環境和lamp環境中的mariadb配置相同
/role/nginx 目錄總結構
[[email protected] nginx]# tree
.
├── files
│ └── index.html
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
│ └── nginx.conf.j2
└── vars
└── main.yaml
在這裡插入圖片描述
配置nginx來支援php
找到一個沒有改過的nginx配置檔案,發現在nginx安裝目錄下的conf.d/default.conf下有模版檔案,直接copy到我們在templates裡準備的nginx.conf中,新增以下行:

    server {
        listen       {{ nginx_port }} default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
        }
location ~ \.php$ {
            root                {{ nginx_web_dir }};
            fastcgi_pass        127.0.0.1:9000;
            fastcgi_index       index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include             fastcgi_params;
        }
        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }



/role/php_nginx目錄總結構
[[email protected] php_nginx]# tree
.
├── handlers
│ └── main.yaml
├── tasks
│ └── main.yaml
├── templates
│ ├── php.ini.j2
│ └── www.conf.j2
└── vars
└── main.yaml

[[email protected] php_nginx]# cat tasks/main.yaml  handlers/main.yaml vars/main.yaml 
---
- name: install php
  yum: name={{ item }} state=present
  with_items:
  - php-mysql
  - php
  - php-gd
  - gd
  - php-fpm

- name: start php-fpm service
  service: name=php-fpm state=started enabled=yes

- name: copy php configure
  template: src=php.ini.j2 dest=/etc/php.ini
  notify: restart nginx
---
- name: restart nginx
  service: name=nginx state=restarted
user: apache
num1: 2048M
num2: 2048M
num3: 2048M

Nginx是個輕量級的HTTPserver,必須藉助第三方的FCGI處理器(php-fpm)才可以對PHP進行解析。
[[email protected] php_nginx]# cat templates/php.ini.j2 |grep num[0-9]
memory_limit = {{ num1 }}
post_max_size = {{ num2 }}
upload_max_filesize = {{ num3 }}
安裝lnmp的環境
在/role/site.yaml配置kvm1主機扮演角色如下

[[email protected] role]# cat site.yaml 
---
- hosts: kvm1
  roles:
  - nginx
  - mariadb
  - php_nginx