如果使用 ansible 复制模块源文件相同,则需要目标文件上的最新时间戳

问题描述 投票:0回答:1

如果源文件和目标文件相同,是否可以反映副本的最新时间戳?

复制目标和源之前的时间戳:

目的地:

[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 timestamp copy
1个回答
0
投票

您应该使用

ansible.posix.synchronize
模块(在场景后面使用
rsync
)来保留时间戳。

- hosts: localhost
  tasks:
    - name: Copy a file to another location
      ansible.posix.synchronize:
        src: /tmp/out
        dest: /home/wluser/out
© www.soinside.com 2019 - 2024. All rights reserved.