Hi,

I do regularly have the problem that I want to manage directory
contents. To make things easier, we take an every day example such as
/etc/apt/sources.list.d on a Debian system. While I am aware that there
is a special apt module in ansible, I still chose to tackle this issue
"manually" because my ultimate goal is generic directory contents
management, not apt management. I chose apt as an example since that's
easier to reproduce for you.

I want:
  - files written in the directory by ansible
  - when a file ceases to be managed by ansible, the file should be
    removed. This is made easier by the fact that the ansible-managed
    files do match a certain glob, here: zda-*.
  - If a file doesn't change, it should not be touched. This includes
    that it is not desired that a file gets deleted and recreated with
    the same contents in the same ansible run.
  - Files placed into the directory manually by the local admin should
    remain untouched.

Here is my code:
==> ./site.yml <==
---
- name: apply common configuration
  hosts: all
  remote_user: mh
  become: "yes"
  roles:
    - common

- name: clean up after apt configuration
  hosts: apt_action_hosts
  remote_user: mh
  become: "yes"
  roles:
    - zzhandleapt
==> ./roles/common/tasks/main.yml <==
---
- name: repos
  import_tasks: repos.yml

==> ./roles/common/tasks/repos.yml <==
---
- name: search for sources.list.d files
  find:
    paths: "/etc/apt/sources.list.d"
    patterns: "zda-*.list"
  register: presentsourceslistfiles
- name: set up fact list for wanted sources.list.d entries
  set_fact:
    wantedsourceslistfiles:

- name: include repositories
  tags:
          repos
  include_tasks:
          "debian/sid/repos.yml"
==> ./roles/common/tasks/debian/sid/repos.yml <==
---
- name: create list of sid wantedsourceslistfiles
  set_fact:
    sidwantedsourceslistfiles:
      - /etc/apt/sources.list.d/zda-sid-mc.list
- name: add sid files to wantedsourceslistfiles
  set_fact:
    wantedsourceslistfiles: "{{ wantedsourceslistfiles }} + {{ 
sidwantedsourceslistfiles }}"
- name: zda-sid-mc.list
  tags:
  - repos
  - sid
  copy:
    dest: /etc/apt/sources.list.d/zda-sid-mc.list
    owner: root
    group: root
    mode: 0644
    content: |
            deb http://debian.debian.zugschlus.de/debian/ sid main contrib
  notify: apt update
==> ./roles/common/handlers/main.yml <==
---
- name: apt update
  add_host:
    name: "{{ inventory_hostname }}"
    groups: apt_action_hosts
==> ./roles/zzhandleapt/tasks/main.yml <==
---
- name: repos
  import_tasks: repos.yml

==> ./roles/zzhandleapt/tasks/repos.yml <==
---
- debug:
    msg: "wantedsourcelistfiles {{wantedsourceslistfiles}}"
- name: delete sources.list.d files
  file:
    path: "{{ item.path }}"
    state: absent
  with_items: "{{ presentsourceslistfiles.files }}"
  when: item.path not in wantedsourceslistfiles

- name: apt update
  command: apt update

==> ./hosts.yml <==
---
all:
  vars:
    dummy: dummy
  children:
    g_all:
      hosts:
        sid01:
        sid02:

The idea is the following:
  (1) build a list of all files that we might want to delete later
  (2) roll out the files that we actually want, build a list of those
      files
  (3) later iterate through list (1) and delete all files that are not
      in list (2).

Unfortunately, that does not seem to work. A possible reason might be
that the lists are not by-host, but per-ansible-run.

On both hosts, remove /etc/apt/sources.list.d/zda-sid-mc.list and touch
/etc/apt/sources.list.d/zda-stretch-mc.list. Expected is that ansible
removes the -stretch- files and places the -sid-files.

First ansible run:
[51/5036]mh@drop:~/git/zgansibletest (master * u+1) (crm) $ ansible-playbook 
--ask-become-pass --inventory=hosts.yml site.yml 
SUDO password: 

PLAY [apply common configuration] 
*******************************************************************************************************************

TASK [Gathering Facts] 
******************************************************************************************************************************
ok: [sid02]
ok: [sid01]

TASK [common : search for sources.list.d files] 
*****************************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : set up fact list for wanted sources.list.d entries] 
**********************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : include repositories] 
****************************************************************************************************************
included: /home/mh/git/zgansibletest/roles/common/tasks/debian/sid/repos.yml 
for sid01, sid02

TASK [common : create list of sid wantedsourceslistfiles] 
*******************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : add sid files to wantedsourceslistfiles] 
*********************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : zda-sid-mc.list] 
*********************************************************************************************************************
changed: [sid01]
changed: [sid02]

RUNNING HANDLER [common : apt update] 
***************************************************************************************************************
changed: [sid01]

PLAY [clean up after apt configuration] 
*************************************************************************************************************

TASK [Gathering Facts] 
******************************************************************************************************************************
ok: [sid01]

TASK [zzhandleapt : debug] 
**************************************************************************************************************************
ok: [sid01] => {
    "msg": "wantedsourcelistfiles  + 
[u'/etc/apt/sources.list.d/zda-sid-mc.list']"
}

TASK [zzhandleapt : delete sources.list.d files] 
****************************************************************************************************
changed: [sid01] => (item={u'uid': 0, u'woth': False, u'mtime': 
1522519373.161373, u'inode': 408337, u'isgid': False, u'size': 0, u'isuid': 
False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': 
False, u'islnk': False, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': 
u'/etc/apt/sources.list.d/zda-stretch-mc.list', u'xusr': False, u'atime': 
1522519416.4131262, u'isdir': False, u'ctime': 1522519373.161373, u'isblk': 
False, u'wgrp': False, u'xgrp': False, u'dev': 65025, u'roth': True, u'isfifo': 
False, u'mode': u'0644', u'rusr': True})                                

