ansible-builder

看到這篇:紅帽推出IT自動化工具Ansible Builder以加速執行環境創建 | iThome ,所以就來試試看。

就我試用過以後,我的理解是,這工具可以幫忙創建出一個具有 Ansible 環境的 docker image。

環境是定義在 execution-environment.yml 裡,讓我們直接看範例

---
version: 1

# 即基礎 image,目前來說,都是用 quay.io/ansible/ansible-runner:stable-2.9-devel
base_image: 'quay.io/ansible/ansible-runner:stable-2.9-devel'

# 需要自訂 ansible.cfg 的話,這邊就是填 ansible.cfg 的路徑檔名
ansible_config: 'ansible.cfg'

# 相依
# galaxy 是填 requirements.yml,檔案裡描述 playbook 會用到的 role/collection,沒用到可以不填
# python 則是填 requirements.txt,檔案裡描述會使用到的 Python modules,沒用到可以不填
dependencies:
  galaxy: requirements.yml
  python:

# 額外要加入的 docker image 建置步驟
# prepend 會在預定的建置步驟之前
# append 則是在預定的建置步驟之後
additional_build_steps:
  prepend: |
    RUN whoami
    RUN cat /etc/os-release
  append:
    - RUN echo This is a post-install command!
    - RUN ls -la /etc

有了 execution-environment.yml 之後,就可以用 ansible-builder 來建置。

首先,得先安裝:

# python3
pip3 install ansible-builder

接著就可以建置了

ansible-builder build --tag=example

建置完成,會有 docker image,在目錄下則會出現 context 資料夾,這個資料夾裏面就是 ansible-builder 建置過程中所產出的 Dockerfile 以及建置 Docker image 所需的檔案。

打開 Dockerfile ,就會看到 ansible-builder 幫我們產出了什麼。

FROM quay.io/ansible/ansible-runner:stable-2.9-devel as galaxy

ADD ansible.cfg ~/.ansible.cfg

ADD requirements.yml /build/

RUN ansible-galaxy role install -r /build/requirements.yml --roles-path /usr/share/ansible/roles
RUN ansible-galaxy collection install -r /build/requirements.yml --collections-path /usr/share/ansible/collections

RUN mkdir -p /usr/share/ansible/roles /usr/share/ansible/collections

FROM quay.io/ansible/python-builder:latest as builder

ADD requirements_combined.txt /tmp/src/requirements.txt
RUN assemble

FROM quay.io/ansible/ansible-runner:stable-2.9-devel

RUN whoami
RUN cat /etc/os-release

COPY --from=galaxy /usr/share/ansible/roles /usr/share/ansible/roles
COPY --from=galaxy /usr/share/ansible/collections /usr/share/ansible/collections

COPY --from=builder /output/ /output/
RUN /output/install-from-bindep && rm -rf /output/wheels
RUN echo This is a post-install command!
RUN ls -la /etc

大抵來說,

  1. 使用了 multi stage build 以減少 docker image 的大小
  2. 使用 ansible-galaxy 安裝相依的 role/collection
  3. 會看到 additional_build_steps 裡描述的步驟

好,那建置出 docker image 以後,怎麼使用呢?

假設 playbook 是放在 project 目錄下,那麼就這樣執行

docker run --rm -v /runner/project:$(pwd)/project -it example:latest ansible-playbook -i localhost, -c local /runner/project/test.yml

這邊稍微取了點巧,只簡單用 local connection (-i localhost, -c local) 在本機執行,你也可以在這邊使用自己的 inventory。

建置出 Ansible 執行環境的 docker image 以後,除了可以固定住執行 Ansible playbook 的環境,也有利於打造出標準化的 CD 環境,進而減少開發與佈署的時間。

Ansible – 版本判斷條件句

執行之前寫好的 Ansible 腳本,卻有錯誤,奇怪,明明之前是好的啊?看了錯誤訊息,發現是找不到 /proc/sys/net/ipv4/tcp_tw_recycle 這個檔案的關係。怎麼會沒有這個檔案呢?使用 SSH 連上主機去看,還真的是沒有,上網找了之後,發現是 Linux Kernel 在 4.12 以後移除掉這檔案了(連結)。

好吧,因為腳本還有機會在較低版本的 Linux kernel 使用,必須得使用條件式來處理。在 Ansible 腳本裡,可以用 when 來做條件判斷,但是要怎麼取得 Linux kernel 版本以及做條件判斷呢?在 StackOverflow 上看到這篇:How to compare kernel (or other) version numbers in Ansible

