星期五, 5月 13, 2016

以 ansible 安裝 consul in openSUSE Leap 42.1

Consul with ansible

因為之後 docker swarm 需要用到 consul , 所以就建立 consul

OS: openSUSE Leap 42.1

一般建立 consul 的作法

建立使用者 consul ( 不一定要建立這個使用者, 這個是工作上協調的結果 )
#useradd  -m  consul

建立相關目錄
#mkdir  -p  /opt/consul/bin   /opt/consul/data   /opt/consul/config

設定權限以及更改擁有人為使用者 consul
#chown  -R  consul   /opt/consul/

#chmod  -R  700  /opt/consul/

從網路取得 consul 並解壓縮 zip ( 這邊以 0.6.4 為例 )


#unzip  consul_0.6.4_linux_amd64.zip

將 consul 複製到指定的目錄
#cp  consul   /opt/consul/bin

切換使用者 consul
#su  -  consul

編寫設定檔 ( 這邊我們是先試用官方的服務 https://atlas.hashicorp.com/consul )
>vi   /opt/consul/config/atlas_consul.json


{
 "bind_addr": "請填入機器IP",
 "atlas_infrastructure": "請填入帳號/請填入名稱",
 "atlas_join": true,
 "atlas_token": "請填入自己的token",
 "bootstrap_expect": 3,
 "datacenter": "Hsinchu-NCHC",
 "data_dir": "/opt/consul/data",
 "log_level": "INFO",
 "node_name": "請填入主機名稱",
 "encrypt": "請填入加密的資訊",
 "server": true
}


啟動 consul agent
> /opt/consul/bin/consul   agent   -config-dir   /opt/consul/config/

目前是用 nohup 方式在背景執行, 然後離開
> nohup /opt/consul/bin/consul agent -config-dir /opt/consul/config/ &

停止 consul 的方式( 這樣不會產生 error )
/opt/consul/bin/consul  leave

------- Ansible 方式 ----------

接下來自己寫了一個 ansible 的playbook 來安裝 consul

檔案名稱 Consul_install.yml

---
#########################################################  
# Install consul and setup boot
- name: use when conditionals and setup module (facts)
 hosts: all
 tasks:
# 使用 setup moudule 列出 OS 種類
   - name: use setup module to list os distribution
# setup moudle 可以使用 filter 過濾相關內容
     setup: filter=ansible_distribution


#########################################################  

- name: Install consul , add user and setting up env
# use group
 hosts: ConsulHost
 sudo: True
 tasks:
   - name: Add user consul
     user: name=consul

   - name: Create folder for consul
     file: path=/opt/consul/{{ item }} state=directory owner=consul mode=700
     with_items:
       - bin
       - data
       - config

   - name: Install wget with openSUSE Leap
     zypper: name={{ item }}
     with_items:
       - wget
     when: ansible_distribution == "openSUSE Leap"

   - name: Install wget with CentOS
     yum: name={{ item }}
     with_items:
       - wget
     when: ansible_distribution == "CentOS"

   - name: Install wget with Ubuntu
     apt: name={{ item }} update_cache=yes
     with_items:
       - wget
     when: ansible_distribution == "Ubuntu"

#-------------------------------------------------------  

   - name: Get consul 0.6.4 zip
     shell: wget   https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zip

#-------------------------------------------------------  

   - name: Unzip consul 0.6.4 zip file
     shell: unzip consul_0.6.4_linux_amd64.zip

#-------------------------------------------------------  

   - name: Copy consul to path
     shell: cp consul /opt/consul/bin

#########################################################  
# Copy config files with user consul
- name: copy config files with user consul
 hosts: ConsulHost
 tasks:
   - name: use setup module to list os distribution
     become_user: consul
     become: yes
     template: src=templates/atlas_consul.json dest=/opt/consul/config/atlas_consul.json

# 這個部份有待討論如何在背景執行 可能要寫成服務的方式, 目前會斷
#    - name: running consul at backgroud
#      become_user: consul
#      become: yes
#      shell: nohup /opt/consul/bin/consul agent -config-dir /opt/consul/config &


檔案 templates/atlas_consul.json

{% for host in groups['ConsulHost'] %}
{
 "bind_addr": "{{ hostvars[host].ansible_default_ipv4.address }}",
 "atlas_infrastructure": "請填入帳號/請填入名稱",
 "atlas_join": true,
 "atlas_token": "請填入自己的token",
 "bootstrap_expect": 3,
 "datacenter": "Hsinchu-NCHC",
 "data_dir": "/opt/consul/data",
 "log_level": "INFO",
 "node_name": "{{ hostvars[host].ansible_hostname }}",
 "encrypt": "請填入加密的資訊",
 "server": true
}
{% endfor %}


沒有留言: