Ansible
Ansible 比較 INI 本地和遠端
我的 Ansible 伺服器和遠端機器上有一個 Java 屬性文件
伺服器:/opt/deployment/application/build.properities
app=application build=1.0.15 etc=etc
在遠端機器上是相同的文件(如果已安裝),它可能包含新版本或舊版本
遠端:/opt/application/build.properities
app=application build=1.0.13 config1=config etc=etc
我可以使用 ansible.builtin.ini 將遠端電腦上的內部版本號與伺服器進行比較,並且:
if server > remote - do my upgrade block
if remote == "" (file does not exist) - do my install block
otherwise do nothing
我不清楚 ansible.builtin.ini 是針對本地伺服器還是遠端機器(我可能錯過了一些東西)。如果有區別的話,兩台機器都是 Ubuntu Linux。
我最終設法讓它工作,並想分享我使用的程式碼……
- name: Check if App is installed stat: path: "{{ app_buildfile }}" register: APPbuildfile - name: Get Build Number shell: grep build {{ APP_buildfile }} | awk -F'=' ' { print $2 } ' | tr -d ' ' when: APPbuildfile.stat.exists register: APP_currentbuild - debug: msg: Current version {{ APP_currentbuild.stdout }}, Deployment Version {{ APP_deployment_version }} - name: New Installation block: - Install Actions.... - name: Set actioned fact set_fact: actioned: 1 when: APP_currentbuild is not defined - name: Upgrade Installation block: - Upgrade Actions... - name: Set actioned fact set_fact: actioned: 2 when: APP_currentbuild.stdout|length == 0 or APP_currentbuild.stdout is version(APP_deployment_version,'lt') - name: Post Install Tasks block: - Post install actions... when: actioned is defined
只是在我發表評論後給出大圖。複製該文件並在它發生更改時通知處理程序。或者,您可以在任務上註冊一個變數並檢查
when : <registered_var>.changed
,但通常首選處理程序。--- - hosts: my_remote_group tasks: - name: Make sure remote ini file is aligned with controller copy: src: /opt/deployment/application/build.properties dest: /opt/application/build.properties owner: some_relevant_user group: some_relevant_group mode: 0660 notify: upgrade_my_package handlers: - name: Upgrade my package listen: upgrade_my_package debug: msg: "Do whatever is needed to upgrade my package if ini files are different. Use an include_task module if needed"