Find all instances, and build a list

50 views
Skip to first unread message

John Harmon

unread,
Dec 3, 2018, 6:33:41 PM12/3/18
to Ansible Project
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

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.

John Harmon

unread,
Dec 3, 2018, 6:46:27 PM12/3/18
to Ansible Project
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"
   
}
}


Piotr Owcarz

unread,
Dec 3, 2018, 6:48:43 PM12/3/18
to ansible...@googlegroups.com
Hi
eg: "{{ lookup('file', '/path/to/file') | regex_findall('(\w+.img)') }}"

Piotr



--
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-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/fca87b68-ce68-4c14-840c-ecfba3a7664b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Harmon

unread,
Dec 3, 2018, 6:50:05 PM12/3/18
to Ansible Project
Great!  Thank you.

John Harmon

unread,
Dec 4, 2018, 12:21:24 PM12/4/18
to Ansible Project
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. 

Kai Stian Olstad

unread,
Dec 4, 2018, 12:32:41 PM12/4/18
to ansible...@googlegroups.com
On Tuesday, 4 December 2018 18:21:23 CET John Harmon wrote:
> 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') }}"

<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.


--
Kai Stian Olstad


John Harmon

unread,
Dec 4, 2018, 1:02:13 PM12/4/18
to Ansible Project
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?

Piotr Owcarz

unread,
Dec 4, 2018, 1:04:15 PM12/4/18
to ansible...@googlegroups.com
John, 
I just realized, that lookups read files on the Ansible control machine. I assume you are searching that file on remote machine - then lookup won't work. If this is the case, read on...
I was researchnig a similar issue today, and couldn't came up with other solution than:
- shell: "grep -Eo '\\w+.img'  {{ file }}"
  register: out
- debug: var=out.stdout_lines

Piotr




--
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-proje...@googlegroups.com.
To post to this group, send email to ansible...@googlegroups.com.

John Harmon

unread,
Dec 4, 2018, 1:07:16 PM12/4/18
to Ansible Project
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.

Kai Stian Olstad

unread,
Dec 4, 2018, 1:07:57 PM12/4/18
to ansible...@googlegroups.com
On Tuesday, 4 December 2018 19:02:13 CET John Harmon wrote:
>
> On Tuesday, December 4, 2018 at 10:32:41 AM UTC-7, Kai Stian Olstad wrote:
> >
> > >
> > > 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.
> >
> >
> >
> >
> >
> 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?

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

--
Kai Stian Olstad


Piotr Owcarz

unread,
Dec 4, 2018, 1:10:35 PM12/4/18
to ansible...@googlegroups.com
Yeah, I've turned a full circle... Sorry, but I don't have a better solution...

Piotr




Kai Stian Olstad

unread,
Dec 4, 2018, 1:11:38 PM12/4/18
to ansible...@googlegroups.com
On Tuesday, 4 December 2018 19:07:16 CET John Harmon wrote:
> 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.

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.


--
Kai Stian Olstad


Reply all
Reply to author
Forward
0 new messages