星期六, 4月 21, 2018

利用 Ansible playbook 建立 azure 與 ansible套件於 openSUSE & Ubuntu Linux

利用 Ansible playbook 建立 azure 與 ansible套件於 openSUSE & Ubuntu Linux

之前的文章在測試  Azure Dynamic Inventory 的時候, azure 與 ansible 環境是手動建立的.

所以順手寫了一個 azure_install.yml 的 playbook
  • 針對 hosts 檔案內的群組[ AzureHost ]  進行相關套件安裝, 所以只要把主機加入該群組就好

處理內容如下
  • 安裝 Azure python SDK
  • 安裝 ansible 套件然後不能跟 python 衝突( 符合 python 套件需要 )
  • 下載 azure_rm.py
  • 建立空檔案 ~/.azure/credentials 方便日後輸入認證資訊


azure_install.yml 檔案內容如下

---
#########################################################  
# Install docker package 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 python-pip and azure package
# use group
 hosts: AzureHost
#  sudo: True
 become: True
 tasks:
   - name: Install python-pip with openSUSE Leap
# 這邊使用 disable_recommends=no 加入zypper 建議的套件, 否則不會加入 apache2等其他套件
# 這邊使用 disable_gpg_check=yes, 讓公有雲例如 azure上面的額外 repo 不用check gpg key
     zypper: name={{ item }} disable_recommends=no disable_gpg_check=yes
     with_items:
       - python-pip
       - curl
       - wget
       - sshpass
     when: ansible_distribution == "openSUSE Leap"

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

   - name: upgrade pip version
     pip:
       name: pip
       state: latest
# 這邊透過 executable 來指定使用 pip2, 不然預設是 pip3
       executable: pip2   

   - name: install ansible[azure] with pip
     pip:
       name: ansible[azure]
# 這邊透過 executable 來指定使用 pip2, 不然預設是 pip3
       executable: pip2   

# 下載 azure_rm.py, 以便日後 Dynamic Inventory 使用
   - name: get azure_rm.py to root home
     get_url:
       url: https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/azure_rm.py
       dest: /root/azure_rm.py
       mode: 0551
# 建立 ~/.azure 目錄
   - name: create ~/.azure folder
     file:
       path: ~/.azure
       state: directory
# 建立 ~/.azure/credentials 檔案,之後要存放 azure 認證檔案
   - name: create ~/.azure/credentials
     file:
       path: ~/.azure/credentials
       state: touch

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


執行方式

如果是在 Azure 上面的 VM 執行
$ ansible-playbook   --ask-pass  --ask-become-pass  -u  使用者名稱   azure_install.yml

如果是一般 VM 可能就可以直接用 root 連接, 不用 --ask-become-pass :)

偷懶戰鬥力 又偷偷 + 1

~ enjoy it

Reference:

星期六, 4月 07, 2018

Docker-machine 與 digital ocean 小記

Docker-machine 與 digital ocean 小記


OS:  openSUSE Leap 42.3


因為讀書會的關係, 進入 Chapter 5 docker-machine
所以接下來進行 docker-machine 實驗


就目前的了解, 就是可以使用 docker-machine 來進行本機或是遠端的docker 安裝.
Docker Machine is a tool that lets you install Docker Engine on virtual hosts, and manage the hosts with docker-machine commands.
You can use Machine to create Docker hosts on your local Mac or Windows box, on your company network, in your data center, or on cloud providers like Azure, AWS, or Digital Ocean.




( 謎之音: 但是這個部份好像你都是用 ansible 解決掉了 ... )


參考官方網站


先來進行 docker-machine 的安裝
Docker-machine 官方 github, 目前是 0.14 版


下載 docker-machine
# curl -L https://github.com/docker/machine/releases/download/v0.14.0/docker-machine-`uname -s`-`uname -m` >  /tmp/docker-machine


  • uname 指令, 上面兩個指令結果會跑出 Linux 以及 x86_64
    • -s, --kernel-name        print the kernel name
    • -m, --machine            print the machine hardware name