從這篇得知:

  1. 可以使用 ansible_kernel 就可以取得 Linux kernel 版本
  2. 可以使用 version_compare 來比較版本

所以下面的腳本就可以在 Linux kernel 是 4.12 以後的版本印出 “foo”

tasks:
  - name: Display "foo" if kernel > 4.12
    debug:
      msg: "foo"
      when: ansible_kernel | version_compare('4.12', '>')

太好了。可是再仔細看,執行時有 deprecated 的警告耶,說在之後會把 version_compare 拿掉。上 Ansible 網站看了 Ansible 的文件之後,做了調整,新的語法要使用 “is” 來判斷,然後用 version() 來取代 version_compare。

tasks:
  - name: Display "foo" if kernel > 4.12
    debug:
      msg: "foo"
      when: ansible_kernel is version('4.12', '>')

好,收工。

Ansible提速

參考資料

上面的文章幾乎都是說要調整 ansible.cfg,Speeding up Ansible Playbook runs 這篇仔細說明每個參數加了之後有提速多少,很有參考價值。accelerate mode 則是加在 playbook 裡。

ansible.cfg 主要關鍵設定:

  1. pipeline = True
  2. control_path = /tmp/ansible-ssh-%%h-%%p-%%r
  3. ssh_args = -o ControlMaster=auto -o ControlPersist=60s
  4. poll_interval = 5
  5. forks = 25
  6. fact_caching = jsonfile
  7. fact_caching_connection = /tmp/.ansible_fact_cache

補充 ssh_args 的說明,這兩個設定主要是在會一直頻繁使用時,可以重複使用連線並避免太快斷線。

也可以加上 UseDNS = no,避免使用 DNS 反查。

Ansible 需要 python

如果目標主機上沒有 Python 時,Ansible 可是會抗議的。

所以必須要先幫目標主機裝上 Python 才行。首先要將 gather_facts 設定為 no,再利用 raw 模組來進行安裝,不使用 gather_facts 跟其他模組的原因是這些都會使用到 Python。

---
- hosts: all
  become: yes
  gather_facts: no
  tasks:
    - name: install python in Ubuntu
      raw: test -e /usr/bin/python || (apt -y update && apt install -y python-minimal)

上面用的是 apt,表示適用於 Debian/Ubuntu 等 debian-like 的 distro,至於其他的 distro ,就看套件管理程式是什麼囉。

Run ansible in gitlab runner

在 gitlab runner 裡想要執行 ansible playbook 進行佈署,幾個問題以及我的應對:

  1. ansible 的安裝:是可以自己加 ansible 的 package repository ,然後用 apt 安裝,但已經有好心人打包了裝好 ansible 的 docker image ,只要在 .gitlab-ci.yml 加入 image 設定即可:
    image: williamyeh/ansible:ubuntu14.04
  2. playbook 要放在哪裡?因為要直接在專案進行佈署,所以 playbook 也要放在專案裡,我在專案裡建立了一個 ansible 的目錄,把 playbook 放在這裡。
  3. 私密資料如何保存?gitlab 在專案設定裡提供了 variables,可以用來存放這些私密的資料,在 .gitlab-ci.yml 裡,只要用 $VAR_NAME 就可以使用這些變數。.gitlab-ci.yml 裡也可以自訂 variables ,但這些 variables 是進到 git repository 的,要視情況使用。
  4. Ansible 連到目的機器需要 SSH Key:這比較麻煩一點,好在 gitlab 有提供如何使用 SSH key 的文件,步驟很詳細,簡述如下:
    1. 在專案設定的 variables 裡新增有 SSH private key 的變數。
    2. 檢查 ssh-agent ,如果沒有就安裝 openssh-client
    3. 執行 ssh-agent
    4. 用 ssh-add 匯入第一個步驟所設定好的變數
    5. 在 ~/.ssh/config 裡加入不檢查 host key 的設定:
      Host *
          StrictHostKeyChecking no

參考資料:

Ansible 處理多主機不同密碼

用 Ansible 一陣子了,比較麻煩的地方是,主機不一定密碼都相同,簡單的方法當然是讓主機密碼都一樣,但這樣就降低了安全性。有沒有不需要改動密碼的方式呢?在網路上找了半天,找到這篇:ubuntu – Ansible: how to run a play with hosts with different passwords? – Stack Overflow ,裏面有個回應是建議使用 group_vars + ansible-vault + ansible_become_user/ansible_become_pass 來解決。

