ansible应用

前言:

假如让你在一组服务器安装某个软件,服务器少的话还可以接受,但如果有上百台服务器的话,这样会耗费大量时间,在这时候Ansible就由此而生;总之Ansible提供的很多模块十分强大。

一、关于ansible

  1、ansible是什么

    (1)https://www.w3cschool.cn/automate_with_ansible/automate_with_ansible-atvo27or.html

  2、ansible环境部署

    (2)https://www.w3cschool.cn/automate_with_ansible/automate_with_ansible-1khc27p1.html

    (3)https://www.cnblogs.com/gzxbkk/p/7515634.html

  3、ansible安装

    (1)http://www.linuxops.cc/2018/06/05/Ansible%E5%AE%89%E8%A3%85%E3%80%81%E9%85%8D%E7%BD%AE%E4%B8%8E%E5%B8%B8%E7%94%A8%E6%A8%A1%E5%9D%97%E4%BB%8B%E7%BB%8D/

  4、其他的一些系列介绍可以参考如下,重点是ansible所包含的模块信息,具体我个人感觉不用都记住,用到的时候去查看相关模块即可:

  (1)http://www.cnblogs.com/f-ck-need-u/p/7576137.html#auto_id_2

  (2)http://www.zsythink.net/    中的运维技术->ansible

  (3)http://www.ansible.com.cn/index.html  中文档

  (4)https://docs.ansible.com/ansible/latest/user_guide/playbooks.html   ansible playbooks官方指南

二、实战实例

  1、ansible批量安装jdk(yml文件)

- hosts: clickhouse_cluster_setup_beijing #hosts 定义单个主机或组
  remote_user: root #以root账户执行
  tasks:
    - name: copy jdk remote hosts
      copy: src=/root/usr/jdk-8u201-linux-x64.tar.gz dest=/usr/local/ backup=yes
    - name: tar jdk
      shell: chdir=/usr/local/ tar -xzvf jdk-8u201-linux-x64.tar.gz
    - name: create links
      file: src=/usr/local/jdk1.8.0_201 dest=/usr/local/java state=link
    - name: java_profile config
      shell: /bin/echo {{ item }} >> /etc/profile
      with_items:
        - export JAVA_HOME=/usr/local/java
        - export JRE_HOME=/usr/local/java/jre
        - export CLASSPATH=.:\$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:\$JRE_HOME/lib:\$CLASSPATH
        - export PATH=\$JAVA_HOME/bin:\$PATH
    - name: take effect
      shell: source /etc/profile

这里注意的是hosts后面的clickhouse_cluster_setup_beijing,当我们在主控机器上安装好ansible后,需要生成的/etc/ansible/hosts文件里面配置自己需要安装的机器IP,例如:

clickhouse_cluster_setup_beijing对应的有五台机器:

  1、ansible批量安装clickhouse

  这里有一个稍微的难点是使用template模块根据自己配置的jinja2模版去加载配置到所有的机器上面去,关于jinja2的介绍可以参见http://docs.jinkan.org/docs/jinja2/、http://www.zsythink.net/archives/3021,另外这里要知道clickhouse的相关部署知识,才可以看懂为什么这么来做。

config.xml.j2模版文件:

"1.0"?>


    
        
        trace
        /data/clickhouse/logs/server.log
        /data/clickhouse/logs/error.log
        1000M
        10
         
    
     
    8123
    9000

    
    

    
    
         
            
            /etc/clickhouse-server/server.crt
            /etc/clickhouse-server/server.key
            
            /etc/clickhouse-server/dhparam.pem
            none
            true
            true
            sslv2,sslv3
            true
        

         
            true
            true
            sslv2,sslv3
            true
            
            
                
                RejectCertificateHandler
            
        
    

    
    

    
    9009

    
    

    
    
    
    0.0.0.0

    
    
    
    

    
    

    

    4096
    3

    
    100

    
    

    
    8589934592

    
    5368709120


    
    /data/clickhouse/

    
    /data/clickhouse/tmp/

    
    /data/clickhouse/user_files/

    
    users.xml

    
    default

    
    

    
    default

    
    

    
    

    
    false

    
    "clickhouse_remote_servers" />

    
    /etc/clickhouse-server/metrika.xml

    
    "zookeeper-servers" optional="true" />

    
    "macros" optional="true" />


    
    3600


    
    3600

    
    60

    
    
    


    
    
        
        system
        query_log
toYYYYMM(event_date) 7500
system query_thread_log
toYYYYMM(event_date) 7500
*_dictionary.xml "clickhouse_compression"> /clickhouse/task_queue/ddl click_cost any 0 3600 86400 60 <default> max 0 60 3600 300 86400 3600 default> /data/clickhouse/format_schemas/

users.xml.j2模版文件:

"1.0"?>

    
    
        
        <default>
            
            10000000000

            
            0

            
            random
            
            1
        default>

        
        <readonly>
            10000000000
            0
            random
            <readonly>1readonly>
        readonly>
    

    
    
        
        <default>
            
            

            
            "networks" replace="replace">
                ::/0
            

            
            default

            
            default
        default>

        
        <readonly>
            
            "networks" replace="replace">
                ::1
                127.0.0.1
            
            readonly
            default
        readonly>
    

    
    
        
        <default>
            
            
                
                3600

                
                0
                0
                0
                0
                0
            
        default>
    

