如果源文件和目标文件相同,是否可以反映副本的最新时间戳?
复制目标和源之前的时间戳:
目的地:
[wluser@myhost7 ~]$ ls -ltr /tmp/out
-rw-r--r-- 1 wluser aces 0 Nov 4 23:56 /tmp/out
来源:
[wluser@myhost7 ~]$ ls -ltr /home/wluser/out
-rw-r--r-- 1 wluser aces 0 Oct 12 05:06 /home/wluser/out
Ansible 剧本:
[wluser@myhost7 ~]$ cat copytry.yml
- hosts: localhost
tasks:
- name: Copy a file to another location
ansible.builtin.copy:
src: /tmp/out
dest: /home/wluser/out
force: yes
输出:
[wluser@myhost7 ~]$ ansible-playbook copytry.yml
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] **************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************
Saturday 04 November 2023 23:57:47 -0500 (0:00:00.048) 0:00:00.048 *****
ok: [localhost]
TASK [Copy a file to another location] ****************************************************************************************
Saturday 04 November 2023 23:57:50 -0500 (0:00:02.884) 0:00:02.933 *****
ok: [localhost]
PLAY RECAP ********************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Saturday 04 November 2023 23:57:51 -0500 (0:00:00.554) 0:00:03.487 *****
===============================================================================
Gathering Facts -------------------------------------------------------------------------------------------------------- 2.88s
Copy a file to another location ---------------------------------------------------------------------------------------- 0.55s
复制后时间戳:
[wluser@myhost7 ~]$ ls -ltr /home/wluser/out
-rw-r--r-- 1 wluser aces 0 Oct 12 05:06 out
我希望时间戳是最新的,即
Nov 4
即使src和dest文件相同。
我可以退回使用
raw
模块和 cp -p src dest
来获取目标文件的最新时间戳,但会丢失备份、文件权限和复制模块提供的其他属性。
你能推荐一下吗?
ansible.posix.synchronize
模块(在场景后面使用 rsync
)来保留时间戳。
- hosts: localhost
tasks:
- name: Copy a file to another location
ansible.posix.synchronize:
src: /tmp/out
dest: /home/wluser/out