ansible部署java及数据库环境
阅读原文时间:2023年07月08日阅读:4

  wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.3.1.rpm
  wget https://artifacts.elastic.co/downloads/logstash/logstash-6.3.1.rpm
     wget https://artifacts.elastic.co/downloads/kibana/kibana-6.3.1-x86_64.rpm

1. 安装ansible
yum install epel-release -y && yum install -y ansible
  配置ansible服务器实现免密登录,配置之后,第一次需要在ansible服务器上ssh

  1. 默认配置文件在/etc/ansible加入以下配置[test]服务器组可随意命名
    [test]
    192.168.0.21
    192.168.0.52
  2. 创建java及data目录 ----java对应的java环境及中间件,data数据库等
    mkdir -pv /etc/ansible/java/roles/{prepare,jdk,redis,zk,mq}/{tasks,files,templates,vars,meta,default,handlers}
    mkdir -pv /etc/ansible/data/roles/{prepare,es,mysql,kibana,logstash}/{tasks,files,templates,vars,meta,default,handlers}
  3. 将软件包移动到对应目录下的files目录

5. 将redis的配置文件修改成如下配置
bind {{ ansible_default_ipv4.address }} #获取服务器的ipv4地址

  1. 编写每个软件安装的tasks
    jdk: #前面格式一定要对齐缩进
    - name: mkdir soft dir
    shell: mkdir /soft
    - name: copy jdk
    copy: src=jdk-8u131-linux-x64.rpm dest=/soft
    - name: install jdk
    shell: yum localinstall /soft/jdk-8u131-linux-x64.rpm -y
    mq:
    - name: install activemq
    copy: src=apache-activemq-5.11.0-bin.tar.gz dest=/soft/apache-activemq-5.11.0-bin.tar.gz
    - name: tar activemq
    shell: tar xzf /soft/apache-activemq-5.11.0-bin.tar.gz -C /usr/local/
    - name: copyconffile
    template: src=activemq dest=/etc/init.d
    - name: chmod
    shell: chmod +x /etc/init.d/activemq && chkconfig --add activemq
    - name: start activemq
    shell: systemctl start activemq
    redis:
    - name: copy redis file
    copy: src=redis-4.0.14.tar.gz dest=/soft
    - name: tar redis
    shell: tar xzf /soft/redis-4.0.14.tar.gz -C /usr/src/
    - name: delete redis conf
    shell: rm -f /usr/src/redis-4.0.14/redis.conf
    - name: install gcc
    shell: yum install -y gcc
    - name: install redis
    shell: cd /usr/src/redis-4.0.14 && make && make PREFIX=/usr/local/redis install
    - name: mkdir redis logfile && conf file
    shell: mkdir /var/log/redis && mkdir /usr/local/redis/conf && mkdir /var/lib/redis
    - name: copyconffile
    template: src=redis.conf dest=/usr/local/redis/conf/redis.conf
    - name: start
    shell: /usr/local/redis/bin/redis-server /usr/local/redis/conf/redis.conf
    zk:
    - name: install zookeeper
    copy: src=zookeeper-3.4.6.tar.gz dest=/soft/
    - name: tar zookeeper
    shell: tar xzf /soft/zookeeper-3.4.6.tar.gz -C /usr/local/
    - name: copy conf file
    copy: src=zoo.cfg dest=/usr/local/zookeeper-3.4.6/conf/zoo.cfg
    - name: mkdir data
    shell: mkdir -p /opt/zookeeper/data
    - name: start zookeeper
    shell: sh /usr/local/zookeeper-3.4.6/bin/zkServer.sh start

7. 在jdk/roles新建site.yml文件
- name: java_env install
remote_user: root
hosts: test
roles:
- jdk
- zk
- redis
- mq

  1. 执行ansible-playbook
    ansible-playbook -i /etc/ansible/hosts /etc/ansible/java/roles/site.yml

9. 安装data数据环境
es: 同样将有地址(es,kibana等)的修改为{{ ansible_default_ipv4.address }}
- name: copy es
copy: src=elasticsearch-6.3.1.rpm dest=/soft/
- name: install es
shell: rpm -ivh /soft/elasticsearch-6.3.1.rpm
- name: remove es config
shell: rm -f /etc/elasticsearch/elasticsearch.yml
- name: copy es config
template: src=elasticsearch.yml dest=/etc/elasticsearch/
- name: start es
service: name=elasticsearch enabled=yes state=started
jdk:和上面安装java环境一样
kibana:
- name: copy kibana
copy: src=kibana-6.3.1-x86_64.rpm dest=/soft/
- name: install kibana
shell: rpm -ivh /soft/kibana-6.3.1-x86_64.rpm
- name: remove es config
shell: rm -f /etc/kibana/kibana.yml
- name: copy kibana config
template: src=kibana.yml dest=/etc/kibana/
- name: start kibana
service: name=kibana enabled=yes state=started
logstash:
- name: copy logstash
copy: src=logstash-6.3.1.rpm dest=/soft/
- name: install logstash
shell: rpm -ivh /soft/logstash-6.3.1.rpm
- name: start es
service: name=logstash enabled=yes state=started
mysql:
- name: copy mysql
copy: src=mysql-community-release-el7-5.noarch.rpm dest=/soft
- name: install mysql.rpm
shell: rpm -ivh /soft/mysql-community-release-el7-5.noarch.rpm
- name: install mysql
shell: yum install -y mysql-community-server
- name: start mysql
service: name=mysql enabled=yes state=started    
ansible-playbook -i /etc/ansible/hosts  /etc/ansible/data/roles/site.yml