第四节:Ansible剧本实战

Ansible playbook 实战

一、基础准备

#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 
[lb_server]
lb01 ansible_ssh_pass='1'
lb02 ansible_ssh_pass='1'

[web_group]
web01 ansible_ssh_pass='1'
web02 ansible_ssh_pass='1'

[nfs_server]
nfs ansible_ssh_pass='1'

[rsync_server]
backup ansible_ssh_pass='1'

[db_server]
db01 ansible_ssh_pass='1'

[nginx:children]
web_group
lb_server

#4.配置hosts
[root@m01 ~]# vim /etc/hosts
10.0.0.4 lb01
10.0.0.5 lb02
10.0.0.7 web01
10.0.0.8 web02
10.0.0.31 nfs
10.0.0.41 backup
10.0.0.51 db01

#5.创建统一目录
[root@m01 ~]# mkdir /project
[root@m01 ~]# cd /project/

二、编写剧本实例

1.第一部分:所有服务器优化

[root@m01 project]# cat base.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
        shell: /sbin/nologin
        create_home: false
        state: present

2.第二部分:nginx部分

1)安装方式

#源码包安装方式
1.上传包
2.解压
unarchive
3.生成
shell
4.编译
shell
5.安装
shell

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

#rpm包的方式
1.上传包
2.推送包
copy
3.安装包
yum

2)准备工作

#1.准备nginx的rpm包
[root@m01 project]# mkdir package
[root@m01 project]# cd package/
[root@m01 package]# rz
[root@m01 package]# ll
total 768
-rw-r--r-- 1 root root 784272 Dec 10 09:13 nginx-1.16.1-1.el7.ngx.x86_64.rpm

#2.准备nginx配置文件
[root@m01 package]# vim /etc/nginx/nginx.conf 
user  www;

3)编写剧本

[root@m01 project]# cat nginx.yml 
- hosts: nginx
  tasks:
    - name: Push nginx rpm
      copy:
        src: /project/package/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: /etc/nginx/nginx.conf
        dest: /etc/nginx/

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

3.第三部分:PHP部分

1)准备工作

#1.准备php的安装包
[root@m01 package]# rz
[root@m01 package]# ll
total 20192
-rw-r--r-- 1 root root   784272 Dec 10 09:13 nginx-1.16.1-1.el7.ngx.x86_64.rpm
-rw-r--r-- 1 root root 19889622 Nov 22 15:52 php.tar.gz

#2.准备配置文件
[root@m01 project]# mkdir conf
[root@m01 project]# mv /etc/php.ini conf/
[root@m01 project]# cp /etc/php-fpm.d/www.conf conf/
[root@m01 project]# vim conf/php.ini
upload_max_filesize = 200M
post_max_size = 300M
[root@m01 project]# vim conf/www.conf 
user = www
group = www

2)编写剧本

[root@m01 project]# vim php.yml
- hosts: web_group
  tasks:
    - name: Tar php Package
      unarchive:
        src: /project/package/php.tar.gz
        dest: /tmp/

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

    - name: Config php Server
      copy:
        src: /project/conf/php.ini
        dest: /etc/

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

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

4.第四部分:配置wordpress网站

1)准备工作

#1.准备wordpress包
[root@m01 ~]# cd /project/package/
[root@m01 package]# rz
[root@m01 package]# ll
total 31032
-rw-r--r-- 1 root root   784272 Dec 10 09:13 nginx-1.16.1-1.el7.ngx.x86_64.rpm
-rw-r--r-- 1 root root 19889622 Nov 22 15:52 php.tar.gz
-rw-r--r-- 1 root root 11098483 Sep 12 17:52 wordpress-5.0.3-zh_CN.tar.gz

#2.准备wordpress配置文件
[root@m01 project]# vim conf/linux.wp.com.conf
server {
    listen 80;
    server_name linux.wp.com;
    root /code/wordpress;
    index index.php;

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

2)编写剧本

[root@m01 project]# cat wordpress.yml 
- hosts: web_group
  tasks:
    - name: Create code Dir
      file:
        path: /code
        state: directory

    - name: Tar wordpress Code
      unarchive:
        src: /project/package/wordpress-5.0.3-zh_CN.tar.gz
        dest: /code/

    - name: Config wordpress DB
      copy:
        src: /project/conf/wp-config.php
        dest: /code/wordpress

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

    - name: Config Nginx wordpress
      copy:
        src: /project/conf/linux.wp.com.conf
        dest: /etc/nginx/conf.d/

    - name: Restart Nginx Server
      systemd:
        name: nginx
        state: restarted

5.第五部分:mariadb部分

联系管理员微信tutu19192010,注册账号

上一篇
下一篇
Copyright © 2022 Egon的技术星球 egonlin.com 版权所有 帮助IT小伙伴学到真正的技术