{"id":7911,"date":"2015-11-09T01:19:26","date_gmt":"2015-11-09T01:19:26","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/09\/ansible-delete-unmanaged-files-from-directory-open-source-projects-ansible-ansible\/"},"modified":"2015-11-09T01:19:26","modified_gmt":"2015-11-09T01:19:26","slug":"ansible-delete-unmanaged-files-from-directory-open-source-projects-ansible-ansible","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/09\/ansible-delete-unmanaged-files-from-directory-open-source-projects-ansible-ansible\/","title":{"rendered":"ansible &#8211; delete unmanaged files from directory?-open source projects ansible\/ansible"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/d24bf6020d9852832e65d7b6e1597aae?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>dalore<\/strong><\/p>\n<p>We do this with our nginx files, since we want them to be in a special order, come from templates, but remove unmanaged ones this works:<\/p>\n<pre><code>  # loop through the nginx sites array and create a conf for each file in order\n  # file will be name 01_file.conf, 02_file.conf etc\n  - name: nginx_sites conf\n    template: &gt;\n      src=templates\/nginx\/{{ item.1.template }}\n      dest={{ nginx_conf_dir }}\/{{ '%02d' % item.0 }}_{{ item.1.conf_name|default(item.1.template) }}\n      owner={{ user }}\n      group={{ group }}\n      mode=0660\n    with_indexed_items: nginx_sites\n    notify:\n      - restart nginx\n    register: nginx_sites_confs\n\n  # flatten and map the results into simple list\n  # unchanged files have attribute dest, changed have attribute path\n  - set_fact:\n      nginx_confs: \"{{ nginx_sites_confs.results|selectattr('dest', 'string')|map(attribute='dest')|list + nginx_sites_confs.results|selectattr('path', 'string')|map(attribute='path')|select|list }}\"\n    when: nginx_sites\n\n  # get contents of conf dir\n  - shell: ls -1 {{ nginx_conf_dir }}\/*.conf\n    register: contents\n    when: nginx_sites\n\n  # so we can delete the ones we don't manage\n  - name: empty old confs\n    file: path=\"{{ item }}\" state=absent\n    with_items: contents.stdout_lines\n    when: nginx_sites and item not in nginx_confs\n<\/code><\/pre>\n<p>The trick (as you can see) is that template and with_items have different attributes in the register results. Then you turn them into a list of files you manage and then get a list of the the directory and removed the ones not in that list.<\/p>\n<p>Could be done with less code if you already have a list of files. But in this case I&#8217;m creating an indexed list so need to create the list as well with map.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>dalore We do this with our nginx files, since we want them to be in a special order, come from templates, but remove unmanaged ones this works: # loop through the nginx sites array and create a conf for each file in order # file will be name 01_file.conf, 02_file.conf etc &#8211; name: nginx_sites conf [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-7911","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=7911"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/7911\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=7911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=7911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=7911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}