ansible-playbook 实战案例 全网备份 实时备份

ansible-playbook 基础介绍

playbook是由一个或多个模块组成的,使用多个不同的模块,完成一件事情。
playbook通过yaml语法识别描述的状态文件。扩展名是yaml

1.YAML三板斧

  • 缩进
    • YAML使用一个固定的缩进风格表示层级结构,每个缩进由两个空格组成, 不能使用tabs
  • 冒号
    • 以冒号结尾的除外,其他所有冒号后面所有必须有空格。
  • 短横线
    • 表示列表项,使用一个短横杠加一个空格。
    • 多个项使用同样的缩进级别作为同一列表。
  • 安装httpd服务->playbook
    1.安装
    2.配置
    3.启动

2. ansible playbook 安装apache 示例

[root@m01 ansible_playbook]# vim  webserver.yaml
- hosts: web
  tasks:
    - name: Install Httpd Server
      yum: name=httpd,httpd-tools state=installed

    - name: Configgure Httpd Server
      copy: src=./file/httpd.conf dest=/etc/httpd/conf/httpd.conf
      notify: Resart Httpd Server

    - name: Start Httpd Server
      service: name=httpd state=started enabled=yes

  handlers:
    - name: Resart Httpd Server
      service: name=httpd state=restarted

案例 全网备份 实时备份

环境规划

角色外网IP(NAT)内网IP(LAN)部署软件
m01eth0:10.0.0.61eth1:172.16.1.61ansible
backupeth0:10.0.0.41eth1:172.16.1.41rsync
nfseth0:10.0.0.31eth1:172.16.1.31nfs、Sersync
web01eth0:10.0.0.7eth1:172.16.1.7httpd

目录规划

[root@m01 ansible_playbook]# pwd
/etc/ansible/ansible_playbook
[root@m01 ansible_playbook]# tree
.
├── base.yaml
├── conf
│   ├── confxml.xml
│   ├── exports
│   ├── resolv.conf
│   ├── rsyncd.conf
│   └── web.yaml
├── file
│   └── sersync2.5.4_64bit_binary_stable_final.tar.gz
├── mail.yaml
├── nfs.yaml
├── rsync.retry
├── rsync.yaml
├── scripts
│   ├── rsync_backup_md5.sh
│   └── rsync_check_backup.sh
└── sersync.yaml

3 directories, 14 files

base.yaml

[root@m01 ansible_playbook]# vim base.yaml
- hosts: all
  tasks:
    - name: clear yum.repos.d
      file: path=/etc/yum.repos.d/ state=absent 

    - name: Create yum.repos.d
      file: path=/etc/yum.repos.d/ state=directory 

    - name: Install Base Repos
      get_url: url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/CentOS-Base.repo 

    - name: Install Epel Repos
      get_url: url=http://mirrors.aliyun.com/repo/Centos-7.repo dest=/etc/yum.repos.d/epel.repo

    - name: Dns Client 
      copy: src=./conf/resolv.conf dest=/etc/rsolv.conf

    - name: Install Rsync Nfs-Utils
      yum: name=rsync,nfs-utils state=installed

    - name: Create Group WWW
      group: name=www gid=666
    
    - name: Create User WWW
      user: name=www uid=666 group=666 create_home=no shell=/sbin/nologin

    - name: Create Rsync_Client_Pass
      copy: content='1' dest=/etc/rsync.pass mode=600
  
    - name: Create Sripts Directory
      file: path=/server/scripts/ recurse=yes state=directory 

    - name: Push Scripts
      copy: src=./scripts/rsync_backup_md5.sh dest=/server/scripts/
    
    - name: Crontable Scripts
      cron: name="backup scripts" hour=01 minute=00 job="/usr/bin/bash /server/scripts/rsync_backup_md5.sh &>/dev/null"

rsync.yaml

[root@m01 ansible_playbook]# cat rsync.yaml
- hosts: backup
  tasks:

    - name: Installed Rsync Server
      yum: name=rsync,mailx state=installed

    - name: configure Rsync Server
      copy: src=/etc/ansible/ansible_playbook/conf/rsyncd.conf dest=/etc/rsyncd.conf
      notify: Restart Rsync Server

    - name: Create Virt User
      copy: content='rsync_backup:1' dest=/etc/rsync.password mode=600

    - name: Create Date
      file: path=/data state=directory recurse=yes owner=www group=www mode=755

    - name: Create Backup
      file: path=/backup state=directory recurse=yes owner=www group=www mode=755

    - name: Start RsyncServer
      service: name=rsyncd state=started enabled=yes


    - name: Push Check Scripts
      copy: src=./scripts/rsync_check_backup.sh dest=/server/scripts/

    - name: Crond Check Scripts
      cron: name="check scripts" hour=05 minute=00 job="/usr/bin/bash /server/scripts/rsync_check_backup.sh &>/dev/null"

  handlers:
    - name: Restart Rsync Server
      service: name=rsyncd state=restarted

nfs.yaml

[root@m01 ansible_playbook]# cat nfs.yaml
- hosts: nfs
  tasks:

    - name: Installed Nfs Server
      yum: name=nfs-utils state=installed

    - name: Configure Nfs Server
      copy: src=./conf/exports dest=/etc/exports
      notify: Restart Nfs Server

    - name: Create Share Data
      file: path=/data state=directory recurse=yes owner=www group=www mode=755

    - name: Start Nfs Server
      service: name=nfs-server state=started enabled=yes

  handlers:
    - name: Restart Nfs Server
      service: name=nfs-server state=restarted

sersync.yaml

[root@m01 ansible_playbook]# cat sersync.yaml
- hosts: nfs
  tasks:

    - name: Scp Sersync
      copy: src=./file/sersync2.5.4_64bit_binary_stable_final.tar.gz dest=/usr/local/sersync.tar.gz

    - name: Zip
      shell: cd /usr/local && tar xf sersync.tar.gz && mv GNU-Linux-x86 sersync
      args:
        creates: /usr/local/sersync

    - name: configure Sersync
      copy: src=./conf/confxml.xml dest=/usr/local/sersync/confxml.xml
      notify: kill old sersync and restart new sersync

    - name: Start Sersync
      shell: pgrep sersync;
             [ $? -eq 0 ] || /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml

  handlers:
    - name: kill old sersync and restart new sersync
      shell: pgrep sersync | xargs kill -9;
             /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml

web.yaml

[root@m01 ansible_playbook]# cat web.yaml
- hosts: web
  tasks:

    - name: Mount NFS Server Share Date
      mount: src=172.16.1.31:/data path=/data fstype=nfs opts=defaults state=mounted

    - name: Install Httpd Php
      yum: name=httpd,php state=installed

    - name: Configurl copy
      copy: src=./conf/httpd.conf dest=/etc/httpd/conf/httpd.conf
      notify: Restart Httpd

    - name: Unzip kaoshi.zip
      unarchive: src=./file/kaoshi.zip dest=/data/ creates=/data/index.html

    - name: Start Httpd
      service: name=httpd state=started enabled=yes

  handlers:
    - name: Restart Httpd
      service: name=httpd state=restarted

mail.yaml

[root@m01 ansible_playbook]# cat mail.yaml
- import_playbook: base.yaml
- import_playbook: rsync.yaml
- import_playbook: nfs.yaml
- import_playbook: sersync.yaml
- import_playbook: web.yaml

转载于:https://www.cnblogs.com/chengkanghua/p/9645043.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值