![](/img/trans.png)
[英]How to compare or lookup specific values from two lists in ansible and print
[英]How do you compare values in two Ansible lists?
我在 Ansible 中有兩個列表,我使用find
模塊即時構建
- 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_files
和new_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 }}"
我知道上面的代碼片段是錯誤的,但我想將 new_files 中的第 0 項與 old_files 中的第 0 項進行 n 次比較。
本 playbook 的目的是比較兩個目錄中的所有文件並替換不同的文件。
事實證明,我讓這變得比它需要的復雜得多,這是更簡單的工作。 基本上,使用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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.