Red Hat Ansible Automation Platform (AAP) 近期引入了一項名為 Policy Enforcement 的強大功能,通常被稱為 Policy as Code。這項新功能旨在使企業能夠大規模地定義、管理並強制執行其 IT 自動化工作的治理、風險與合規性 (GRC) 要求。透過將策略直接整合到自動化工作流程中,組織可以確保其 Ansible 自動化內容在執行前、執行中和執行後,皆能符合內部政策與外部法規的嚴格標準。
核心概念:以程式碼定義與強制執行策略
傳統上,IT 策略通常被記錄在靜態文件中,依賴手動審查和流程來執行。這種方式不僅效率低下,更容易因人為疏忽而產生錯誤與風險。Ansible Automation Platform 的 Policy as Code 功能,其核心理念便是將這些抽象的策略轉化為可由機器讀取和自動執行的程式碼,實現自動化治理。
此功能的核心技術整合了業界廣泛採用的開源專案 Open Policy Agent (OPA)。OPA 是一個通用的策略引擎,它使用一種名為 Rego 的宣告式語言來編寫策略。其運作模式如下:
對 OPA 與 Rego 語法有興趣的讀者,可以參考官方文件獲得更詳細的資訊:Open Policy Agent Documentation
https://www.openpolicyagent.org/docs
實際應用場景:
Policy as Code 的應用範圍極廣,能有效解決企業在自動化管理上的諸多痛點,例如:
筆者出於對此功能的興趣,自行建置了一套環境進行測試。以下將分享安裝 Red Hat AAP 並啟用 Policy as Code 功能的過程與注意事項。
1. 版本與功能狀態
首先,Policy as Code 功能需要安裝 AAP 2.5 或更新版本。在撰寫本文時,此功能仍處於技術預覽 (Tech Preview) 階段。
https://docs.redhat.com/en/documentation/red_hat_ansible_automation_platform/2.5/html/release_notes/patch_releases#policy_as_code
根據官方文件,啟用此功能需要透過 feature flags 來明確開啟。
https://access.redhat.com/articles/7109282?extIdCarryOver=true&intcmp=RHCTG0250000449879&sc_cid=701f2000001Css0AAC
2. 安裝方式與環境準備
為了能將所有 AAP 元件部署在單一 VM 上,我選擇了 Containerized Installation (容器化安裝)。
安裝指南https://docs.redhat.com/en/documentation/red_hat_ansible_automation_platform/2.5/html/containerized_installation/aap-containerized-installation#system-requirements
在安裝過程中,有幾點需要特別注意:
其他細節請參照https://docs.redhat.com/en/documentation/red_hat_ansible_automation_platform/2.5/html/containerized_installation/aap-containerized-installation#preparing-the-rhel-host-for-containerized-installation
3. 執行安裝
安裝腳本需要一個 inventory-growth (all in one) 檔案來定義主機與相關變數。以下是一個精簡的範例,請根據您的環境進行修改:
[automationgateway]
controller.jeff.io
[automationcontroller]
controller.jeff.io
[database]
controller.jeff.io
[all:vars]
ansible_connection=local
backup_dir=/app/ansible-automation-platform-containerized-setup-bundle-2.5-17-x86_64/
postgresql_admin_username=請填user
postgresql_admin_password=請填密碼
bundle_install=true
bundle_dir=/app/ansible-automation-platform-containerized-setup-bundle-2.5-17-x86_64/bundle
redis_mode=standalone
gateway_admin_password=請填密碼
gateway_pg_host=controller.jeff.io
gateway_pg_password=請填密碼
controller_admin_password=請填密碼
controller_pg_host=controller.jeff.io
controller_pg_password=請填密碼
controller_percent_memory_capacity=0.5
接著,使用以下指令執行安裝 Playbook。請注意,我們透過 -e 參數直接傳遞 feature_flags 變數來啟用此功能:
ansible-playbook -i inventory-growth -e "{'feature_flags': {'FEATURE_POLICY_AS_CODE_ENABLED': True}}" ansible.containerized_installer.install