--- - name: Find files everything except myrepo.repo ansible.builtin.find: paths: /etc/yum.repos.d file_type: file excludes: "myrepo.repo" register: output - name: Delete other files ansible.builtin.file: path: "{{ item.path }}" state: absent loop: "{{ output.files }}" - name: Check for marker line ansible.builtin.lineinfile: path: /etc/yum.repos.d/myrepo.repo create: true state: present line: "# BEGIN ANSIBLE MANAGED BLOCK" register: marker_check - name: Delete file if marker is absent ansible.builtin.file: path: /etc/yum.repos.d/myrepo.repo state: absent when: marker_check.changed