From the course: Ansible for Automation Essential Training: Advanced Playbooks, Roles, and Diverse Hosts

Unlock this course with a free trial

Join today to access over 24,900 courses taught by industry experts.

Conditional failure with assert

Conditional failure with assert

- [Instructor] On occasion, it can be useful to place some checks in a playbook. Often it can be useful to have a playbook fail if certain conditions aren't met. Common conditions would be if a system doesn't have enough RAM or perhaps not enough hard drive space. This is done with the assert module. I'm going to start with a playbook named assert.yml. Notice at the top that I've commented out gather_facts false, so my playbook will reach out to my host and gather my variablized information. My first task here is just dumping out how much RAM my system has. The ansible_memtotal_mb variable is populated with gather_facts at run. I'll create an assert statement by checking if my system has more than a gig of RAM. So I'll start with a name assert if RAM is at least one gigabyte. Now the assert module, ansible.builtin.assert. Now for the assert parameters that do the work, that ansible_memtotal megabyte is greater than 1000. Now we'll create a message to display if the assert fails…

Contents