您如何比较两个可取列表中的值?

发布于 2025-02-11 03:54:08 字数 994 浏览 3 评论 0原文

我在Ansible中有两个列表,可以使用查找模块

- name: "Find all files in {{ backup_path }}"
  ansible.builtin.find:
    paths:
      - "{{ backup_path }}"
    get_checksum: true
    recurse: yes
    depth: 2
    patterns: "{{ file_patterns }}"
  register: backup_files

- name: "Find all files in {{ new_path }}"
  ansible.builtin.find:
    paths:
      - "{{ new_path }}"
    get_checksum: true
    recurse: yes
    depth: 2
    patterns: "{{ file_patterns }}"
  register: new_files

,一旦我注册这些变量,我想在old_filesnew_files 列表。

- name: diff files between new and old
  ansible.builtin.shell: 
    cmd: "diff {{ item.0 }} {{ item.1 }}"
  register: diff_files
  loop: 
    - "{{ backup_files }}"
    - "{{ new_files }}"

我知道上面的片段是错误的,但是我想将0个项目从new_files与old_files n次数的0次数进行比较。

本剧本的目的是比较两个目录中的所有文件,并替换那些不同的文件。

I've got two lists in Ansible that I build on the fly using the find module

- name: "Find all files in {{ backup_path }}"
  ansible.builtin.find:
    paths:
      - "{{ backup_path }}"
    get_checksum: true
    recurse: yes
    depth: 2
    patterns: "{{ file_patterns }}"
  register: backup_files

- name: "Find all files in {{ new_path }}"
  ansible.builtin.find:
    paths:
      - "{{ new_path }}"
    get_checksum: true
    recurse: yes
    depth: 2
    patterns: "{{ file_patterns }}"
  register: new_files

Once I register those variables, I want to run a diff between the old_files and new_files lists.

- name: diff files between new and old
  ansible.builtin.shell: 
    cmd: "diff {{ item.0 }} {{ item.1 }}"
  register: diff_files
  loop: 
    - "{{ backup_files }}"
    - "{{ new_files }}"

I know the above snippet is wrong, but I want to compare the 0th item from new_files to 0th item from old_files n number of times.

The purpose of this playbook is to compare all the files in two directories and replace those that are different.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十二 2025-02-18 03:54:08

事实证明,我使这比需要的要复杂得多,这是更简单的工作。基本上,使用diff -rq \ path1 \ path2

- name: diff conf files between new and old
  ansible.builtin.shell: 
    cmd: "diff -rq {{ new_path }}conf {{ backup_path }}conf"
  register: diff_conf_files
  failed_when: diff_conf_files.rc not in [0,1]
  ignore_errors: yes

- name: diff bin files between new and old
  ansible.builtin.shell: 
    cmd: "diff -rq {{ new_path }}bin {{ backup_path }}bin"
  register: diff_bin_files
  failed_when: diff_bin_files.rc not in [0,1]
  ignore_errors: yes

Turns out I was making this far more complicated than it needed to be, here is what worked that was much simpler. Basically, use diff -rq \path1 \path2.

- name: diff conf files between new and old
  ansible.builtin.shell: 
    cmd: "diff -rq {{ new_path }}conf {{ backup_path }}conf"
  register: diff_conf_files
  failed_when: diff_conf_files.rc not in [0,1]
  ignore_errors: yes

- name: diff bin files between new and old
  ansible.builtin.shell: 
    cmd: "diff -rq {{ new_path }}bin {{ backup_path }}bin"
  register: diff_bin_files
  failed_when: diff_bin_files.rc not in [0,1]
  ignore_errors: yes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文