以下紀錄大致的步驟:

  1. 在 playbook 所在目錄或是 /etc/ansible 下建立 group_vars 資料夾 (以下簡稱 group_vars 資料夾)。
  2. 在 inventory 檔案裡,將相同密碼的主機編成一個群組,這裡假設群組名稱是 foo。
  3. 進入 group_vars 資料夾,用 ansible-vault create foo.yml ,這時候會詢問你 vault 的密碼,輸入完成以後,會開啟編輯器,請在裏面輸入
    ---
    ansible_become_user: "root"
    ansible_become_pass: "your_password"
  4. 存檔離開編輯器。
  5. 在輸入 ansible-playbook 或 ansible 指令時,帶 –ask-vault-pass 參數,也就是在執行時,會問你 vault 檔案的密碼,然後自動解密並讀入裏面的變數執行。

之後要編輯加密過的檔案,得用 ansible-vault edit foo.yml 才行。

參考資料:

Ansible 的 callback

Callback 可以讓人有機會處理執行的結果,網路上有些人會寫自己的 callback plugin 以便統計執行結果什麼的。寫 callback plugin 的方法蠻簡單的,先繼承 CallbackBase ,然後覆寫裏面的方法即可,Ansible 有提供不少範例可供參考。

幾件事情要注意:

  • 寫 callback plugin 時:
    • 要寫 CALLBACK_TYPE,這裡如果填 ‘stdout’ 時,在 ansible.cfg 裡的 stdout_callback 也要指定為這個 callback ,這樣 callback 才會生效。
    • 要寫 CALLBACK_NAME,這是 callback 的名稱,建議最好跟檔案名稱相符,並且不要跟官方的 callback 重複。
  • 使用時:
    • ansible.cfg 裡要指定 callback_plugins,這是填路徑。可以填相對路徑也可以填絕對路徑。
    • ansible.cfg 裡要指定 callback_whitelist,這是填 callback 的名稱,可以用 ‘,’ 分隔多個 callback。顧名思義,是一個白名單的形式,有填才表示啟用 callback。
    • callback 在預設情況下只有在使用 ansible-playbook 時才會生效,如果要讓 ansible adhoc 也能用 callback,得在 ansible.cfg 裡指定 bin_ansible_callback=True 。不過,CALLBACK_TYPE=’stdout’ 的 callback plugin 是適用的。

 

關於 Ansible EC2 module 的紀錄

指定 exact_count=1,第一次執行時,新建的 instances 會同時放在 instances 與 tagged_instances 裡。(請看 ec_create_result.txt)

exact_count=1,再執行一次,changes 會是 false,instances 裡是空的,而 tagged_instances 裡則是之前已經建立的 instances (請看 ec2_create_again_result.txt)

將 exact_count 設定為 2,再次執行,changes 會是 true,instances 裡是新建的 instance,tagged_instances 裡除了之前已經建立的 instance 之外,還會有新建的 instance。

將 exact_count 改回 1,再次執行,changes 會是 true,instances 裡是被終結掉的 instance,state 是 terminated,而 tagged_instances 裡則是仍存活的 instances。 (請看 ec2_create_decrease_exact_count_result.txt)

Deprecation of tags= in include

碰到這個訊息:

[DEPRECATION WARNING]: You should not specify tags in the include parameters. All tags should be specified using the task-level option. This feature will be removed in a future release. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg.

這是因為這樣寫:

- include: setup.yml tags=myrole,setup
- include: myrole.yml tags=myrole

論壇有人問,開發團隊回答說,建議的作法是把 tags 拿下來,不要放在 include: xxx 的後面,也就是:

- include: setup.yml
  tags=myrole,setup
- include: myrole.yml
  tags=myrole

Automate Django createsuperuser in Ansible

Ansible 有個 django_manage 模組,可以很方便的執行 django 裡的 manage.py,但是受限於 createsuperuser 的關係,並沒辦法在建立 superuser 的同時,一併設定密碼。

一般網路上的解決方法是自己寫個小 python 腳本 (可以看這篇 How to automate createsuperuser on django? ),丟給 shell 去執行。我是想到可以利用 manage.py 提供的 changepassword 並搭配 expect 來做,大致上是這樣子:

另外也做了避免重複建 superuser 的機制。