ansible 的playbook脚本

1、

    #vim /etc/ansible/yaml/back.yml
    - hosts: siyi
      tasks:
         - name: "yum rsync"
           yum: name=rsync state=installed
         - name: "配置rsync模块"
           copy: backup=yes src=/etc/rsyncd.conf dest=/etc/rsyncd.conf
         - name: "创建rsync用户"
           user: name=rsync
         - name: "创建backup目录"
           file: name=/backup state=directory owner=rsync group=rsync
         - name: "创建/server/tools/目录"
           file: name=/server/tools/ state=directory owner=root group=root
         - name: "开启rsync服务"
           service: name=rsyncd state=started enabled=yes
         - name: "创建密码文件"
           file:  name=/etc/rsync.password state=touch mode=600
         - name: "添加密码"
           blockinfile: path=/etc/rsync.password block='rsync_backup:oldboy'
    - hosts: sanyi
      tasks:
         - name: "yum rsync"
           yum: name=rsync  state=installed
         - name: "创建data目录"
           file: name=/data state=directory
         - name: "创建密码文件"
           file:  name=/etc/rsync.password state=touch mode=600
         - name: "添加密码"
           blockinfile: path=/etc/rsync.password block='oldboy'
         - name: "添加bashrc"
           blockinfile: path=/etc/bashrc block='export RSYNC_PASSWORD=oldboy'
         - name: "执行bashrc"
           shell: source /etc/bashrc
         - name: "推送测试"
           command: rsync -az /tmp rsync_backup@172.16.1.41::backup/
         - name: "解压监控包"
           command: tar -xf /server/tools/application.tar.gz -C /server/tools/
         - name: "创建日志目录"
           file: name=/application/logs/ state=directory 
         - name: "创建日志文件"
           file:  name=/application/logs/rsync_fail_log.sh state=touch
         - name: "执行监控"
           command: /server/tools/application/bin/sersync2 -d -o /server/tools/application/conf/confxml.xml
         - name: "yum nfs"
           yum: name=nfs-utils state=installed
         - name: "yum rpcbind"
           yum: name=rpcbind state=installed
         - name: "开启rpcbind服务"
           service: name=rpcbind state=started enabled=yes
         - name: "开启nfs服务"
           service: name=nfs state=started enabled=yes
         - name: "添加共享目录"
           blockinfile: path=/etc/exports block='/data 172.16.1.0/24(rw,sync)'
         - name: "重启nfs服务"
           service: name=nfs state=restarted
         - name: "修改data属主"
           file:  name=/data owner=nfsnobody group=nfsnobody
    - hosts: oldboy
      tasks:
         - name: "yum nfs-utils rpcbind"
           yum: name=nfs-utils state=installed
         - name: "yum nfs-utils rpcbind"
           yum: name=rpcbind state=installed
         - name: "开启rpcbind服务"
           service: name=rpcbind state=started enabled=yes
         - name: "挂载"
           mount: src='172.16.1.31:/data' state=mounted fstype=nfs name=/mnt 
         - name: "添加执行权限"
           file:  name=/etc/rc.local  mode=755
         - name: "添加密码"
           blockinfile: path=/etc/rc.local block='/usr/bin/mount -t nfs 172.16.1.31:/data /mnt'

4、m01的/server/scripts/目录下

        # vim /server/scripts/mokuai.sh
        #!/bin/bash
        cd /root/.ssh/ &&\
        rm -f * &&\
        /bin/ssh-keygen -t rsa -f /root/.ssh/id_rsa  -P "" &>/dev/null
        yum install -y sshpass >/dev/null 2>&1
        for n in  7 8  31 41
        do 
        sshpass -p123456 ssh-copy-id -i ~/.ssh/id_rsa.pub 172.16.1.$n -o StrictHostKeyChecking=no &>/dev/null
          if [ $? -eq 0 ]
          then
            echo "172.16.1.$n Distribute successfully "
              else
            echo "172.16.1.$n Distribute no successfully "
          fi
         done &&\
     scp -P 22 -rp /server/tools/application.tar.gz root@172.16.1.31:/server/tools/ &>/dev/null &&\
    cp /etc/ansible/hosts{,.ori} &&\
    cat >/etc/ansible/hosts<<EOF
    [siyi]
    172.16.1.41
    
    [sanyi]
    172.16.1.31
    
    [oldboy]
    172.16.1.7
    172.16.1.8
    EOF
    cp /etc/rsyncd.conf{,.ori} &&\
    cat >/etc/rsyncd.conf<<EOF
    #rsync_config_______________start
    #created by oldboy
    #site: http://www.oldboyedu.com
    uid = rsync
    gid = rsync
    use chroot = no
    fake super = yes
    max connections = 200
    timeout = 600 
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    log file = /var/log/rsyncd.log
    ignore errors
    read only = false
    list = false
    hosts allow = 172.16.1.0/24
    auth users = rsync_backup
    secrets file = /etc/rsync.password
    [backup]
    comment = welcome to oldboyedu backup!
    path = /backup/
    EOF
    sed -i '71s/#//' /etc/ansible/ansible.cfg &&\
    ansible-playbook  /etc/ansible/yaml/back.yml
posted @ 2019-04-24 19:34  小疯紫  阅读(634)  评论(0编辑  收藏  举报