Ansible 比较 INI 本地与远程

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。

答案1

只是为了在我发表评论后提供大局。复制文件并通知处理程序(如果发生更改)。或者,您可以在任务上注册一个变量并进行检查,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"

答案2

我最终成功地完成了这个工作,并想分享我使用的代码......

- 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

相关内容