[svc]ansibleバックアップ実現メール遠位解凍

3634 ワード

後続は簡単から複雑、文法から応用例、大通りから簡単の原則に基づいてansibleをやり続けます.
1.遠位解凍リモートの圧縮パッケージをリモートのディレクトリに解凍
- hosts: nodexx
  remote_user: root
  vars:
  - dfpath: "/backup/scm-data/"
  tasks:
    - name "this vm restore scm data to test data on app"
      unarchive:
        src: /tmp/etc.tar.gz  # url, 
        dest: /root
        remote_src: "{{ dfpath }}"

2、遠位端圧縮、取り戻し
- hosts: nodexx
    remote_user: root
    vars:
      - sfpath: "/backup/scm-data/*_$(date +%F)_scmdata.tar.gz"
      - dfpath: "/backup/scm-data/"
    - name: Package | make /root/.scm to tar.gz package on node14
      raw: cd /root && tar zcf /data/backup/scm-data/`ifconfig|sed -n '2p'|awk -F':' '{print $2}'|awk '{print $1}'`_$(date +%F)_scmdata.tar.gz .scm


    - name: Download | copy nodexx's tar.gz to ansible vm([this vm] /data/scm-data)
      synchronize:
        src: "{{ sfpath }}"
        dest: "{{ dfpath }}"
        mode: pull
#cd/&&echo 1>1のような実行コマンドを使用することに注意してください.txt,shellは使いにくいのでrawモジュールを使う必要があります
#変数付き定義
3、mailモジュールを利用してメールを送る
- hosts: nodexx
    remote_user: root
    - name: sendMail to op
      mail:
        host: smtp.sina.com
        port: 25
        username: [email protected]
        password: xxx
        from: [email protected] (lannymxl)
        to: Jack 
        # cc: John Doe , Suzie Something 
        # cc: Wang Wu , Li Si 
        attach: /etc/fstab /etc/hosts
        subject: Backup-scm successfully
        body: 'System {{ ansible_hostname }}-192.168.x.x from 192.168.x.x scm-backup has been successfully provisioned.'

4,scmホストから毎日バックアップして、バックアップが終わった後にメールを送る--ansible実現
- hosts: nodexx-scm
  remote_user: root
  vars:
  - sfpath: "/backup/scm-data/*_$(date +%F)_scmdata.tar.gz"
  - dfpath: "/backup/scm-data/"
  tasks:# , scm-data.tar.gz  2, 
    - name: Clean | keeping [scm-server-node14]'s /backup/scm-data dir only have one tar pkg
      shell: find /backup/scm-data/ -name "*.tar.gz"  -type f -mtime|xargs rm -f


    - name: Package | make /root/.scm to tar.gz package on node14
      raw: cd /root && tar zcf /data/backup/scm-data/`ifconfig|sed -n '2p'|awk -F':' '{print $2}'|awk '{print $1}'`_$(date +%F)_scmdata.tar.gz .scm


    - name: Download | copy node14's tar.gz to ansible vm([this vm] /data/scm-data)
      synchronize:
        src: "{{ sfpath }}"
        dest: "{{ dfpath }}"
        mode: pull
    
    #  
    # - name "this vm restore scm data to test data on app"
    #   unarchive:
    #     src: "{{ dfpath }}"
    #     dest: /root
    #     remote_src: True
- hosts: localhost
  remote_user: root
  tasks: 
  # scm , 7 .  , .
    - name: Keeps scm-data.tar.gz of 7 days in local
      shell: find /backup/scm-data/ -name "*.tar.gz"  -type f -mtime|xargs rm -f


    - name: sendMail to op
      mail:
        host: smtp.sina.com
        port: 25
        username: [email protected]
        password: xxx
        from: [email protected] (lannymxl)
        to: Jack 
        # cc: John Doe , Suzie Something 
        # cc: Wang Wu , Li Si 
        attach: /etc/fstab /etc/hosts
        subject: Backup-scm successfully
        body: 'System {{ ansible_hostname }}-192.168.x.x from 192.168.x.x scm-backup has been successfully provisioned.'