Ansible_8.lnmp简易版

基本步骤

- 1. 创建role,通过role完成项目

- 2. 部署nginx调度器

- 3. 部署两台lnmp服务器

- 4. 部署mariaDB数据库

 

基本配置

- 192.168.3.111      node1      # 测试服务器
- 192.168.3.112      node2      # 代理服务器
- 192.168.3.113      node3      # web服务器
  192.168.3.114      node4      # web服务器
- 192.168.3.115      node5      # 数据库服务器

# 在/etc/hosts中添加地址映射

# 在/data/ansible/hosts中 设置主机分组 

 

lnmp

[root@ansible ~]# ansible-galaxy-3 init /data/ansible/roles/lnmp               # 创建roles
- Role /data/ansible/roles/lnmp was created successfully

[root@ansible ~]# mv /opt/nginx-1.20.2.tar.gz /data/ansible/roles/lnmp/files/
[root@ansible ~]# ansible-3 all -m shell -a 'useradd -r -s /sbin/nologin lnmp'

[root@ansible ~]# vi /data/ansible/roles/lnmp/files/install_nginx.sh
=====================================================================
#!/bin/bash
conf = /usr/local/nginx/conf/nginx.conf
yum -y install gcc pcre-devel openssl-devel make
cd /opt
tar -xvf nginx-1.20.2.tar.gz
cd nginx-1.20.2
./configure --with-http_ssl_module
make && make install                                     # 这里用 -j2会出错
sed -i '65,71s/#//' $conf                                # 去掉#号注释
sed -i '/SCRIPT_FILENAME/d' $conf                        # 删除一行
sed -i 's/fastcgi_params/fastcgi.conf/' $conf            # 修改一行
=====================================================================

[root@ansible ~]# ansible-3 all -m setup | grep hostname
[root@ansible ~]# vi /data/ansible/roles/lnmp/templates/index.html
=====================================================================
welcome to {{ ansible_hostname}} on {{ ansible_all_ipv4_addresses }}
=====================================================================

[root@ansible ~]# vi /data/ansible/roles/lnmp/tasks/main.yml
=====================================================================
# tasks file for /root/ansible/roles/lnmp
- name: copy nginx-1.20.2.tar.gz to webserver
  copy: 
    src: nginx-1.20.2
    dest: /opt/
- name: install nginx through shell script
  script: install_nginx.sh
  args:
    creates: /usr/local/nginx/sbin/nginx
- name: copy index.html to webserver
  template:
    src: index.html
    dest: /usr/local/nginx/html/index.html
- name: install php
  yum:
    name:
      - php 
      - php-fpm
      - php-mysqlnd          # centos7 用 php-mysql
      - mariadb-devel
- name: run all service
  block:
    - systemd:
        name: php-fpm
        state: started
    - shell: /usr/local/nginx/sbin/nginx
      args:
        creates: /usr/local/nginx/logs/nginx.pid
=====================================================================

[root@ansible ~]# vi /data/ansible/lnmp.yml
===============================================
---
- hosts: webserver
  roles:
    - lnmp
===============================================

[root@ansible ~]# ansible-playbook-3 /data/ansible/lnmp.yml
PLAY RECAP ************************************************************************
node3    : ok=7    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0  
node4    : ok=7    changed=0    unreachable=0    failed=0    skipped=1    rescued=0    ignored=0  

# 遇到的问题:
- 1.lnmp根据实际情况决定是否要创建,并且可以加到剧本里,而不是在外边执行。
- 2.block 下边的 systemd 模块中 键值对要缩进4个,这里比较奇怪,没搞明白什么情况
- 3.shell里的 sed 无问题,但是无法执行;换了个方式,直接拷贝编写好的conf
================================================
    - name: copy nginx.conf to webserver
      copy:
        src: web_nginx.conf
        dest: /usr/local/nginx/conf/nginx.conf
        backup: yes
================================================

# conf修改的内容
===============================================
# 这里正确的编写是将65到71行注释去掉,看源文件,我这里行发生了变化
# sed -i '65,71s/#//' $conf
location ~ \.php$ {
    root           html;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    # 下边一行要删除掉 我这里注释了  sed -i '/SCRIPT_FILENAME/d' $conf
    # fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
    # sed -i 's/fastcgi_params/fastcgi.conf/' $conf
    # include        fastcgi_params;
    include        fastcgi.conf
}
===============================================

 

proxy

[root@ansible ~]# ansible-galaxy-3 init /data/ansible/roles/proxy
[root@ansible ~]# cp nginx-1.20.2.tar.gz /data/ansible/roles/proxy/files/
[root@ansible ~]# cp /usr/local/nginx/conf/nginx.conf /data/ansible/roles/proxy/files/
[root@ansible ~]# cp /data/ansible/roles/{lnmp/files/install_nginx.sh,proxy/files/install_proxy.sh}

# 配置文件修改的内容
===================================================
http {
    upstream webs {                                # 这里配置集群
        ip_hash;
        server 192.168.3.113;
        server 192.168.3.114;
    }

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://webs;              # 这里加一行,调用集群
            root   html;
            index  index.html index.htm;
        }
===================================================

[root@ansible ~]# vi /data/ansible/roles/proxy/tasks/main.yml
=============================================================
---
# tasks file for /data/ansible/roles/proxy
- name: copy source file to proxy
  copy:
    src: nginx-1.20.2.tar.gz
    dest: /opt/
- name: install nginx
  script: install_proxy.sh
  args:
    creates: /usr/local/nginx/sbin/nginx
- name: copy nginx.conf to proxy
  copy:
    src: nginx.conf
    dest: /usr/local/nginx/conf/nginx.conf
    backup: yes
=============================================================

[root@ansible ~]# vi /data/ansible/proxy.yml
=============================================================
---
- hosts: node2
  roles:
    - proxy

- hosts: node5
  tasks:
    - name: install mariadb server
      yum:
        name:
          - mariadb
          - mariadb-server
          - mariadb-devel
    - name: run mariadb server
      systemd:
        name: mariadb
        state: started
=============================================================

[root@ansible ~]# ansible-playbook-3 /data/ansible/proxy.yml
posted @   梵高de画笔  阅读(41)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
点击右上角即可分享
微信分享提示