使用 install 指令將 docker-machine 複製到 /usr/local/bin 下
# install    /tmp/docker-machine  /usr/local/bin/docker-machine


這樣就安裝完成了


還沒開始之前先觀察一下


# docker-machine   ls
NAME   ACTIVE  DRIVER STATE   URL SWARM DOCKER   ERRORS


接下來就是 docker-machine 與 digital ocean 的實驗


開始之前, 必須先要有 digital ocean 的 API, 可以參考


接下來就可以測試 docker-machine 指令了


# docker-machine  create  --driver digitalocean  --digitalocean-access-token 599122321a3b75aea39f07c3XXXXXXXXX52911013806421589763bdd667e511 docker-machine-test


Creating CA: /root/.docker/machine/certs/ca.pem
Creating client certificate: /root/.docker/machine/certs/cert.pem
Running pre-create checks...
Creating machine...
(docker-machine-test) Creating SSH key...
(docker-machine-test) Creating Digital Ocean droplet...
(docker-machine-test) Waiting for IP address to be assigned to the Droplet...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with ubuntu(systemd)...
Installing Docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env docker-machine-test


  • 重點在 --driver 選擇要建立的 host 種類
  • 會在 digital ocean 建立一個 droplet 名稱為 docker-machine-test
  • 看他的行為是透過 SSH 與憑證來控管


可以在 Digital Ocean 的 dashboard 上面觀察




再次觀察


# docker-machine   ls
NAME                  ACTIVE DRIVER  STATE URL              SWARM DOCKER ERRORS
docker-machine-test   - digitalocean  Running tcp://XXX.YYY.ZZ.WWW:2376           v18.03.0-ce   


觀察 Host IP
# docker-machine  ip docker-machine-test


查看相關資訊
# docker-machine   inspect  docker-machine-test


  • 這邊可能要注意的是 docker-machine 會把相關的資訊存在 ~/.docker/machine/machines/主機名稱/config.json


接下來使用 docker-machine  env 來觀察相關環境變數


# docker-machine  env docker-machine-test
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST=" tcp://XXX.YYY.ZZ.WWW:2376"
export DOCKER_CERT_PATH="/root/.docker/machine/machines/docker-machine-test"
export DOCKER_MACHINE_NAME="docker-machine-test"
# Run this command to configure your shell:
# eval $(docker-machine env docker-machine-test)


按照上面建議的 eval $(docker-machine env docker-machine-test) 去執行相關變數


# eval   $(docker-machine  env docker-machine-test)


觀察相關資訊
# echo   $DOCKER_TLS_VERIFY
1


# echo $DOCKER_HOST
tcp://XXX.YYY.ZZ.WWW:2376


# echo $DOCKER_MACHINE_NAME
docker-machine-test


這個時候如果下 docker  images 就會發現已經是在遠端 droplet 了 ( 因為是空的 …. )


# docker  images
REPOSITORY          TAG IMAGE ID            CREATED SIZE


進行相關測試


# docker  run   -d  -p  80:80  --name   demo  yeasy/simple-web


開啟 Droplet 的網頁觀察




成功 :)


接下來練習  docker-machine  stop 停止 droplet
# docker-machine  stop  docker-machine-test


Stopping "docker-machine-test"...
Machine "docker-machine-test" was stopped.


在 Digital Ocean Dashboard 觀察


# docker-machine  rm  docker-machine-test
About to remove docker-machine-test
WARNING: This action will delete both local reference and remote instance.
Are you sure? (y/n): y
Successfully removed docker-machine-test


  • 會把 ~/.docker/machine/machines 下面的主機相關設定移除
  • 也會移除 Digital Ocean 上面的 Droplet


好了, 可以繼續看第五章了
~ enjoy  it


Reference