美文网首页
Ansible Playbook 实战案例

Ansible Playbook 实战案例

作者: 祁恩达 | 来源:发表于2019-10-02 10:27 被阅读0次

    1.Playbook 实战案例

    image

    1.环境规划

    角色 外网IP(NAT) 内网IP(LAN) 部署软件
    m01 eth0:10.0.0.61 eth1:172.16.1.61 ansible
    backup eth0:10.0.0.41 eth1:172.16.1.41 rsync
    nfs eth0:10.0.0.31 eth1:172.16.1.31 nfs、Sersync
    web01 eth0:10.0.0.7 eth1:172.16.1.7 httpd

    2.配置ansible对应的主机

    [root@m01 ~]# vim /etc/ansible/hosts
    [web]
    172.16.1.7
    
    [nfs]
    172.16.1.31 
    
    [backup]
    172.16.1.41
    
    

    3.检查对应的主机组和规划的IP是否一致

    [root@m01 ~]# ansible web --list-host  
      hosts (1):
        172.16.1.7
    [root@m01 ~]# ansible backup --list-host
      hosts (1):
        172.16.1.41
    [root@m01 ~]# ansible nfs --list-host
      hosts (1):
        172.16.1.31
    [root@m01 ~]# ansible all --list-host
      hosts (3):
        172.16.1.31
        172.16.1.41
        172.16.1.7
    
    

    4.建立对应的目录站点,用于存放ansible-playbook文件

    [root@m01 ~]# mkdir -p /etc/ansible/ansible_playbook/file -p
    
    

    5.编写基础模块的palybook

    0.基础仓库准备
    1.安装rsync
    2.安装nfs-utils
    3.创建www用户指定uid、gid
    4.准备rsync客户端密码文件

    1.建立基础环境的yaml

    [root@m01 ansible_playbook]# cat base.yml 
    - hosts: all
      remote_user: root
    
      tasks:
    
        - name: configure yum repos
          yum_repository:
            name: base
            description: base yum repo
            baseurl: 
              - http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
              - http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
              - http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
            gpgcheck: no
    
        - name: configure yum repos
          yum_repository:
            name: epel
            description: epel yum repo
            baseurl: http://mirrors.aliyun.com/epel/7/$basearch
            gpgcheck: no
    
        - name: Create www Group
          group: name=www gid=666
    
        - name: Create www User
          user: name=www uid=666 group=666 shell=/sbin/nologin create_home=no
    
        - name: create rsync client pass
          copy: content='123456' dest=/etc/rsync.pass mode=0600
    
        - name: Push backup scripts
          copy: src=./files/clinet_push_rsync.sh dest=/server/scripts/
          when: (ansible_hostname != "backup")
    
        - name: Cron Tasks
          cron: name=Rsync_Backup minute=00 hour=01 job='/bin/bash /server/scripts/clinet_push_rsync.sh &>/dev/null'
          when: (ansible_hostname != "backup")
    
    

    2.使用ansible-playbook检测语法, 并进行模拟执行

    # 检测语法
    [root@m01 ansible_playbook]# ansible-playbook --syntax-check base.yaml
    playbook: base.yaml
    
    # 模拟执行
    [root@m01 ansible_playbook]# ansible-playbook -C  base.yaml 
    
    

    6.编写应用模块rsyncpalybook

    1.安装rsync
    2.配置rsync
    3.启动rsync
    4.准备对应数据存储仓库/backup /data 授权为www
    5.准备虚拟用户和密码文件,权限600
    6.变更配置,重载服务

    1.准备对应的配置文件存放至/etc/ansible/ansible_playbook/files/

    [root@m01 conf]# cat /etc/ansible/ansible_playbook/files/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]
    path = /backup
    
    [data]
    path = /data
    
    

    2.编写rsync安装的的yaml语法

    [root@m01 ansible_playbook]# cat rsync.yml 
    - hosts: backup
      remote_user: root
    
      tasks:
        - name: Install Rsync Server
          yum: name=rsync state=present
    
        - name: Config Rsync Server
          copy: src=./files/{{ item.src }} dest=/etc/{{ item.dest }}  mode={{ item.mode }}
          with_items:
            - { src: "rsyncd.conf", dest: "rsyncd.conf", mode: "0644" }
            - { src: "rsync.passwd", dest: "rsync.passwd", mode: "0600" }
          notify: 
            - Restart Rsync Server
          tags: conf_rsync
    
        - name: Create Directory 
          file: name={{ item }} state=directory owner=www group=www recurse=yes
          with_items:
            - /data
            - /backup
    
        - name: Server Rsync Server
          service: name=rsyncd state=started enabled=yes
    
        - name: Check Rsync Status
          shell: netstat -lntp|grep rsync
          register: Rsync_Status
    
        - name: Out Rsync Status
          debug: msg={{ Rsync_Status.stdout_lines }}
    
      handlers:
        - name: Restart Rsync Server
          service: name=rsyncd state=restarted
    
    

    7.编写应用模块nfspalybook

    1.安装nfs
    2.配置nfs
    3.启动nfs
    4.准备对应数据存储仓库/data授权为www
    5.变更配置,重载服务

    1.准备nfs配置文件exports

    [root@m01 ansible_playbook]# cat /etc/ansible/ansible_playbook/files/exports 
    {{ share_dir }} {{ share_ip }}(rw,sync,all_squash,anonuid=666,anongid=666)
    
    

    2.编写nfs安装与配置的yaml

    [root@m01 ansible_playbook]# cat /etc/ansible/ansible_playbook/nfs.yml
    - hosts: nfs
      remote_user: root
      vars:
        share_dir: /data
        share_ip: 172.16.1.0/24
      tasks:
        - name: Install NFS-Server
          yum: name=nfs-utils state=present
    
        - name: Configure NFS-Server
          template: src=./files/exports dest=/etc/exports 
          notify: Restart Nfs Server
    
        - name: Create Directory
          file: name={{ share_dir }} state=directory owner=www group=www recurse=yes
    
        - name: Start NFS-Server
          service: name=nfs state=started enabled=yes
    
        - name: Check Nfs Server
          shell: cat /var/lib/nfs/etab
          register: NFS_Status
    
        - name: Out Nfs Server
          debug: msg={{ NFS_Status.stdout_lines }} 
    
      handlers:
        - name: Restart Nfs Server
          service: name=nfs state=restarted
    
    

    8.编写应用模块sersyncpalybook

    1.安装sersync
    2.配置sersync
    3.启动sersync

    1.下载Sersync软件包

    [root@m01 ansible_playbook]# ll /etc/ansible/ansible_playbook/file/
    -rw-r--r-- 1 root root 727290 Aug  1 12:04 sersync.tar.gz
    
    

    2.准备sersync实时同步的配置文件

    [root@m01 ansible_playbook]# cat /etc/ansible/ansible_playbook/conf/confxml.xml.nfs 
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <head version="2.5">
        <host hostip="localhost" port="8008"></host>
        <debug start="false"/>
        <fileSystem xfs="true"/>
        <filter start="false">
        <exclude expression="(.*)\.svn"></exclude>
        <exclude expression="(.*)\.gz"></exclude>
        <exclude expression="^info/*"></exclude>
        <exclude expression="^static/*"></exclude>
        </filter>
        <inotify>
        <delete start="true"/>
        <createFolder start="true"/>
        <createFile start="true"/>
        <closeWrite start="true"/>
        <moveFrom start="true"/>
        <moveTo start="true"/>
        <attrib start="false"/>
        <modify start="false"/>
        </inotify>
    
        <sersync>
        <localpath watch="/data">
            <remote ip="172.16.1.41" name="data"/>
        </localpath>
    
        <rsync>
            <commonParams params="-az"/>
            <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.pass"/>
            <userDefinedPort start="false" port="874"/><!-- port=874 -->
            <timeout start="true" time="100"/><!-- timeout=100 -->
            <ssh start="false"/>
        </rsync>
        <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins execute once-->
        <crontab start="false" schedule="600"><!--600mins-->
            <crontabfilter start="false">
            <exclude expression="*.php"></exclude>
            <exclude expression="info/*"></exclude>
            </crontabfilter>
        </crontab>
        <plugin start="false" name="command"/>
        </sersync>
    
        <plugin name="command">
        <param prefix="/bin/sh" suffix="" ignoreError="true"/>  <!--prefix /opt/tongbu/mmm.sh suffix-->
        <filter start="false">
            <include expression="(.*)\.php"/>
            <include expression="(.*)\.sh"/>
        </filter>
        </plugin>
    
        <plugin name="socket">
        <localpath watch="/opt/tongbu">
            <deshost ip="192.168.138.20" port="8009"/>
        </localpath>
        </plugin>
        <plugin name="refreshCDN">
        <localpath watch="/data0/htdocs/cms.xoyo.com/site/">
            <cdninfo domainname="ccms.chinacache.com" port="80" username="xxxx" passwd="xxxx"/>
            <sendurl base="http://pic.xoyo.com/cms"/>
            <regexurl regex="false" match="cms.xoyo.com/site([/a-zA-Z0-9]*).xoyo.com/images"/>
        </localpath>
        </plugin>
    </head>
    
    

    3.编写sersync应用的yaml

    [root@m01 ansible_playbook]# cat sersync.yaml 
    - hosts: nfs
      tasks:
    
        - name: Installed Sersync
          copy: src=./file/sersync.tar.gz dest=/server/tools/
    
        - name: Tar xf Sersync
          shell: cd /server/tools/ && tar xf sersync.tar.gz && mv GNU-Linux-x86 /usr/local/sersync
          args:
            creates: /usr/local/sersync
    
        - name: Config Sersync
          copy: src=./conf/confxml.xml.nfs dest=/usr/local/sersync/confxml.xml
    
        - name: Service Start Sersync
          shell: /usr/local/sersync/sersync2 -dro /usr/local/sersync/confxml.xml
    
    

    9.编写web应用模块的palybook

    [root@m01 ansible_playbook]# cat web.yaml 
    [root@m01 ansible_playbook]# cat web.yml 
    - hosts: web
      remote_user: root
      vars:
        remote_nfs_ip: 172.16.1.31
        local_dir: /var/www/html/
        http_port: 80
    
      tasks:
        - name: Installed Httpd Server
          yum: name=httpd,php state=present
    
        - name: Configure Httpd Server
          template: src=./files/httpd.conf dest=/etc/httpd/conf/httpd.conf
          notify: Restart Httpd Server
    
        - name: Start Httpd Server
          service: name=httpd state=started enabled=yes
    
        - name: Mount Nfs Server 
          mount: src={{remote_nfs_ip}}:/data path={{ local_dir }} fstype=nfs opts=defaults state=mounted
    
        - name: Push kaoshi.zip 
          unarchive: src=./files/kaoshi.zip dest={{ local_dir }}
    
      handlers:
        - name: Restart Httpd Server
          service: name=httpd state=restarted
    
    #httpd配置文件中引入变量的使用,所以在yml文件中需要提前定义
    [root@m01 ansible_playbook]# cat /etc/ansible/ansible_playbook/files/httpd.conf |grep "^Listen"
    Listen {{ http_port }}
    
    

    10.将所有编写好的yaml引入至一个文件中, 这样便于一次执行

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

    11.测试

    1.先测试web是否能同步数据至nfs存储
    2.nfs是否实时同步至rsync的/data
    3.使用客户端测试能否推送数据至rsync的backup

    相关文章

      网友评论

          本文标题:Ansible Playbook 实战案例

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