技術討論區 > DevOps 討論版

以 ansible 安裝 consul in openSUSE Leap 42.1

(1/1)

sakana:
原文在此 http://sakananote2.blogspot.tw/2016/05/ansible-consul-in-opensuse-leap-421.html

以 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 為例 )

 
--- 代碼: ---#wget   https://releases.hashicorp.com/consul/0.6.4/consul_0.6.4_linux_amd64.zip

 #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 %}
--- 程式碼結尾 ---

導覽

[0] 文章列表

前往完整版本