ansible剧本初使用-day03--搭建nfs和rsync

ansible剧本初使用--搭建rsync和nfs

ansible介绍

  • 啥是anisble的剧本
PlayBook即"剧本","兵书"之意,PlayBook是由以下部分组成的

play: 定义的是主机的角色。(主角还是配角)
task: 定义的是具体执行的任务。(角色的台词和动作)
playbook: 由一个或多个play(角色)组成,一个play(角色)可以包含多个task(台词,动作)。
简单理解为:使用不同模块完成一件事情
  • ansible剧本和saltstack剧本文件区别
在Ansible中"剧本文件"是以yml结尾的文件。
在SaltStack中"剧本文件"是以sls结尾的文件。
但是语法,使用的都是yaml语法

  • playbook和ad-hoc的区别

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


ansible剧本语法规则

语法 描述
缩进 YAML使用固定的缩进风格表示层级结构,每个缩进由两个空格组成, 不能使用TAB
冒号 以冒号结尾的除外,其他所有冒号后面所有必须有空格
短横线 表示列表项,使用一个短横杠加一个空格,多个项使用同样的缩进级别作为同一列表

host:对哪些主机进行操作(演员)
remote_user:使用什么用户执行(通行证)
tasks:具体执行任务(台词和动作)

  • 示例
[root@m01 ~]# cat foo.yml				# 必须以yml或者yaml结尾
---
- hosts: all						   # 针对主机清单中所有主机
  remote_user: root					   
  vars:
    file_name: zls.txt       
  tasks:							   # 指定使用模块
    - name: Create New File				# 模块指定语法①
      file: name=/tmp/{{ file_name }} state=touch	# 模块指定语法②

案例操作--安装rsync及nfs

要求

1.web01和web02安装nginx
2.自己写一个前端页面(xxx_web01_page)
3.安装nfs
4.web01和web02随便挂载目录到nfs
5.nfs将共享目录的数据,推送到backup
6.rsync

环境准备

主机名 ip 角色
m01 10.0.0.61 ansible管理端
web01 10.0.0.7 nginx
web02 10.0.0.8 nginx
nfs 10.0.0.31 nfs共享存储
rsync 10.0.0.41 备份数据

操作流程

1.前期准备

# 具体流程:
----前期ansible准备
· 本地准备nginx官方源
· 书写nginx配置文件
· 书写rsync配置文件
· 脚本准备

----安全配置
0. 统一用户www
1. 防火墙关闭
2. 防火墙开通nfs服务,80和873端口
----rsync
3. 安装rsync
4. 将配置文件cp到rsync服务端
5. 书写密码文件并修改权限为600
6. 创建备份目录,修改属主属组信息
-----nfs
7. 安装nfs
8. 书写nfs配置文件
9. 创建共享目录,修改权限,
10. 启动nfs服务
11. 将脚本cp到nfs中
12.书写定时任务
13.启动nfs服务,并且书写连接rsync密码文件

-----nginx操作
14. 将nginx官方源cp到web两台主机
15. 安装nginx
16. 将nginx配置文件cp到web两台主机,书写nginx前段页面
17. 启动nginx
18. 挂载两台web到nfs

2.主机清单

---------ssh秘钥对创建及公钥分发这里不做配置
# 1.书写主机清单
[root@m01 ~]# vim /etc/ansible/hosts 

[web_group]
web01 ansible_ssh_host=10.0.0.7
web02 ansible_ssh_host=10.0.0.8

[nfs_group]
nfs ansible_ssh_host=10.0.0.31

[backup_group]
backup ansible_ssh_host=10.0.0.41

[nfs_install]
web_group
nfs_group

[backup_install]
backup_group
nfs_group


3.ansible准备文件

# 1.创建ansible目录
[root@m01 ~]# mkdir /ansible

# 2.创建nfs,rsync,nginx文件
[root@m01 ansible]# mkdir script rsync nginx/

# 3.准备nginx官方源文件
[root@m01 ansible]# vim /ansible/nginx/nginx.repo 

