对于要从远程计算机复制到本地文件夹的文件夹,您有一个列表,并且并非所有文件夹都存在。
当找不到某个文件夹时,当前方法的确会在日志上生成嘈杂的错误错误:
- set_fact:
folders:
- /foo
- /bar
- synchronize:
dest: "{{ log_path }}"
mode: pull
src: "{{ item }}"
verify_host: true
ignore_errors: true
with_items: "{{ folders }}"
我要实现的一种行为是,当某些文件夹丢失时,rsync运行时不会出现任何错误。
当前的不良行为会产生一些噪音,例如:
rsync: change_dir "foo" failed: No such file or directory (2)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1650) [Receiver=3.1.2]
rsync: [Receiver] write error: Broken pipe (32)
我显然考虑过使用stat模块来验证文件夹是否确实存在,但是问题是我无法使用带有块的循环。
有人知道另一种干净的方法吗?
答案 0 :(得分:0)
我真的看不到使用stat
的问题所在。这是我在计算机上运行的测试手册,反映了我了解您正在尝试做的事情。
---
- name: Test selective log synchro
hosts: localhost
gather_facts: false
become: true
vars:
remote_log_path: /var/log
local_copy_path: /tmp
folder_names:
- containers
- cups
- IDoNotExist
tasks:
- name: Check which folders exist on remote
stat:
path: "{{ remote_log_path }}/{{ item }}"
register: log_folders_check
loop: "{{ folder_names }}"
- name: Debug registered var to see structure on -v
debug:
var: log_folders_check
verbosity: 1
- name: Create a list of existing folders only
set_fact:
existing_log_folders: "{{ log_folders_check.results | json_query('[?(stat.exists)][item][]') }}"
- name: Debug calculated list on -v
debug:
var: existing_log_folders
verbosity: 1
- name: Make sure local needed folders exist
file:
path: "{{ local_copy_path }}/{{ item }}"
state: directory
owner: root
group: root
loop: "{{ existing_log_folders }}"
- name: Sync existing folders locally
synchronize:
dest: "{{ local_copy_path }}/{{ item }}/"
src: "{{ remote_log_path }}/{{ item }}/"
mode: pull
with_items: "{{ existing_log_folders }}"
哪个给定(如果要查看已注册/设置的变量,请运行-v
PLAY [Test selective log synchro] *****************************************************************************************************************************************************************************************
TASK [Check which folders exist on remote] ********************************************************************************************************************************************************************************
ok: [localhost] => (item=containers)
ok: [localhost] => (item=cups)
ok: [localhost] => (item=IDoNotExist)
TASK [Debug registered var to see structure on -v] ************************************************************************************************************************************************************************
skipping: [localhost]
TASK [Create a list of existing folders only] *****************************************************************************************************************************************************************************
ok: [localhost]
TASK [Debug calculated list on -v] ****************************************************************************************************************************************************************************************
skipping: [localhost]
TASK [Make sure local needed folders exist] *******************************************************************************************************************************************************************************
ok: [localhost] => (item=containers)
ok: [localhost] => (item=cups)
TASK [Sync existing folders locally] **************************************************************************************************************************************************************************************
ok: [localhost] => (item=containers)
ok: [localhost] => (item=cups)
PLAY RECAP ****************************************************************************************************************************************************************************************************************
localhost : ok=4 changed=0 unreachable=0 failed=0 skipped=2 rescued=0 ignored=0