# remove files from host that are not in a dictionary

**URL:** <https://forum.ansible.com/t/remove-files-from-host-that-are-not-in-a-dictionary/23416>\
**Category:** Ansible Project\
**Created:** [January 10, 2017, 12:21pm UTC](https://forum.ansible.com/t/remove-files-from-host-that-are-not-in-a-dictionary/23416 "2017-01-10T12:21:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![mekonhead100](https://avatars.discourse-cdn.com/v4/letter/m/ce73a5/32.png) [@mekonhead100](https://forum.ansible.com/u/mekonhead100)\
**Post date:** [January 10, 2017, 12:21pm UTC](https://forum.ansible.com/t/remove-files-from-host-that-are-not-in-a-dictionary/23416/1 "2017-01-10T12:21:56Z")

</div>

I’m trying to remove config files from a host if they are no longer managed by ansible.

The ansible managed files are in a dictionary similar to the following :

config\_files:  
vhost01:  
servername: vhost01  
domain: [company.com](http://company.com)  
vhost02:  
servername: vhost02  
domain: [company.com](http://company.com)

And get added to the host as follows :

- name: Add ansible managed vhost config files.  
template:  
src: vhost.j2  
dest: “{{ vhost\_path }}/{{ item.key }}.conf”  
mode: 0644  
with\_dict: “{{ config\_files }}”

But when attempting to remove them with the following it does not work :

- name: Build list of ansible managed files.  
shell: echo “{{ item.key }}.conf”  
with\_dict: “{{ config\_files }}”  
register: managed\_files

- name: Build list of config files on host.  
shell: ls -1 “{{ vhost\_path }}”  
register: vhost\_config\_files

- name: Remove unmanaged vhost config files.  
file:  
path: “{{ vhost\_path }}/{{ item }}”  
state: absent  
with\_items: vhost\_config\_files.stdout\_lines  
when: item not in managed\_files.stdout\_lines

The error is as follows :

Unable to look up a name or access an attribute in template string ({% if item not in managed\_files.stdout\_lines %} True {% else %} False {% endif %})

Here is the output of managed\_files :

{“msg”: “All items completed”, “changed”: true, “results”: [{“\_ansible\_parsed”: true, “changed”: true, “stdout”: “vhost02.conf”, “\_ansible\_item\_result”: true, “warnings”: , “delta”: “0:00:00.002037”, “stdout\_lines”: [“vhost02.conf”], “end”: “2017-01-10 12:14:06.090689”, “\_ansible\_no\_log”: false, “start”: “2017-01-10 12:14:06.088652”, “cmd”: “echo "vhost02.conf"”, “item”: {“key”: “vhost02”, “value”: {“servername”: “vhost02”, “domain”: “[company.com](http://company.com)”}}, “stderr”: “”, “rc”: 0, “invocation”: {“module\_name”: “command”, “module\_args”: {“warn”: true, “executable”: null, “\_uses\_shell”: true, “\_raw\_params”: “echo "vhost02.conf"”, “removes”: null, “creates”: null, “chdir”: null}}}, {“\_ansible\_parsed”: true, “changed”: true, “stdout”: “vhost01.conf”, “\_ansible\_item\_result”: true, “warnings”: , “delta”: “0:00:00.002117”, “stdout\_lines”: [“vhost01.conf”], “end”: “2017-01-10 12:14:06.253953”, “\_ansible\_no\_log”: false, “start”: “2017-01-10 12:14:06.251836”, “cmd”: “echo "vhost01.conf"”, “item”: {“key”: “vhost01”, “value”: {“servername”: “vhost01”, “domain”: “[company.com](http://company.com)”}}, “stderr”: “”, “rc”: 0, “invocation”: {“module\_name”: “command”, “module\_args”: {“warn”: true, “executable”: null, “\_uses\_shell”: true, “\_raw\_params”: “echo "vhost01.conf"”, “removes”: null, “creates”: null, “chdir”: null}}}]}

Here is the output of vhost\_config\_files :

{“start”: “2017-01-10 12:14:06.895256”, “delta”: “0:00:00.002718”, “cmd”: “ls -1 "/etc/nginx/conf.d"”, “end”: “2017-01-10 12:14:06.897974”, “stderr”: “”, “stdout”: “dummy.conf\nvhost01.conf\nvhost02.conf\nvhost03.conf”, “stdout\_lines”: [“dummy.conf”, “vhost01.conf”, “vhost02.conf”, “vhost03.conf”], “changed”: true, “rc”: 0, “warnings”: }

I would expect it to remove dummy.conf and vhost03.conf from the host.

Any help on how to do this would be greatly appreciated.

I am running Ansible version 2.2.0.0

---

<div class="post-metadata">

**Author:** ![Kai\_Stian\_Olstad](https://avatars.discourse-cdn.com/v4/letter/k/e19adc/32.png) [@Kai\_Stian\_Olstad](https://forum.ansible.com/u/Kai_Stian_Olstad)\
**Post date:** [January 11, 2017, 10:28am UTC](https://forum.ansible.com/t/remove-files-from-host-that-are-not-in-a-dictionary/23416/2 "2017-01-11T10:28:34Z")

</div>

> I'm trying to remove config files from a host if they are no longer managed  
> by ansible.
> 
> The ansible managed files are in a dictionary similar to the following :
> 
> config\_files:  
> &nbsp;&nbsp;vhost01:  
> &nbsp;&nbsp;&nbsp;&nbsp;servername: vhost01  
> &nbsp;&nbsp;&nbsp;&nbsp;domain: company.com  
> &nbsp;&nbsp;vhost02:  
> &nbsp;&nbsp;&nbsp;&nbsp;servername: vhost02  
> &nbsp;&nbsp;&nbsp;&nbsp;domain: company.com
> 
> And get added to the host as follows :
> 
> - name: Add ansible managed vhost config files.  
> &nbsp;&nbsp;template:  
> &nbsp;&nbsp;&nbsp;&nbsp;src: vhost.j2  
> &nbsp;&nbsp;&nbsp;&nbsp;dest: "{{ vhost\_path }}/{{ item.key }}.conf"  
> &nbsp;&nbsp;&nbsp;&nbsp;mode: 0644  
> &nbsp;&nbsp;with\_dict: "{{ config\_files }}"
> 
> But when attempting to remove them with the following it does not work :
> 
> - name: Build list of ansible managed files.  
> &nbsp;&nbsp;shell: echo "{{ item.key }}.conf"  
> &nbsp;&nbsp;with\_dict: "{{ config\_files }}"  
> &nbsp;&nbsp;register: managed\_files
> 
> - name: Build list of config files on host.  
> &nbsp;&nbsp;shell: ls -1 "{{ vhost\_path }}"  
> &nbsp;&nbsp;register: vhost\_config\_files
> 
> - name: Remove unmanaged vhost config files.  
> &nbsp;&nbsp;file:  
> &nbsp;&nbsp;&nbsp;&nbsp;path: "{{ vhost\_path }}/{{ item }}"  
> &nbsp;&nbsp;&nbsp;&nbsp;state: absent  
> &nbsp;&nbsp;with\_items: vhost\_config\_files.stdout\_lines  
> &nbsp;&nbsp;when: item not in managed\_files.stdout\_lines

When you are using register and with\_dict together, all the result will be a list in managed\_files.results, one item in the list for every iteration.  
So the managed\_files.stdout\_lines doesn't exist.

> The error is as follows :
> 
> Unable to look up a name or access an attribute in template string ({% if  
> item not in managed\_files.stdout\_lines %} True {% else %} False {% endif %})
> 
> Here is the output of managed\_files :
> 
> {"msg": "All items completed", "changed": true, "results":  
> [{"\_ansible\_parsed": true, "changed": true, "stdout": "vhost02.conf",  
> "\_ansible\_item\_result": true, "warnings": , "delta": "0:00:00.002037",  
> "stdout\_lines": ["vhost02.conf"], "end": "2017-01-10 12:14:06.090689",  
> "\_ansible\_no\_log": false, "start": "2017-01-10 12:14:06.088652", "cmd":  
> "echo \"vhost02.conf\"", "item": {"key": "vhost02", "value": {"servername":  
> "vhost02", "domain": "company.com"}}, "stderr": "", "rc": 0, "invocation":  
> {"module\_name": "command", "module\_args": {"warn": true, "executable":  
> null, "\_uses\_shell": true, "\_raw\_params": "echo \"vhost02.conf\"",  
> "removes": null, "creates": null, "chdir": null}}}, {"\_ansible\_parsed":  
> true, "changed": true, "stdout": "vhost01.conf", "\_ansible\_item\_result":  
> true, "warnings": , "delta": "0:00:00.002117", "stdout\_lines":  
> ["vhost01.conf"], "end": "2017-01-10 12:14:06.253953", "\_ansible\_no\_log":  
> false, "start": "2017-01-10 12:14:06.251836", "cmd": "echo  
> \"vhost01.conf\"", "item": {"key": "vhost01", "value": {"servername":  
> "vhost01", "domain": "company.com"}}, "stderr": "", "rc": 0, "invocation":  
> {"module\_name": "command", "module\_args": {"warn": true, "executable":  
> null, "\_uses\_shell": true, "\_raw\_params": "echo \"vhost01.conf\"",  
> "removes": null, "creates": null, "chdir": null}}}]}
> 
> Here is the output of vhost\_config\_files :
> 
> {"start": "2017-01-10 12:14:06.895256", "delta": "0:00:00.002718", "cmd":  
> "ls -1 \"/etc/nginx/conf.d\"", "end": "2017-01-10 12:14:06.897974",  
> "stderr": "", "stdout":  
> "dummy.conf\nvhost01.conf\nvhost02.conf\nvhost03.conf", "stdout\_lines":  
> ["dummy.conf", "vhost01.conf", "vhost02.conf", "vhost03.conf"], "changed":  
> true, "rc": 0, "warnings": }

I recommend using  
- debug: var=managed\_files  
and  
- debug: var=vhost\_config\_files

Then you get it in more human readable format, and you can easily the structure of the variable.
