• 欢迎访问显哥博客,本网站纯属学习技术,绝无商业用途,欢迎小伙伴们共同学习!研究技术!QQ:52249909 加我QQ
  • 世界75亿人,这么小的概率,能认识你,是我一生的幸运,不妨加个QQ接触一下:52249909 加我QQ

Ansible自动化模块——playbook实战

Ansible lixian 5年前 (2020-03-30) 1999次浏览 0个评论 扫描二维码
文章目录[隐藏]

一、playbook概述

1.什么是playbook

PlayBook即”剧本”,”兵书”之意,PlayBook是由以下部分组成的

play(host): 定义的是主机的角色。(主角还是配角)
book(task): 定义的是具体执行的任务。(角色的台词和动作)
playbook: 由一个或多个play(角色)组成,一个play(角色)可以包含多个task(台词,动作)。

简单理解为: 使用不同的模块完成一件事情
在Ansible中”剧本文件”是以yml结尾的文件。
在SaltStack中”剧本文件”是以sls结尾的文件。

2.playbook组成

[root@m01 ~]# vim touch.yml
#定义要执行动作的主机
- hosts: web_group
#定义操作的用户
  remote_user: root
#定义变量
  vars:
    file_name: lhd
#动作
  tasks:
#注释
    - name: Touch File
#模块与实际动作
      shell: touch /tmp/{{ file_name }}
      
      
#执行前先验证语法(只能验证语法,无法验证逻辑)
[root@m01 ~]# ansible-playbook --syntax-check touch.yml

playbook: touch.yml

3.playbook与ad-hoc区别

特点 PlayBook ad-hoc
完整性
持久性
执行效率
变量 支持 不支持
耦合度

1.PlayBook功能比ad-hoc更全,是对ad-hoc的一种编排.
2.PlayBook能很好的控制先后执行顺序, 以及依赖关系.
3.PlayBook语法展现更加的直观.
4.playbook可以持久使用,ad-hoc无法持久使用.

二、playbook实战

1.基础准备

#1.安装anzible
[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='1'
web02 ansible_ssh_pass='1'

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

#4.上传nginx,WordPress和php源码包
[root@m01 ~]# rz nginx-1.16.1-1.el7.ngx.x86_64.rpm
[root@m01 ~]# rz php.tar.gz
[root@m01 ~]# rz wordpress-5.0.3-zh_CN.tar.gz


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

#6.配置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;
    }
}

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服务

- 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服务

    - 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站点

    - 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)第五部分:安装maridb

- hosts: db01
  tasks:
    - name: Install Mariadb Server
      yum:
        name: mariadb-server
        state: present

    - name: Install MySQL-python Server
      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

三.数据库迁移(扩展)

[root@m01 base]# cat sql.yml 
- hosts: db01
  tasks:
    - name: Dump Database WordPress
      mysql_db:
        state: dump
        name: wordpress
        target: /root/wordpress.sql

    - name: Get wordpress.sql
      fetch:
        src: /root/wordpress.sql
        dest: /root/

- 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
      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
      mysql_db:
        state: import
        name: wordpress
        target: /tmp/wordpress.sql

本站博主 , 版权所有丨如未注明 , 均为原创
转载请注明原文链接:Ansible自动化模块——playbook实战
喜欢 (0)

您必须 登录 才能发表评论!