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

Remove file or directory with chattr in one task #79587

Open
1 task done
felipenazario opened this issue Dec 13, 2022 · 4 comments · May be fixed by #83111
Open
1 task done

Remove file or directory with chattr in one task #79587

felipenazario opened this issue Dec 13, 2022 · 4 comments · May be fixed by #83111
Labels
affects_2.11 bug This issue/PR relates to a bug. easyfix This issue is considered easy to fix by aspiring contributors. has_pr This issue has an associated PR. module This issue/PR relates to a module. verified This issue has been verified/reproduced by maintainer

Comments

@felipenazario
Copy link

felipenazario commented Dec 13, 2022

Summary

I'm trying to delete a directory that has the attribute with chattr +a, but I can not do it even specifing -a. I believe it could done in two tasks.

1 - Create directory with chattr +a
2 - Delete directory with chattr -a

Today is only possible in three tasks.

1 - Create directory with chattr +a
2 - Remove the attribute -a of the directory
3 - Delete directory

Since the module is able to create files or directories with attributes in one task, it should also be able to undo it.

Issue Type

Bug Report

Component Name

ansible.builtin.file

Ansible Version

ansible [core 2.11.12] 
  config file = /home/marcos/desenvolvimento/workspace_infra/InfraAsCode/ansible/linux/ansible.cfg
  configured module search path = ['/home/marcos/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.6/site-packages/ansible
  ansible collection location = /opt/ansible/collections:/home/marcos/desenvolvimento/workspace_infra/InfraAsCode/ansible/linux/collections
  executable location = /usr/bin/ansible
  python version = 3.6.15 (default, Sep 23 2021, 15:41:43) [GCC]
  jinja version = 3.0.3
  libyaml = True

Configuration

CALLBACKS_ENABLED(/home/marcos/desenvolvimento/workspace_infra/InfraAsCode/ansible/linux/ansible.cfg) = ['ansible.posix.timer', 'ansible.posix.profile_roles', 'ansible.posix.profile_tasks', 'ansible.builtin.oneline']
COLLECTIONS_PATHS(/home/marcos/desenvolvimento/workspace_infra/InfraAsCode/ansible/linux/ansible.cfg) = ['/opt/ansible/collections', '/home/marcos/desenvolvimento/workspace_infra/InfraAsCode/ansible/linux/collections']
DEFAULT_FORKS(/home/marcos/desenvolvimento/workspace_infra/InfraAsCode/ansible/linux/ansible.cfg) = 10
DEFAULT_HOST_LIST(/home/marcos/desenvolvimento/workspace_infra/InfraAsCode/ansible/linux/ansible.cfg) = ['/home/marcos/desenvolvimento/workspace_infra/InfraAsCode/ansible/linux/inventories/docker/hosts.ini']
DEFAULT_PRIVATE_ROLE_VARS(/home/marcos/desenvolvimento/workspace_infra/InfraAsCode/ansible/linux/ansible.cfg) = True
DEFAULT_ROLES_PATH(/home/marcos/desenvolvimento/workspace_infra/InfraAsCode/ansible/linux/ansible.cfg) = ['/opt/ansible/roles', '/home/marcos/desenvolvimento/workspace_infra/InfraAsCode/ansible/linux/roles']
DEFAULT_TIMEOUT(/home/marcos/desenvolvimento/workspace_infra/InfraAsCode/ansible/linux/ansible.cfg) = 15
DEPRECATION_WARNINGS(/home/marcos/desenvolvimento/workspace_infra/InfraAsCode/ansible/linux/ansible.cfg) = True
HOST_KEY_CHECKING(/home/marcos/desenvolvimento/workspace_infra/InfraAsCode/ansible/linux/ansible.cfg) = False
INTERPRETER_PYTHON(/home/marcos/desenvolvimento/workspace_infra/InfraAsCode/ansible/linux/ansible.cfg) = auto

OS / Environment

NAME="openSUSE Leap"
VERSION="15.3"
ID="opensuse-leap"
ID_LIKE="suse opensuse"
VERSION_ID="15.3"
PRETTY_NAME="openSUSE Leap 15.3"
ANSI_COLOR="0;32"
CPE_NAME="cpe:/o:opensuse:leap:15.3"
BUG_REPORT_URL="https://bugs.opensuse.org"
HOME_URL="https://www.opensuse.org/"

Steps to Reproduce

---
- hosts: all
  become: true
  gather_facts: true

  tasks:
    - name: 'Create directory xpto.'
      ansible.builtin.file:
        path: /root/xpto
        attributes: +a
        state: directory
        owner: "root"
        group: "root"
        mode: '755'

    - name: 'Remove directory xpto.'
      ansible.builtin.file:
        path: /root/xpto
        attributes: -a
        state: absent

Expected Results

PLAY [docker_managed_nodes] ******************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
Tuesday 13 December 2022  11:20:41 -0300 (0:00:00.028)       0:00:00.028 ****** 
Tuesday 13 December 2022  11:20:41 -0300 (0:00:00.028)       0:00:00.028 ****** 
ok: [ubuntu_22_nd_1]

TASK [Create directory xpto.] ****************************************************************************************************
Tuesday 13 December 2022  11:20:42 -0300 (0:00:01.807)       0:00:01.836 ****** 
Tuesday 13 December 2022  11:20:42 -0300 (0:00:01.807)       0:00:01.836 ****** 
changed: [ubuntu_22_nd_1]

TASK [Remove directory xpto.] ****************************************************************************************************
Tuesday 13 December 2022  11:20:44 -0300 (0:00:00.826)       0:00:03.602 ****** 
Tuesday 13 December 2022  11:20:44 -0300 (0:00:00.826)       0:00:03.602 ****** 
changed: [ubuntu_22_nd_1]

PLAY RECAP ***********************************************************************************************************************
ubuntu_22_nd_1             : ok=3    changed=2    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

Actual Results

PLAY [docker_managed_nodes] ******************************************************************************************************

TASK [Gathering Facts] ***********************************************************************************************************
Tuesday 13 December 2022  11:34:51 -0300 (0:00:00.024)       0:00:00.024 ****** 
Tuesday 13 December 2022  11:34:51 -0300 (0:00:00.024)       0:00:00.024 ****** 
ok: [ubuntu_22_nd_1]

TASK [Create directory xpto.] ****************************************************************************************************
Tuesday 13 December 2022  11:34:52 -0300 (0:00:01.670)       0:00:01.695 ****** 
Tuesday 13 December 2022  11:34:52 -0300 (0:00:01.670)       0:00:01.695 ****** 
changed: [ubuntu_22_nd_1]

TASK [Remove directory xpto.] ****************************************************************************************************
Tuesday 13 December 2022  11:34:53 -0300 (0:00:00.969)       0:00:02.665 ****** 
Tuesday 13 December 2022  11:34:53 -0300 (0:00:00.969)       0:00:02.665 ****** 
fatal: [ubuntu_22_nd_1]: FAILED! => {"changed": false, "msg": "rmtree failed: [Errno 1] Operation not permitted: '/root/xpto'"}

PLAY RECAP ***********************************************************************************************************************
ubuntu_22_nd_1             : ok=2    changed=1    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Code of Conduct

  • I agree to follow the Ansible Code of Conduct
@ansibot
Copy link
Contributor

ansibot commented Dec 13, 2022

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.

click here for bot help

@ansibot ansibot added affects_2.11 bug This issue/PR relates to a bug. module This issue/PR relates to a module. needs_triage Needs a first human triage before being processed. labels Dec 13, 2022
@mkrizek mkrizek added needs_verified This issue needs to be verified/reproduced by maintainer and removed needs_triage Needs a first human triage before being processed. labels Dec 13, 2022
@bcoca
Copy link
Member

bcoca commented Dec 13, 2022

note: the module is probably skipping all chattr/chwon/chmod operations when it sees state=absent

@s-hertel s-hertel added easyfix This issue is considered easy to fix by aspiring contributors. verified This issue has been verified/reproduced by maintainer and removed needs_verified This issue needs to be verified/reproduced by maintainer labels Dec 6, 2023
@pinradaThan
Copy link

Will try working on this in the upcoming week.

@pinradaThan
Copy link

pinradaThan commented Apr 6, 2024

The reason why the module can create files or directories with attributes in one task is because ensure_directory calls set_fs_attributes_if_different → set_attributes_if_different which will set the attribute of the directory to +a. This function is not called in ensure_absent (which handles deletions). Should we consider a way to add set_fs_attributes_if_different to ensure_absent or should we use a method of fixing this bug that is more specific to +a and -a attributes (i.e. checking in ensure_absent for the append-only attribute and removing it before deletion)?

pinradaThan added a commit to pinradaThan/ansible that referenced this issue Apr 20, 2024
@pinradaThan pinradaThan linked a pull request Apr 20, 2024 that will close this issue
@ansibot ansibot added the has_pr This issue has an associated PR. label Apr 20, 2024
pinradaThan added a commit to pinradaThan/ansible that referenced this issue Apr 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects_2.11 bug This issue/PR relates to a bug. easyfix This issue is considered easy to fix by aspiring contributors. has_pr This issue has an associated PR. module This issue/PR relates to a module. verified This issue has been verified/reproduced by maintainer
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants