大家可以來思考看看,Playbook 是一個會根據時間演變一直膨脹的東西,換句話說流程會一直根據業務情境不同一直變換。
雖然說 Ansible 具有冪等性,但是也不可能每一次執行都要從頭跑到尾吧。
於是這時候我們就可以善用 Tags 在我們龐大的 Playbook 中定義出每個流程會需要執行的任務。
---
# tags-demo.yml
- name: Day18 tags demo
hosts: all
gather_facts: false
tasks:
- name: Preflight check
assert:
that: ansible_version.full is defined
tags: [always]
- name: Install a CLI tool (example)
package:
name: curl
state: present
become: yes
tags: [packages]
- name: Render config to /tmp
copy:
dest: /tmp/tags-demo.conf
content: |
demo=true
mode: '0644'
tags: [config]
- name: Dangerous operation (only if explicitly targeted)
debug:
msg: "NEVER runs unless --tags never is set"
tags: [never]
- name: Grouped config changes
tags: [config]
block:
- lineinfile:
path: /tmp/tags-demo.conf
line: "feature_x=enabled"
- debug:
msg: "Config updated"
接著我們來執行看看
# 只要跑 config 任務
ansible-playbook -i inventory.ini tags-demo.yml --tags config
# 要略過 packages 任務
ansible-playbook -i inventory.ini tags-demo.yml --skip-tags packages
看到這邊大家有感受到 Tags 的魅力了嗎?筆者只能說這真的很好用,大家要記得多多善用。
這裡筆者分享個實際上會怎麼使用這個 Tags,我們先創建一份 site.yaml
。
假設我們已經有寫好一些 Roles 可以使用了
# site.yaml
---
- name: "[初始化機器設定]"
hosts: web_servers
vars:
ansible_user: root
vars_files:
- "vars_files.yml"
roles:
- role: deploy_user
tags: ["bootstrap", "deploy_user"]
- role: setup_hostname
tags: ["bootstrap", "setup_hostname"]
- name: "[初始化機器設定]"
hosts: web_servers
vars_files:
- "vars_files.yml"
roles:
- role: create_default_directory
tags: ["bootstrap", "create_default_directory"]
- role: setup_system_tools
tags: ["bootstrap", "setup_system_tools"]
- role: setup_logrotate
tags: ["bootstrap", "setup_logrotate"]
- name: "[同步機器設定]"
hosts: web_servers
vars_files:
- "vars_files.yml"
roles:
- role: sync_app_config
tags: ["bootstrap", "sync_config"]
- role: sync_nginx_config
tags: ["bootstrap", "sync_config", "sync_nginx_config", "deploy_nginx"]
- role: sync_kafka_config
tags: ["bootstrap", "sync_kafka_config", "deploy_kafka"]
- role: sync_redis_config
tags: ["deploy_redis", "sync_redis_config"]
- role: sync_mysql_config
tags: ["deploy_mysql", "sync_mysql_config"]
- role: check_container_status
tags: ["predeploy_fastapi", "sync_fastapi_config", "deploy_fastapi"]
- role: sync_fastapi_config
tags: ["predeploy_fastapi", "deploy_fastapi", "sync_fastapi_config"]
- name: "[部署服務任務]"
hosts: web_servers
vars_files:
- "vars_files.yml"
roles:
- role: deploy_redis
tags: ["deploy_redis"]
- role: deploy_mysql
tags: ["deploy_mysql"]
- role: deploy_nginx
tags: ["bootstrap", "deploy_nginx"]
- role: deploy_kafka
tags: ["bootstrap", "deploy_kafka"]
- role: predeploy_fastapi
tags: ["predeploy_fastapi", "deploy_fastapi"]
- role: deploy_fastapi
tags: ["deploy_fastapi"]
# 所以我們可以只透過 site.yaml 並搭配 Tags 來做到非常靈活的流程控制
# 假設我們今天開啟一台新機器,直接下 bootstrap tag,跑完 Ansible 任務後這台新機器就會處於 Ready 狀態
ansible-playbook -i inventory.ini site.yml -t bootstrap
# 這個時後我們再透過其他的 tag 來做部署,那就會自動把 fastapi 部署到新機器上面了
ansible-playbook -i inventory.ini site.yml -t predeploy_fastapi, deploy_fastapi
以上只是舉個比較實務上的例子,那怎麼搭配就看各位的取捨跟設計了。
packages
、config
、deploy
三類,分別以 --tags/--skip-tags
驗證。never
,確認只有 --tags never
時才會執行。明天來講點如何加速 Ansible 自動化流程!