3.yum安装分布式LNMP--剧本

文章目录

    • 修改hosts
    • 创建剧本文件

修改hosts

vim /etc/ansible/hosts

[webservers]
192.168.242.67

[dbservers]
192.168.242.68

[phpservers]
192.168.242.69

创建剧本文件

vim lnmp.yaml
- name: nginx play
  hosts: webservers
  remote_user: root
  vars:
  - http_port: 192.168.242.67:80
  - http_hostname: www.ggl.com
  - root_dir: /usr/share/nginx/html
  - http_remote: 192.168.242.69:9000
  - service: nginx

  tasks:
  - name: disable firewalld
    service: name=firewalld  state=stopped  enabled=no
    
  - name: disable selinux
    shell: "/usr/sbin/setenforce 0"
    ignore_errors: true
  
  - name: copy nginx.repo
    copy: src=nginx.repo dest=/etc/yum.repos.d/

  - name: install nginx
    yum: name={{service}} state=latest

  - name: copy index.php
    copy: src=index.php dest={{root_dir}}

  - name: copy nginx.conf
    template: src=default.conf.j2 dest=/etc/nginx/conf.d/default.conf
    notify: reload nginx

  - name: start nginx
    service: name={{service}} state=started enabled=yes
  handlers:
  - name: reload nginx
    service: name={{service}} state=reloaded
    
- name: mysql play
  hosts: dbservers
  remote_user: root
  tasks:
  - name: disable firewalld
    service: name=firewalld  state=stopped  enabled=no
    
  - name: disable selinux
    shell: "/usr/sbin/setenforce 0"
    ignore_errors: true

  - name: yum remove mariadb
    yum: name=mariadb* state=absent

  - name: install rpm
    shell: wget -P /etc/yum.repos.d/ http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
    ignore_errors: true

  - name: install noarch.rpm
    shell: yum -y install /etc/yum.repos.d/mysql57-community-release-el7-10.noarch.rpm
    ignore_errors: true

  - name: replace wprd
    replace: path=/etc/yum.repos.d/mysql-community.repo regexp='gpgcheck=1' replace='gpgcheck=0'
    ignore_errors: true

  - name: install mysql
    yum: name="mysql-server"

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


  - name: login mysql
    shell: mysql -uroot -p"$(grep "password" /var/log/mysqld.log | awk 'NR==1 {print $NF}')" --connect-expired-password -e "ALTER USER 'root'@'localhost' IDENTIFIED BY 'Admin@123';"

  - name: grantmysql
    shell: mysql -uroot -pAdmin@123 -e "grant all privileges on *.* to root@'%' identified by 'Admin@123' with grant option;"


- name: php play
  hosts: phpservers
  remote_user: root
  tasks:
  vars: 
  - service: php-fpm
  - timezone: Asia/Shanghai
  - user_name: php
  - http_port: 192.168.242.69:9000
  - remote_addr: 192.168.242.67
  - root_dir: /usr/share/nginx/html
  - mysql_dir: /var/lib/mysql/mysql.sock
  tasks:
  - name: disable firewalld
    service: name=firewalld  state=stopped  enabled=no
    
  - name: disable selinux
    shell: "/usr/sbin/setenforce 0"
    ignore_errors: true
    
  - name: install yum repo
    shell: "rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm && rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm"
    ignore_errors: true

  - name: install php
    with_items:
    - php72w
    - php72w-cli
    - php72w-common
    - php72w-devel
    - php72w-embedded
    - php72w-gd
    - php72w-mbstring
    - php72w-pdo
    - php72w-xml
    - php72w-fpm
    - php72w-mysqlnd
    - php72w-opcache
    yum: name={{item}}

  - name: create php user
    user: name={{user_name}}

  - name: create php group
    group: name={{user_name}}

  - name: create web dir
    file: name={{root_dir}} state=directory


  - name: copy index.php
    copy: src=index.php dest={{root_dir}}


  - name: modify php.ini
    template: src=php.ini.j2 dest=/etc/php.ini
    notify: reload php

  - name: copy www.conf
    copy: src=www.conf dest=/etc/php-fpm.d/www.conf

  - name: start php
    service: name={{service}} state=started enabled=yes
  handlers:
  - name: reload php
    service: name={{service}} state=reloaded
                                                              
  
  
##在剧本的当前目录中,需要准备需要的文件

index.php
nginx.repo
default.conf.j2

www.conf
php.ini.j2
###  index.php


#  nginx.repo


[nginx]
name=nginx repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true
###修改www.conf文件


##主要修改几行配置文件

user = php
group = php
listen = 192.168.242.69:9000
listen.allowed_clients = 192.168.242.67
##  php.ini.j2  配置文件模板


###修改几行

1097行
mysqli.default_socket = {{mysql_dir}}

877行
date.timezone = {{timezone}}
##templates目录  中存放  nginx 的  j2  的配置文件模板

##修改几行

listen       {{http_port}};
server_name  {{http_hostname}};

root   {{root_dir}};


    location ~ \.php$ {
        root           {{root_dir}};
        fastcgi_pass   {{http_remote}};
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  {{root_dir}}$fastcgi_script_name;
        include        fastcgi_params;
    }

你可能感兴趣的:(12.Ansible,分布式)