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

Fix changes status and return diff information when state=touch and c… #59341

Closed
wants to merge 4 commits into from

Conversation

DSNortsev
Copy link

@DSNortsev DSNortsev commented Jul 20, 2019

SUMMARY

Fixes #50452

  • Add check_model parameter to update_timestamp_for_file function and the file timestamp
    only updates when check_mode is set to False (default option)
  • Changed is set to TRUE if file does not exist or the access_time and modification_time
    option are not set to preserve
ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

file

ADDITIONAL INFORMATION

Playbook:

---
- hosts: localhost
  connection: local
  tasks:

  - name: Ensure file for specifications
    file:
      path: /tmp/test
      state: touch

Playbook when access_time and modification_time are preserve:

---
- hosts: localhost
  connection: local
  gather_facts: no
  tasks:
    - name: Ensure file for specifications
      file:
        path: /tmp/test
        state: touch
        access_time: preserve
        modification_time: preserve

Before Fix:

ok: [localhost] => {
    "changed": false,
    "dest": "/tmp/test",
    "gid": 1001,
    "group": "dmitry",
    "invocation"

After Fix if file does not exist:

changed: [localhost] => {
    "changed": true,
    "dest": "/tmp/test",
    "invocation": {
        "module_args":

After Fix if file exists:

changed: [localhost] => {
    "changed": true,
    "dest": "/tmp/test",
    "diff": {
        "after": {
            "atime": 1557243232.714623,
            "mtime": 1557243232.714623,
            "path": "/tmp/test",
            "state": "touch"
        },
        "before": {
            "atime": 1557243223.6023092,
            "mtime": 1557243223.6023092,
            "path": "/tmp/test",
            "state": "file"
        }
    }

After fix if acccess_time and modification_time are set to preserve:

    "changed": false,
    "dest": "/tmp/test",
    "gid": 1001,
    "group": "dmitry",
    "invocation":

@ansibot ansibot added affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. core_review In order to be merged, this PR must follow the core review workflow. files Files category module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. new_contributor This PR is the first contribution by a new community member. support:community This issue/PR relates to code supported by the Ansible community. support:core This issue/PR relates to code supported by the Ansible Engineering Team. labels Jul 20, 2019
@DSNortsev
Copy link
Author

@bcoca I accidentally closed my previous PR #59219 and was not able to re-open it. I had to create e new one. Also, I am planning to add integration tests.

@DSNortsev
Copy link
Author

DSNortsev commented Jul 21, 2019

@DSNortsev , would be cool to see integration tests
test/integration/targets/file/tasks/main.yml I saw file module tests there

@Andersson007, I added integration tests. Please take a look when you have a chance. Thanks

Copy link
Contributor

@Andersson007 Andersson007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, several cosmetic suggestions.
I can't take a deep review now, maybe later


- name: verify touch updates only mtime when atime is preserved
file: path={{output_dir}}/foo.txt state=touch access_time=preserve

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change


- name: verify touch updated only atime when mtime is preserved
file: path={{output_dir}}/foo.txt state=touch modification_time=preserve

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

@@ -0,0 +1,2 @@
bugfixes:
- file - Fix changed status and return diff information in file module when state=touch and check mode is set https://github.com/ansible/ansible/issues/50452
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- file - Fix changed status and return diff information in file module when state=touch and check mode is set https://github.com/ansible/ansible/issues/50452
- file - Fix changed status and return diff information in file module when state=touch and check mode is set (https://github.com/ansible/ansible/issues/50452)

@@ -0,0 +1,113 @@
# -*- coding: utf-8 -*-
# Copyright:
# (c) 2018 Ansible Project
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# (c) 2018 Ansible Project

@@ -0,0 +1,113 @@
# -*- coding: utf-8 -*-
# Copyright:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Copyright:
# Copyright: 2018 Ansible Project

@bcoca bcoca added P3 Priority 3 - Approved, No Time Limitation and removed needs_triage Needs a first human triage before being processed. labels Jul 23, 2019
@ansibot
Copy link
Contributor

ansibot commented Jul 31, 2019

The test ansible-test sanity --test future-import-boilerplate [explain] failed with 1 error:

test/units/modules/files/test_file.py:0:0: missing: from __future__ import (absolute_import, division, print_function)

click here for bot help

@ansibot ansibot added ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed core_review In order to be merged, this PR must follow the core review workflow. labels Jul 31, 2019
@DSNortsev DSNortsev force-pushed the touch_fix_new branch 2 times, most recently from 8bbe9b9 to 52b3bee Compare August 1, 2019 01:52
@ansibot ansibot added core_review In order to be merged, this PR must follow the core review workflow. and removed ci_verified Changes made in this PR are causing tests to fail. needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. labels Aug 1, 2019
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Aug 9, 2019
Dmitry Nortsev and others added 2 commits August 13, 2019 04:14
…heck_mode is set

Fixes ansible#50452

* Add check_model parameter to update_timestamp_for_file function and the file timestamp
  only updates when check_mode is set to False (default option)
* Changed is set to TRUE if file does not exist or the access_time and modification_time
  option are not set to preserve
* Add unittest to test touch module in check mode

Signed-off-by: Dmitry Nortsev <dmitry.nortsev@gmail.com>
@ansibot ansibot removed the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Aug 13, 2019
@ansibot ansibot added the stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. label Aug 29, 2019
@ansibot ansibot added needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. and removed core_review In order to be merged, this PR must follow the core review workflow. labels May 16, 2020
@Akasurde Akasurde requested a review from s-hertel June 18, 2020 08:27
@Akasurde
Copy link
Member

@DSNortsev Could you please rebase the PR using https://docs.ansible.com/ansible/latest/dev_guide/developing_rebasing.html? Thanks.

Copy link
Contributor

@s-hertel s-hertel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. lib/ansible/modules/files/file.py should now be lib/ansible/modules/file.py.

# parameters are not set to "preserve"
if prev_state == 'absent':
result['changed'] = True
elif module.params['access_time'] != 'preserve' or module.params['modification_time'] != 'preserve':
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is handled by update_timestamp_for_file. You can simplify this and just do an if/else

Suggested change
elif module.params['access_time'] != 'preserve' or module.params['modification_time'] != 'preserve':
else:

if prev_state == 'absent':
result['changed'] = True
elif module.params['access_time'] != 'preserve' or module.params['modification_time'] != 'preserve':
result['changed'] = True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove this and add it to the conditional below

Suggested change
result['changed'] = True

file_args = module.load_file_common_arguments(module.params)
# Attach diff to result only when the file exists
if update_timestamp_for_file(file_args['path'], mtime, atime, diff, check_mode=True):
result['diff'] = diff
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
result['diff'] = diff
result['diff'] = diff
result['changed'] = True

@Andersson007
Copy link
Contributor

@DSNortsev any updates on this?

@ansibot ansibot added pre_azp This PR was last tested before migration to Azure Pipelines. and removed stale_ci This PR has been tested by CI more than one week ago. Close and re-open this PR to get it retested. labels Dec 4, 2020
@ansibot ansibot removed the support:community This issue/PR relates to code supported by the Ansible community. label Mar 5, 2021
@sivel
Copy link
Member

sivel commented Nov 30, 2022

Resolved by #79422

@sivel sivel closed this Nov 30, 2022
@ansible ansible locked and limited conversation to collaborators Dec 7, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.9 This issue/PR affects Ansible v2.9 bug This issue/PR relates to a bug. files Files category module This issue/PR relates to a module. needs_rebase https://docs.ansible.com/ansible/devel/dev_guide/developing_rebasing.html needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR. new_contributor This PR is the first contribution by a new community member. P3 Priority 3 - Approved, No Time Limitation pre_azp This PR was last tested before migration to Azure Pipelines. support:core This issue/PR relates to code supported by the Ansible Engineering Team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

File module should show return a changed=true with check_mode=true and state=touch
8 participants