metrika.xml.j2模版文件:


    
        
            {for i in range(2,7,1) %}
            {% if i<10 %}
            
                true
                
                    {{shard_host_pre}}0{{i}}
                    {{shard_port}}
                    {{shard_user}}
                
            
            {else %}
            
                true
                
                    {{shard_host_pre}}{{i}}
                    {{shard_port}}
                    {{shard_user}}
            
            
            {%endif%}
            {% endfor %}
        
    

    
            {for i in range(1,6,1) %}
            {% if i<10 %}
            "{{i}}">
                {{zk_host}}0{{i}}
                {{zk_prot}}
            
            {else %}
            "{{i}}">
                {{zk_host}}{{i}}
                {{zk_prot}}
            
            {%endif%}
            {% endfor %}
    

    
    

    
        <case>
            10000000000
            0.01
            lz4
        case>
    

    
        ::/0
    

这里是我有5台机器需要安装,所以配置还比较简单。

yml文件:

#ansible-playbook playbook.yml --list-hosts
#ansible-playbook /etc/ansible/install_file/clickhouse_install.yml --list-hosts
#https://www.cnblogs.com/f-ck-need-u/p/7571974.html
- hosts: clickhouse_cluster_setup_beijing #hosts 定义单个主机或组
  remote_user: root #以root账户执行
  vars: #定义变量
    ck_version: 19.4.0.49-1.el7 #ck rpm module version
    #shard of ck variable parameter
    shard_port: 9000
    shard_user: default
    shard_host_pre: bjg-techcenter-appservice-appservice-push-push-clickhouse-
    shard_num: 5
    replica_num: 1
    #zk variable parameter
    zk_prot: 2181
    zk_host: bje-data-platform-zookeeper-

  tasks:
    - name: download and install curl #在所有机器上下载并安装 curl
      shell: yum install -y curl
    - name: Download and execute the clickhouse installation script provided by packagecloud.io on the replica, distributed, chproxy machine # 将指定版本的 clickhouse-server, clickhouse-client 安装到 replica 和 distributed 机器上
      shell: curl -s https://packagecloud.io/install/repositories/altinity/clickhouse/script.rpm.sh | sudo bash
    - name: Install clickhouse-server, clickhouse-client on replica and distributed machines
      shell: sudo yum install -y clickhouse-server-{{ck_version}} clickhouse-client-{{ck_version}} clickhouse-compressor-{{ck_version}}
    - name: Batch modify startup scripts # 批量修改启动脚本
      shell: sed -i 's/\/var\/log\/clickhouse-server/\/data\/clickhouse\/logs/g' /etc/init.d/clickhouse-server
    - name: write the metrika config file
      template: src=/etc/ansible/install_file/metrika.xml.j2 dest=/etc/clickhouse-server/metrika.xml backup=yes
    - name: write the config config file
      template: src=/etc/ansible/install_file/config.xml.j2 dest=/etc/clickhouse-server/config.xml backup=yes
    - name: write the user config file
      template: src=/etc/ansible/install_file/users.xml.j2 dest=/etc/clickhouse-server/users.xml backup=yes
    - name: Synchronous configuration #将 clickhouse 用户设置为 login 用户
      shell: usermod -s /bin/bash clickhouse
    - name: Synchronous mkdir configuration
      shell: mkdir /data/clickhouse/logs -p
    - name: Synchronous chown configuration #将 clickhouse 放置到 /data/clickhouse/ 下
      shell: chown clickhouse.clickhouse /data/clickhouse/ -R
    - name: service clickhouse-server restart #重新启动服务
      shell: service clickhouse-server restart

 这里因为clickhose安装需要config.xml\users.xml\metika.xml文件,因为这边通过在主控机器上配置通用的模版来批量在每一个管理机器上创建并写入文件,我的例子中是将主控机器上的/etc/ansible/install_file/下面的三个.j2文件根据模版语法写入到管理机器上的/etc/clickhouse-server/目录下面.xml文件中,当然具体还需要根据自己实际情况来修改模版即可。

3、定时任务删除ck集群分区数据

yml文件

- hosts: delete_ck_host #hosts 定义单个主机或组
  remote_user: root #以root账户执行
  vars: #定义变量
    port: 9000
    tableName:
      - ck_local_qukan_report_cmd_11001
      - ck_local_qukan_report_cmd_under8
  tasks:
    - name: echo date
      command: date -d "2 days ago" +%Y-%m-%d
      register: date_output
    - name: echo partition
      command: clickhouse-client --host {{inventory_hostname}} --port {{port}} --database default --multiquery -q "SELECT DISTINCT formatDateTime(log_timestamp, '%F') AS partition FROM {{item}}"
      loop: "{{tableName}}"
      register: partitions
    - name: execute shell
      shell: clickhouse-client --host {{inventory_hostname}} --port {{port}} --database default --multiquery -q "alter table {{item[0]}} drop partition '{{item[1]}}'"
      when: item[1] < "{{date_output.stdout}}"
      with_nested:
        - "{{tableName}}"
        - "{{partitions.results[0].stdout_lines}}"

sh 文件

#!/bin/bash
echo "---------------------------------delete_ck.sh task start---------------------------------------------"
ansible-playbook /etc/ansible/install_file/task/delete_ck.yml
echo "---------------------------------delete_ck.sh task end--------------------------------------------"


#27 20 * * * /etc/ansible/install_file/task/delete_ck.sh

 

转载于:https://www.cnblogs.com/wynjauu/p/10702412.html

你可能感兴趣的:(ansible应用)