Register variable numerical comparisons

4,599 views
Skip to first unread message

Gonzalo Servat

unread,
Nov 12, 2013, 12:50:53 AM11/12/13
to ansible-project
Hi All,

I've seen some examples of using the shell module with register, but I can't find any that allow a numerical comparison to be made on stdout. For example:

- hosts: all
  gather_facts: False
  tasks:
    - name: Get file count
      shell: ls /tmp | wc -l
      register: filecount
   
    - name: Show a debug message
      debug: msg="{{ inventory_hostname }}"
      when: filecount.stdout > 100

I am not trying to implement monitoring with Ansible :) It is a one-off operation and I'm trying to just print a message if the output from wc is > a number. I assume that because filecount.stdout is a string, the comparison fails, but how can I convert stdout to an integer?

- Gonzalo

Kahlil Hodgson

unread,
Nov 12, 2013, 12:52:41 AM11/12/13
to ansible...@googlegroups.com
try using the 'int' filter, i.e.,

when: filecount.stdout|int > 100



Kahlil (Kal) Hodgson GPG: C9A02289
Head of Technology (m) +61 (0) 4 2573 0382
DealMax Pty Ltd (w) +61 (0) 3 9008 5281

Suite 1415
401 Docklands Drive
Docklands VIC 3008 Australia

"All parts should go together without forcing. You must remember that
the parts you are reassembling were disassembled by you. Therefore,
if you can't get them together again, there must be a reason. By all
means, do not use a hammer." -- IBM maintenance manual, 1925
> --
> 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.
> For more options, visit https://groups.google.com/groups/opt_out.

Gonzalo Servat

unread,
Nov 12, 2013, 12:58:01 AM11/12/13
to ansible-project
Ah, thanks Kahlil :)

- Gonzalo
Reply all
Reply to author
Forward
0 new messages