Register variable numerical comparisons

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 :slight_smile: 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

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

Ah, thanks Kahlil :slight_smile:

  • Gonzalo