Linux架构26 playbook实战, 安装数据库, 网站迁移

Ansible playbook实战

1.基础准备

#1.安装ansible
[root@m01 ~]# yum install -y ansible

#2.配置ansible
[root@m01 ~]# vim /etc/ansible/ansible.cfg
host_key_checking = False        #这个解开注释

#3.配置主机清单
[root@m01 ~]# vim /etc/ansible/hosts
[web_group]
web01 ansible_ssh_pass='admin123'
web02 ansible_ssh_pass='admin123'

[db_group]
db01 ansible_ssh_pass='admin123'
db02 ansible_ssh_pass='admin123'

#4.创建项目目录
[root@m01 ~]# mkdir /project/{web,mariadb,rsyncd,nfs,sersync,base} -p
[root@m01 ~]# tree /project/
/project/
├── base
├── mariadb
├── nfs
├── rsyncd
├── sersync
└── web
6 directories, 0 files

2.编写playbook

1)第一部分:所有服务器优化和创建用户

[root@m01 ~]# cd /project/base
[root@m01 base]# vim lnmp.yml
- hosts: all
  tasks:
    - name: Stop Firewalld
      systemd:
        name: firewalld
        state: stopped
        enabled: no

    - name: Stop Selinux
      selinux:
        state: disabled

    - name: Create www Group
      group:
        name: www
        gid: 666
        state: present

    - name: Create www User
      user:
        name: www
        uid: 666
        group: www
        create_home: false    # 不需要家目录
        shell: /sbin/nologin
        state: present

2)第二部分:安装nginx服务器

1.安装方式选择

#源码包安装
1.上传nginx包
copy
2.解压
unarchive
3.生成
shell
4.编译
shell
5.安装
shell
-m shell -a 'cd /nginx/ && ./configure -- && make && make install'
6.编辑system管理nginx文件
copy
7.配置conf.d的目录
8.推配置文件

#官方源安装
1.配置官方源
copy
2.安装nginx
yum
3.配置nginx

#rpm包安装

2.准备工作

#1.上传rpm包
[root@m01 ~]# rz nginx-1.16.1-1.el7.ngx.x86_64.rpm

#2.配置nginx配置文件
[root@m01 ~]# vim nginx.conf
user  www;
...

#3.配置wordpress配置文件
[root@m01 ~]# vim wordpress.conf
server {
    listen 80;
    server_name blog.linux.com;
    root /code/wordpress;
    index index.php;

    location ~ \.php$ {
        root /code/wordpress;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

3.编写playbook

[root@m01 base]# vim lnmp.yml    # 结尾继续接上
- hosts: web_group
  tasks:
    - name: Copy Nginx Rpm
      copy: 
        src: /root/nginx-1.16.1-1.el7.ngx.x86_64.rpm
        dest: /tmp/

    - name: Install Nginx Server
      yum:
        name: /tmp/nginx-1.16.1-1.el7.ngx.x86_64.rpm
        state: present

    - name: Config Nginx Server
      copy:
        src: /root/nginx.conf
        dest: /etc/nginx/

    - name: Config Wordpress Conf
      copy:
        src: /root/wordpress.conf
        dest: /etc/nginx/conf.d/

    - name: Start Nginx Server
      systemd:
        name: nginx
        state: started

3)第三部分:安装php服务

1.准备工作

#1.上传php包
[root@m01 ~]# rz php.tar.gz

#2.配置php用户
[root@m01 ~]# vim www.conf
user = www
group = www

2.编写playbook

[root@m01 base]# vim lnmp.yml    # 结尾继续接上
    - name: Tar PHP Package
      unarchive:
        src: /root/php.tar.gz
        dest: /tmp/

    - name: Install PHP Server
      shell: "yum localinstall -y /tmp/*.rpm"

    - name: Config PHP Server
      copy:
        src: /root/www.conf
        dest: /etc/php-fpm.d/

    - name: Start PHP Server
      systemd:
        name: php-fpm
        state: started

4)第四部分:配置wordpress站点

1.准备工作

#1.上传wordpress包
[root@m01 ~]# rz wordpress-5.0.3-zh_CN.tar.gz

2.编写playbook

[root@m01 base]# vim lnmp.yml    # 结尾继续接上
    - name: Create Code Dir
      file:
        path: /code
        state: directory

    - name: Install Wordpress Code
      unarchive:
        src: /root/wordpress-5.0.3-zh_CN.tar.gz
        dest: /code/

    - name: Chown Code Dir
      file:
        path: /code
        state: directory
        owner: www
        group: www
        recurse: yes

 

5)第五部分:安装mariadb

- hosts: db01
  tasks:
    - name: Install Mariadb Server
      yum:
        name: mariadb-server
        state: present
        
    - name: Install MySQL-python Server        #ansible要控制mysql需要安装这个
      yum:
        name: MySQL-python
        state: present
        
    - name: Start Mariadb Server
      systemd:
        name: mariadb
        state: started
        enabled: yes

    - name: Create Database Wordpress
      mysql_db:
        name: wordpress
        state: present

    - name: Create Database Wordpress User
      mysql_user:
        name: "wp"
        host: "172.16.1.%"
        password: '123456'
        priv: 'wordpress.*:ALL'
        state: present

6)完成wordpress搭建,访问并配置数据库测试

 

3.迁移(扩展)

1)编写数据迁移

[root@m01 base]# cat sql.yml
- hosts: db01
  tasks:
    - name: Dump Database Wordpress
      mysql_db:        #把wordpress库导出到sql中
        state: dump
        name: wordpress
        target: /root/wordpress.sql

    - name: Get wordpress.sql
      fetch:    #把db01的sql拉到本地路径
        src: /root/wordpress.sql
        dest: /root/    #会在下面创建 主机名/文件路径  /root/db01/root/wordpress.sql

- hosts: db02
  tasks:
    - name: Post wordpress.sql
      copy:
        src: /root/db01/root/wordpress.sql
        dest: /tmp/

    - name: Install Mariadb Server    #下面同上创建数据库
      yum:
        name: mariadb-server
        state: present
        
    - name: Install MySQL-python Server        #ansible要控制mysql需要安装这个
      yum:
        name: MySQL-python
        state: present
        
    - name: Start Mariadb Server
      systemd:
        name: mariadb
        state: started
        enabled: yes

    - name: Create Database Wordpress
      mysql_db:
        name: wordpress
        state: present

    - name: Create Database Wordpress User
      mysql_user:
        name: "wp"
        host: "172.16.1.%"
        password: '123456'
        priv: 'wordpress.*:ALL'
        state: present
        
    - name: Import wordpress.sql    #导入sql数据
      mysql_db:
        state: import
        name: wordpress
        target: /tmp/wordpress.sql

2)一键迁移博客

- hosts: web01
  tasks:
    - name: Tgz Wordpress Code
    - archive:
      path: /code/wordpress
      dest: /root/wordpress.tgz
      
    - name: Get Wordpress Code
      fetch:
        src: /root/wordpress.tgz
        dest: /root/

- hosts: web02
  tasks:
    - name: Tar Wordpress Code
      unarchive: 
        src: /root/web01/root/wordpress.tgz
        dest: /

 

posted @ 2024-03-25 16:01  战斗小人  阅读(20)  评论(0编辑  收藏  举报