Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

copy module: modify time on the file (mtime) is not updating #83013

Open
ShawnHardwick opened this issue Apr 10, 2024 · 3 comments
Open

copy module: modify time on the file (mtime) is not updating #83013

ShawnHardwick opened this issue Apr 10, 2024 · 3 comments
Labels
affects_2.17 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. module This issue/PR relates to a module. P3 Priority 3 - Approved, No Time Limitation

Comments

@ShawnHardwick
Copy link

ShawnHardwick commented Apr 10, 2024

Summary

Using the ansible.builtin.copy module to update the contents of a file that already exists is not updating the modify file descriptor on the file even though the file contents are updated.

This appears to have been introduced around 2.17:
2.16.5.post0 - works
2.17.0.dev0 - works
2.17.0b1.post0 - does not work
2.18.0.dev0 - does not work

# Works
ansible [core 2.16.5.post0]
  config file = /home/shawn.hardwick/.ansible.cfg
  configured module search path = ['/home/shawn.hardwick/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/shawn.hardwick/code/venv/ansible-2.16/lib/python3.10/site-packages/ansible
  ansible collection location = /home/shawn.hardwick/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/shawn.hardwick/code/venv/ansible-2.16/bin/ansible
  python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/home/shawn.hardwick/code/venv/ansible-2.16/bin/python3)
  jinja version = 3.1.3
  libyaml = True

Issue Type

Bug Report

Component Name

copy

Ansible Version

# Does not work
ansible [core 2.17.0b1.post0]
  config file = /home/shawn.hardwick/.ansible.cfg
  configured module search path = ['/home/shawn.hardwick/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/shawn.hardwick/code/venv/ansible-2.17/lib/python3.10/site-packages/ansible
  ansible collection location = /home/shawn.hardwick/.ansible/collections:/usr/share/ansible/collections
  executable location = /home/shawn.hardwick/code/venv/ansible-2.17/bin/ansible
  python version = 3.10.12 (main, Nov 20 2023, 15:14:05) [GCC 11.4.0] (/home/shawn.hardwick/code/venv/ansible-2.17/bin/python3)
  jinja version = 3.1.3
  libyaml = True

Configuration

# if using a version older than ansible-core 2.12 you should omit the '-t all'
$ ansible-config dump --only-changed -t all

OS / Environment

Ansible controller: Ubuntu 22.04.2 LTS
Ansible remote target: Ubuntu 20.04.6 LTS"

Steps to Reproduce

Create file on target-hostname, run below on target host
echo "Test content" >> /tmp/test

Collect stat data
stat /tmp/test

   File: /tmp/test
 Size: 13              Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 131153      Links: 1
Access: (0644/-rw-r--r--)  Uid: (1243051161/shawn.hardwick)   Gid: (1243000513/domain-users)
Access: 2024-04-10 17:16:20.363826238 +0000
Modify: 2024-04-10 17:16:20.363826238 +0000
Change: 2024-04-10 17:16:20.363826238 +0000
Birth: -

Execute ansible to update file contents
ansible all -i "target-hostname," -m ansible.builtin.copy -a "content=test2 dest=/tmp/test"
Note that the task is marked as changed:

target-hostname | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3.8"
    },
    "changed": true,
    "checksum": "109f4b3c50d7b0df729d299bc6f8e9ef9066971f",
    "dest": "/tmp/test",
    "gid": 1243000513,
    "group": "domain-users",
    "md5sum": "ad0234829205b9033196ba818f7a872b",
    "mode": "0644",
    "owner": "shawn.hardwick",
    "size": 5,
    "src": "/home/shawn.hardwick/.ansible/tmp/ansible-tmp-1712769550.823656-41798-135864879611184/.source",
    "state": "file",
    "uid": 1243051161
}

Compare stat data and note that modify (and even access) have not been updated

  File: /tmp/test
  Size: 5               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 136736      Links: 1
Access: (0644/-rw-r--r--)  Uid: (1243051161/shawn.hardwick)   Gid: (1243000513/domain-users)
Access: 2024-04-10 17:16:20.363826238 +0000
Modify: 2024-04-10 17:16:20.363826238 +0000
Change: 2024-04-10 17:19:13.088483862 +0000
 Birth: -

Expected Results

I expect modify and access to be updated via stat when using ansible.built.copy to update file contents:

  File: /tmp/test
  Size: 5               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 136736      Links: 1
Access: (0644/-rw-r--r--)  Uid: (1243051161/shawn.hardwick)   Gid: (1243000513/domain-users)
Access: 2024-04-10 17:19:13.088483862 +0000
Modify: 2024-04-10 17:19:13.088483862 +0000
Change: 2024-04-10 17:19:13.088483862 +0000
 Birth: -

Actual Results

`stat` returns that `modify` and `access` do not have updated timestamps.

  File: /tmp/test
  Size: 5               Blocks: 8          IO Block: 4096   regular file
Device: fd00h/64768d    Inode: 136736      Links: 1
Access: (0644/-rw-r--r--)  Uid: (1243051161/shawn.hardwick)   Gid: (1243000513/domain-users)
Access: 2024-04-10 17:16:20.363826238 +0000
Modify: 2024-04-10 17:16:20.363826238 +0000
Change: 2024-04-10 17:19:13.088483862 +0000
 Birth: -


### Code of Conduct

- [X] I agree to follow the Ansible Code of Conduct
@ansibot ansibot added bug This issue/PR relates to a bug. needs_triage Needs a first human triage before being processed. affects_2.16 module This issue/PR relates to a module. labels Apr 10, 2024
@ansibot
Copy link
Contributor

ansibot commented Apr 10, 2024

Files identified in the description:

If these files are incorrect, please update the component name section of the description or use the component bot command.

@sivel
Copy link
Member

sivel commented Apr 10, 2024

I think I see what is going on. Due to moving to shutil.copystat, we're preserving the atime and mtime of the dest also, which we probably shouldn't.

Can probably fix this by setting them in the dest_stat to int(time.time()).

@bcoca bcoca changed the title Modify file descriptor not updating when using copy module copy module: modify time on the file (mtime) is not updating Apr 11, 2024
@bcoca bcoca added P3 Priority 3 - Approved, No Time Limitation and removed needs_triage Needs a first human triage before being processed. affects_2.16 labels Apr 11, 2024
@dkuji
Copy link

dkuji commented May 7, 2024

Hi, I am new to contributing to OSS but would like to work on this issue.

@ansibot ansibot added the has_pr This issue has an associated PR. label May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects_2.17 bug This issue/PR relates to a bug. has_pr This issue has an associated PR. module This issue/PR relates to a module. P3 Priority 3 - Approved, No Time Limitation
Projects
None yet
Development

No branches or pull requests

5 participants