# Find all instances, and build a list

**URL:** <https://forum.ansible.com/t/find-all-instances-and-build-a-list/29183>\
**Category:** Ansible Project\
**Created:** [December 3, 2018, 11:33pm UTC](https://forum.ansible.com/t/find-all-instances-and-build-a-list/29183 "2018-12-03T23:33:41Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [December 3, 2018, 11:33pm UTC](https://forum.ansible.com/t/find-all-instances-and-build-a-list/29183/1 "2018-12-03T23:33:41Z")

</div>

I have a file, as an example, with the following content:

`disk = [file:/OVS/Repositories/0004fb0000030000df2ee11376f1cd94/VirtualDisks/b7558d7745c14b02806d08f8003906e1.img,xvda,w,phy:/OVS/Repositories/0004fb0000030000df2ee11376f1cd94/VirtualDisks/0004fb0000120000049ec392e534e039,xvdb,w,file:/OVS/Repositories/0004fb0000030000df2ee11376f1cd94/VirtualDisks/0004fb0000120000843bb7e2fe395f24.img,xvdc,w]`

I wish to find all instances of image files, and build a list with them. Here is my regexp:

`(\w+.img)`

Here is a regexp tester with all of the above:  
[https://regex101.com/r/UKcYIa/3](https://regex101.com/r/UKcYIa/3)

I know that I can do this with shell, or similar modules, but I wish to do this in a more ansible-native way (if there is one). Is there a module that will allow me to search a file with a regular expression and then register the results to a variable? I just want to build a list of \*img files (can be more than 1 and probably wont exceed 10) so that I can manipulate them later.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [December 3, 2018, 11:46pm UTC](https://forum.ansible.com/t/find-all-instances-and-build-a-list/29183/2 "2018-12-03T23:46:27Z")

</div>

I am trying the following, but it isn’t quite working like I hoped:

`

- name: Gather current virtual disks  
lineinfile:  
path: “{{ file }}”  
regexp: (\w+.img)  
line: ‘’  
check\_mode: yes  
register: virt\_disk

`

Result (debug of virt\_disk):

`  
ok: [myftpserver] =\> {  
“virt\_disk”: {  
“backup”: “”,  
“changed”: true,  
“diff”: [  
{  
“after”: “”,  
“after\_header”: “/var/ftp/vm.cfg (content)”,  
“before”: “”,  
“before\_header”: “/var/ftp/vm.cfg (content)”  
},  
{  
“after\_header”: “/var/ftp/vm.cfg (file attributes)”,  
“before\_header”: “/var/ftp/vm.cfg (file attributes)”  
}  
],  
“failed”: false,  
“msg”: “line replaced”  
}  
}

`

---

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [December 3, 2018, 11:48pm UTC](https://forum.ansible.com/t/find-all-instances-and-build-a-list/29183/3 "2018-12-03T23:48:23Z")

</div>

Hi  
Read the file contents with the ‘file’ lookup [https://docs.ansible.com/ansible/2.6/plugins/lookup/file.html](https://docs.ansible.com/ansible/2.6/plugins/lookup/file.html) then run regexp filter on the lookup result: [https://docs.ansible.com/ansible/2.5/user\_guide/playbooks\_filters.html#regular-expression-filters](https://docs.ansible.com/ansible/2.5/user_guide/playbooks_filters.html#regular-expression-filters)  
eg: “{{ lookup(‘file’, ‘/path/to/file’) | regex\_findall(‘(\w+.img)’) }}”

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [December 3, 2018, 11:50pm UTC](https://forum.ansible.com/t/find-all-instances-and-build-a-list/29183/4 "2018-12-03T23:50:04Z")

</div>

Great! Thank you.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [December 4, 2018, 5:21pm UTC](https://forum.ansible.com/t/find-all-instances-and-build-a-list/29183/5 "2018-12-04T17:21:23Z")

</div>

Perhaps I am doing this wrong, but it doesn’t like my syntax. If I escape the backslash (‘\w+.img’) it tries to run but with a different error:

`

- name: Find virtual Disks  
set\_fact:  
virt\_disks: “{{ lookup(‘file’,‘/var/ftp/vm.cfg’) | regex\_findall(‘\w+.img’) }}”  
`

Result:

`  
ERROR! Syntax Error while loading YAML.  
found unknown escape character

The error appears to have been in ‘/etc/ansible/playbooks/one-offs/sanitize\_vm.yml’: line 21, column 75, but may  
be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

set\_fact:  
virt\_disks: “{{ lookup(‘file’,‘/var/ftp/vm.cfg’) | regex\_findall(‘\w+.img’) }}”  
^ here  
We could be wrong, but this one looks like it might be an issue with  
missing quotes. Always quote template expression brackets when they  
start a value. For instance:

with\_items:

- {{ foo }}

Should be written as:

with\_items:

- “{{ foo }}”

`

If I escape the backslash:

`  
TASK [Find virtual Disks] \*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*  
[WARNING]: Unable to find ‘/var/ftp/vm.cfg’ in expected paths (use -vvvvv to see paths)

fatal: [ovmftp]: FAILED! =\> {“msg”: “An unhandled exception occurred while running the lookup plugin ‘file’. Error was a \<class ‘ansible.errors.AnsibleError’\>, original message: could not locate file in lookup: /var/ftp/vm.cfg”}

`

I find the error strange on the last one, because I stat the file previous to it and can see it fine.

---

<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:** [December 4, 2018, 5:32pm UTC](https://forum.ansible.com/t/find-all-instances-and-build-a-list/29183/6 "2018-12-04T17:32:20Z")

</div>

> Perhaps I am doing this wrong, but it doesn't like my syntax. If I escape  
> the backslash ('\\w+.img') it tries to run but with a different error:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Find virtual Disks  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;virt\_disks: "{{ lookup('file','/var/ftp/vm.cfg') |  
> regex\_findall('\w+.img') }}"
> 
> Result:  
> ERROR! Syntax Error while loading YAML.  
> &nbsp;&nbsp;found unknown escape character
> 
> The error appears to have been in  
> '/etc/ansible/playbooks/one-offs/sanitize\_vm.yml': line 21, column 75, but  
> may  
> be elsewhere in the file depending on the exact syntax problem.
> 
> The offending line appears to be:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;set\_fact:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;virt\_disks: "{{ lookup('file','/var/ftp/vm.cfg') | regex\_findall('\w+.img') }}"

\<snip /\>

> I find the error strange on the last one, because I stat the file previous  
> to it and can see it fine.

In your set\_fact, try to swap your single quotes to double quotes and you double to single quotes.

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [December 4, 2018, 6:02pm UTC](https://forum.ansible.com/t/find-all-instances-and-build-a-list/29183/7 "2018-12-04T18:02:13Z")

</div>

Now, whether escaped or not it says it can’t find the file. Do I need to delegate\_to in set\_fact? or should it be looking on the host specified?

---

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [December 4, 2018, 6:03pm UTC](https://forum.ansible.com/t/find-all-instances-and-build-a-list/29183/8 "2018-12-04T18:03:52Z")

</div>

John,

---

<div class="post-metadata">

**Author:** ![John\_Harmon](https://avatars.discourse-cdn.com/v4/letter/j/edb3f5/32.png) [@John\_Harmon](https://forum.ansible.com/u/John_Harmon)\
**Post date:** [December 4, 2018, 6:07pm UTC](https://forum.ansible.com/t/find-all-instances-and-build-a-list/29183/9 "2018-12-04T18:07:16Z")

</div>

Thx Piotr. That works. I was hoping to be able to do this without shell, but I haven’t been able to find another way around it either.

---

<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:** [December 4, 2018, 6:07pm UTC](https://forum.ansible.com/t/find-all-instances-and-build-a-list/29183/10 "2018-12-04T18:07:40Z")

</div>

lookup plugins run only on localhost not remote, if you file is on the host you can't use lookup.

---

<div class="post-metadata">

**Author:** ![powcarz](https://avatars.discourse-cdn.com/v4/letter/p/3be4f8/32.png) [@powcarz](https://forum.ansible.com/u/powcarz)\
**Post date:** [December 4, 2018, 6:10pm UTC](https://forum.ansible.com/t/find-all-instances-and-build-a-list/29183/11 "2018-12-04T18:10:14Z")

</div>

Yeah, I’ve turned a full circle… Sorry, but I don’t have a better solution…

Piotr

---

<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:** [December 4, 2018, 6:11pm UTC](https://forum.ansible.com/t/find-all-instances-and-build-a-list/29183/12 "2018-12-04T18:11:27Z")

</div>

A lot more code.  
First use the slurp module to fetch the content and store it to a variable.  
The result is base64 encode, so you need to use b64decode filer and then you can use the regex filter.
