美文网首页
ansible脚本操作:在m01管理服务器上一键自动化完成rsy

ansible脚本操作:在m01管理服务器上一键自动化完成rsy

作者: 马帅琦 | 来源:发表于2019-04-25 17:31 被阅读0次

    首先下载ansible

    yum install ansible -y
    然后配置/etc/ansible/hosts文件
    [root@m01 /server/scripts]# cat /etc/ansible/hosts
    [oldboy]
    172.16.1.66
    [data]
    172.16.1.67
    172.16.1.68
    172.16.1.69
    在把 /etc/ansible/ansible.cfg 71的注释删除
    [root@m01 /server/scripts]# vim /etc/ansible/ansible.cfg +71
    host_key_checking = False
    

    先写个脚本rsync服务端的需求:

    [root@m01 /server/scripts]# cat rsync.sh 
    #!/bin/sh
    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
    hosts deny = 0.0.0.0/32
    auth users = rsync_backup
    secrets file = /etc/rsync.password
    [backup]
    comment = welcome to oldboyedu backup!
    path = /backup/
    EOF
    useradd rsync
    mkdir /backup
    chown -R rsync.rsync /backup/
    echo "rsync_backup:oldboy" > /etc/rsync.password
    chmod 600 /etc/rsync.password
    echo "export RSYNC_PASSWORD=rsync_backup:oldboy" >>/etc/bashrc
    source /etc/bashrc
    
    

    编写剧本rsync服务端剧本

    [root@m01 /server/scripts]# cat /etc/ansible/yaml/r1.yml
    - hosts: oldboy
      tasks:
        - name: rsync客户端操作
          script: /server/scripts/rsync.sh
    

    再写个脚本rsync客户端的需求:

    [root@m01 /server/scripts]# cat rsync1.sh 
    echo "oldboy" > /etc/rsync.password
    chmod 600 /etc/rsync.password
    echo ' export RSYNC_PASSWORD=oldboy' >>/etc/bashrc
    source /etc/bashrc
    

    编写剧本rsync客户端剧本

    [root@m01 /server/scripts]# cat /etc/ansible/yaml/r2.yml
    - hosts: data
      tasks:
        - name: rsync客户端
          script: /server/scripts/rsync1.sh
    

    在写脚本nfs服务端需求

    [root@m01 /server/scripts]# cat nfs.sh 
    #!/bin/sh
    yum install nfs-utils rpcbind -y
    systemctl start rpcbind &&\
    systemctl enable rpcbind &&\
    systemctl start nfs &&\
    systemctl enable nfs
    cat >/etc/exports <<EOF
    /data 172.16.1.0/24(rw,sync,all_squash,anonuid=3344,anongid=3344)
    EOF
    systemctl reload nfs
    useradd -u 3344 lvsha
    mkdir -p /data
    chown lvsha.lvsha /data
    

    编写剧本nfs服务端剧本

    [root@m01 /server/scripts]# cat /etc/ansible/yaml/n1.yml
    - hosts: 172.16.1.67
      tasks:
        - name: nfs服务端
          script: /server/scripts/nfs.sh
    

    在写脚本nfs客户端需求

    [root@m01 /server/scripts]# cat nfs1.sh 
    #!/bin/sh
    yum install nfs-utils rpcbind -y
    systemctl start rpcbind &&\
    systemctl enable rpcbind &&\
    systemctl start nfs &&\
    systemctl enable nfs
    useradd -u 3344 lvsha
    mount -t nfs 172.16.1.67:/data /mnt
    echo "mount -t nfs 172.16.1.67:/data /mnt" >>/etc/rc.local
    chmod +x /etc/rc.local
    

    编写剧本nfs客户端剧本

    [root@m01 /server/scripts]# cat /etc/ansible/yaml/n2.yml 
    - hosts: 172.16.1.68
      tasks:
        - name: nfs客户端
          script: /server/scripts/nfs1.sh
    - hosts: 172.16.1.69
      tasks:
        - name: nfs客户端
          script: /server/scripts/nfs1.sh
    

    sersync配置首先要在m01下把sersync这个文件下载好
    首先得下载配置文件

    [root@m01 /server/scripts]# cd /server/tools/
    [root@m01 /server/tools]# ls
    application  sersync_oldboy_64bit_20160928.tar.gz
    [root@m01 /server/tools]# 
    [root@m01 /server/tools]# cd application/
    [root@m01 /server/tools/application]# mkdir -p /application
    [root@m01 /server/tools/application]# mv sersync/ /application/
    

    在写脚本sersync客户端需求

    
    [root@m01 /server/scripts]# cat s1.sh 
    #!/bin/sh
    ansible data -m copy -a "src=/application/ dest=/application/" &&\
    ansible data -m file -a "path=/application/ recurse=on mode=+x" &&\
    ansible data -m shell -a "/application/sersync/bin/sersync -d -o /application/sersync/conf/confxml.xml &>/dev/null" 
    

    编写剧本sersync客户端剧本

    [root@m01 /server/scripts]# cat /etc/ansible/yaml/s1.yml 
    - hosts: data
      tasks:
        - name: 客户端sersync
          copy: src=/application/ dest=/application/
        - file: path=/application/ recurse=on mode=+x
        - shell: /application/sersync/bin/sersync -d -o /application/sersync/conf/confxml.xml &>/dev/null 
    
    

    首先优化m01的ssh把这些内容写进去

    [root@m01 /server/scripts]# cat /etc/ssh/sshd_config 
    ####Start by oldboy#2018-04-26###
    PermitEmptyPasswords no
    UseDNS no
    GSSAPIAuthentication no
    #ListenAddress 172.16.1.61:22  #将来配置VPN安全服务之后再加此行。
    ####End by oldboy#2018-04-26###
    

    最后执行大脚本

    [root@m01 /server/scripts]# cat 666.sh 
    #!/bin/sh
    #删除公钥
    [ ~/.ssh/id_rsa ]&& rm -fr ~/.ssh
    #创建密钥对
    ssh-keygen -f ~/.ssh/id_rsa  -P '' -q &&\
    #分发公钥和优化ssh
    for n in 172.16.1.{66..69}
    do
    sshpass -p123456 ssh-copy-id -f -i ~/.ssh/id_rsa.pub -o StrictHostKeyChecking=no $n
    ansible $n -m copy -a "src=/etc/ssh/sshd_config dest=/etc/ssh/sshd_config backup=yes"
    done &&\
    #rsync服务端
    ansible-playbook /etc/ansible/yaml/r1.yml
    #rsync客户端
    ansible-playbook /etc/ansible/yaml/r2.yml
    #nfs服务端
    ansible-playbook /etc/ansible/yaml/n1.yml
    #nfs客户端
    ansible-playbook /etc/ansible/yaml/n2.yml
    #sersync客户端
    ansible-playbook /etc/ansible/yaml/s1.yml
    

    相关文章

      网友评论

          本文标题:ansible脚本操作:在m01管理服务器上一键自动化完成rsy

          本文链接:https://www.haomeiwen.com/subject/yawzgqtx.html