[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true

# 4.书写nginx配置文件
[root@m01 nginx]# cat web01.conf 
server {
	listen 80;
	server_name www.web01.com;
	root /tmp;
	index index.html;

}
[root@m01 nginx]# cat web02.conf 
server {
	listen 80;
	server_name www.web02.com;
	root /tmp;
	index index.html;

}

# 5.书写rsync配置文件
[root@m01 rsync]# vim rsyncd.conf
uid = www
gid = www
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup
secrets file = /etc/rsync.passwd
log file = /var/log/rsyncd.log
#####################################
[backup]
comment = welcome to oldboyedu backup!
path = /backup

# 6.准备脚本
[root@nfs backup]# vim /root/backup.sh 
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
Host=$(hostname)
Addr=$(ifconfig eth1|awk 'NR==2{print $2}')
Date=$(date +%F)
Dest=${Host}_${Addr}_${Date}
Path=/backup

#2.创建备份目录
[ -d $Path/$Dest ] || mkdir -p $Path/$Dest

#3.备份对应的文件
cd / && 
[ -f $Path/$Dest/system.tar.gz ] || tar czf $Path/$Dest/system.tar.gz etc/fstab etc/rsyncd.conf && 
[ -f $Path/$Dest/log.tar.gz ] || tar czf $Path/$Dest/log.tar.gz  var/log/messages var/log/secure && 

#4.携带md5验证信息
[ -f $Path/$Dest/flag ] || md5sum $Path/$Dest/*.tar.gz >$Path/$Dest/flag_$Date

#5.推送本地数据至备份服务器
export RSYNC_PASSWORD=123
rsync -avz $Path/ rsync_backup@10.0.0.41::backup

#6.本地保留最近7天的数据
find $Path/ -type d -mtime +7|xargs rm -rf
~                                                     

4.ansible剧本书写

[root@m01 ansible]# cat playbook.yml 
- hosts: all
  tasks:
    - name: create group www
      group:
        name: www
        gid: 666
        state: present

    - name: create user www
      user:
        name: www
        uid: 666
        group: www
        state: present
        shell: /sbin/nologin
        create_home: false

- hosts: backup_install
  tasks:
    - name: yum install rsync
      yum:
        name: rsync
        state: present

- hosts: backup_group
  tasks:
    - name: scp rsync config
      copy:
        src: /ansible/rsync/rsyncd.conf
        dest: /etc/rsyncd.conf

    - name: create mima file
      copy:
        content: rsync_backup:123
        dest: /etc/rsync.passwd
        mode: 0600

    - name: create backup directory
      file:
        path: /backup
        owner: www
        group: www
        state: directory

    - name: start rsync server
      service:
        name: rsyncd
        state: restarted
       
- hosts: nfs_group 
  tasks:
    - name: yum install nfs
      yum:
        name: nfs-utils
        state: present

    - name: create nfs config
      copy:
        content: '/data 10.0.0.0/24(rw,sync,all_squash,anonuid=666,anongid=666)'
        dest: /etc/exports

    - name: create nfs directory
      file:
        path: /data
        owner: www
        group: www
        state: directory

    - name: start nfs server
      service:
        name: nfs-server
        state: restarted

    - name: cp nfs script to nfs
      copy:
        src: /ansible/script/backup.sh
        dest: /root/

    - name: crontab file
      cron:
        name: "backup web data"
        job: "/bin/sh /root/backup.sh"

    - name: rsync to nfs
      copy:
        content: 123
        dest: /etc/rsync.passwd

- hosts: web_group
  tasks:
    - name: cp nginx.repo to web
      copy:
        src: /ansible/nginx/nginx.repo
        dest: /etc/yum.repos.d/

    - name: yum install nginx
      yum:
        name: nginx
        state: present

- hosts: web01
  tasks:
    - name: cp to web01
      copy:
        src: /ansible/nginx/web01.conf
        dest: /etc/nginx/conf.d/

- hosts: web02
  tasks:
    - name: cp to web02
      copy:
        src: /ansible/nginx/web02.conf
        dest: /etc/nginx/conf.d/

- hosts: web_group
  tasks:
    - name: restart nginx
      service:
        name: nginx
        state: restarted

    - name: mount
      mount:
        path: /mnt
        src: '10.0.0.31:/data'
        fstype: nfs
        state: mounted

- hosts: all
  tasks:
    - name: start firewalld nfs
      service:
        name: firewalld
        state: restarted

    - name: open nfs
      firewalld:
        service: nfs
        permanent: no
        state: enabled

    - name: open rsync
      firewalld:
        port: 873/tcp
        permanent: no
        state: enabled

    - name: open http
      firewalld:
        service: http
        permanent: no
        state: enabled

5.验证

  • nfs实现完成

1591790002039

  • rsync实现完成推送

1591790825858

原文地址:https://www.cnblogs.com/tcy1/p/13089394.html