3

I have to check different hardware and configurations elements on Linux machines using ansible, and I am not sure at all about how to do it (RAM, disk space, DNS, CPU...), I've understood that I can find nearly all I want in the ansible facts, but I do not understand how I can use it.

For example, I have to check if the RAM amount is at least of 4GB and have an alarm if not, so I tried many things, and... nothing works.

Here is an example of what I tried.

 - hosts: client
   remote_user: user

  tasks:
      - debug: var=ansible_memory_mb
      - debug: msg="total RAM is {{ ansible_memory_mb.real.total }}"
      - fail: msg="not enough RAM"t
      - when: {{ ansible_memory_mb.real.total }} < 4096

Could you tell me how it works ? and maybe there is a better way to do what I want using Ansible ?

Thank you for your answer.

Matthew
  • 459
  • 2
  • 5
  • 16

1 Answers1

14

There are a few things wrong with the snippet you posted.

  • Your indentation is off. tasks needs to be at the same indentation level as hosts.

  • The when condition needs to be part of the fail task block, not a separate list item.

  • In general, you do not need to use {{ ... }} in a when condition, the entire expression will be treated as a Jinja template.

Try this:

- hosts: client
  remote_user: user
  tasks:
    - debug: var=ansible_memory_mb
    - debug: msg="total RAM is {{ ansible_memory_mb.real.total }}"
    - fail: msg="not enough RAM"
      when: ansible_memory_mb.real.total < 4096

You can also use the assert module to check a condition or list of conditions.

- assert:
    that:
      - ansible_memory_mb.real.total >= 4096
      - some other condition
      - ...
Gergely Lukacsy
  • 2,881
  • 2
  • 23
  • 28
augurar
  • 12,081
  • 6
  • 50
  • 65
  • So it is how it works ! (and yes, your code works) Sorry for the indentation, it's only a failed copy/paste. I tried assert module, it's a seductive way to do what I want but fail/when look more adapted in what I want. For the moment I will continue my tests for all the things I want to check, but I thing I begin to understand how Ansible works. Thank you ! – Matthew Jun 26 '16 at 12:20