TASK [zzhandleapt : apt update] 
*********************************************************************************************************************
changed: [sid01]

PLAY RECAP 
******************************************************************************************************************************************
sid01                      : ok=12   changed=4    unreachable=0    failed=0   
sid02                      : ok=7    changed=1    unreachable=0    failed=0   

The first host is fine, but the -stretch- file is not removed on the second 
host.

Second ansible run:
[52/5037]mh@drop:~/git/zgansibletest (master * u+1) (crm) $ ansible-playbook 
--ask-become-pass --inventory=hosts.yml site.yml 
SUDO password: 

PLAY [apply common configuration] 
*******************************************************************************************************************

TASK [Gathering Facts] 
******************************************************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : search for sources.list.d files] 
*****************************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : set up fact list for wanted sources.list.d entries] 
**********************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : include repositories] 
****************************************************************************************************************
included: /home/mh/git/zgansibletest/roles/common/tasks/debian/sid/repos.yml 
for sid01, sid02

TASK [common : create list of sid wantedsourceslistfiles] 
*******************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : add sid files to wantedsourceslistfiles] 
*********************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : zda-sid-mc.list] 
*********************************************************************************************************************
ok: [sid01]
ok: [sid02]
 [WARNING]: Could not match supplied host pattern, ignoring: apt_action_hosts
PLAY [clean up after apt configuration] 
*************************************************************************************************************
skipping: no hosts matched

PLAY RECAP 
******************************************************************************************************************************************
sid01                      : ok=7    changed=0    unreachable=0    failed=0   
sid02                      : ok=7    changed=0    unreachable=0    failed=0   


This is an obvious no-op, with ansible not doing anything on the second host,
but I would have expected it to remove the -stretch- file anyway

To force things, remove the -sid- file on the second host and invoke third 
ansible run:
[53/5037]mh@drop:~/git/zgansibletest (master * u+1) (crm) $ ansible-playbook 
--ask-become-pass --inventory=hosts.yml site.yml 
SUDO password:

PLAY [apply common configuration] 
*******************************************************************************************************************

TASK [Gathering Facts] 
******************************************************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : search for sources.list.d files] 
*****************************************************************************************************
ok: [sid02]
ok: [sid01]

TASK [common : set up fact list for wanted sources.list.d entries] 
**********************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : include repositories] 
****************************************************************************************************************
included: /home/mh/git/zgansibletest/roles/common/tasks/debian/sid/repos.yml 
for sid01, sid02

TASK [common : create list of sid wantedsourceslistfiles] 
*******************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : add sid files to wantedsourceslistfiles] 
*********************************************************************************************
ok: [sid01]
ok: [sid02]

TASK [common : zda-sid-mc.list] 
*********************************************************************************************************************
ok: [sid01]
changed: [sid02]

RUNNING HANDLER [common : apt update] 
***************************************************************************************************************
changed: [sid02]

PLAY [clean up after apt configuration] 
*************************************************************************************************************

TASK [Gathering Facts] 
******************************************************************************************************************************
ok: [sid02]

TASK [zzhandleapt : debug] 
**************************************************************************************************************************
ok: [sid02] => {
    "msg": "wantedsourcelistfiles  + 
[u'/etc/apt/sources.list.d/zda-sid-mc.list']"
}

TASK [zzhandleapt : delete sources.list.d files] 
****************************************************************************************************
changed: [sid02] => (item={u'uid': 0, u'woth': False, u'mtime': 
1522519445.3628857, u'inode': 409536, u'isgid': False, u'size': 0, u'isuid': 
False, u'isreg': True, u'gid': 0, u'ischr': False, u'wusr': True, u'xoth': 
False, u'islnk': False, u'nlink': 1, u'issock': False, u'rgrp': True, u'path': 
u'/etc/apt/sources.list.d/zda-stretch-mc.list', u'xusr': False, u'atime': 
1522519449.9509423, u'isdir': False, u'ctime': 1522519445.3628857, u'isblk': 
False, u'wgrp': False, u'xgrp': False, u'dev': 65025, u'roth': True, u'isfifo': 
False, u'mode': u'0644', u'rusr': True})

TASK [zzhandleapt : apt update] 
*********************************************************************************************************************
changed: [sid02]

PLAY RECAP 
******************************************************************************************************************************************
sid01                      : ok=7    changed=0    unreachable=0    failed=0
sid02                      : ok=12   changed=4    unreachable=0    failed=0

Now, things are as I want them.

However, the way to get there is unsatisfactory. Can somebody explain
what is happening and tell me how to do things right? If inserting debug
code helps, I'll happily do this, but I do not yet know too well how to
do this, so please suggest pasteable things. Thanks in advance!

Greetings
Marc







-- 
-----------------------------------------------------------------------------
Marc Haber         | "I don't trust Computers. They | Mailadresse im Header
Leimen, Germany    |  lose things."    Winona Ryder | Fon: *49 6224 1600402
Nordisch by Nature |  How to make an American Quilt | Fax: *49 6224 1600421

-- 
You received this message because you are subscribed to the Google Groups 
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to ansible-project+unsubscr...@googlegroups.com.
To post to this group, send email to ansible-project@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/ansible-project/20180331183546.73hzhumfulqmoaje%40torres.zugschlus.de.
For more options, visit https://groups.google.com/d/optout.

Reply via email to