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.

Adding conditionals

Adding conditionals

- [Instructor] Conditionals are another feature I couldn't live without as they make frequent appearances in my playbooks. In essence, they are simple or compound IF statements. They're evaluated for each host being operated against, and if the condition isn't found to be true, the host is skipped for this task. The Win Task Level Parameter is used to designate a conditional. After that comes the condition in the form of value 1 compared in some way to value 2. As in when x > 2. As shown, you can use standard mathematical operators like >, <, =, and !=. You can also do the written out version of these with no spaces in the words as in greaterthan, lessthan, or equalto. I've duplicated the previous loop playbook and named it conditionals.yml. I'm going to add a membership conditional which checks if a value exists in a list. I'll change my tasks so that it will only add users that aren't designated to be ignored. Up in the vars section, I'll add a variable. And I'll name it…

Contents