Conditional control structures allow Ansible to follow an alternate path, skip a task, or select a specific file to import based on certain conditions. In a generic programming language, this is done with the help of if-then
, else if
, else
, case
statements. Ansible does this using the "when
" statement. Some example conditions are:
Whether a certain variable is defined
Whether an earlier command sequence is successful
Whether the task has run before
Whether a platform on a target node matches the supported platforms
Whether a certain file exists
We have already used the when
statement to extract the WordPress archive based on the result of another command, which is:
- name: download wordpress register: wp_download - name: extract wordpress when: wp_download.rc == 0
This would be vaguely equivalent to writing a shell snippet, as follows:
DOWNLOAD_WORDPRESS var=`echo $? if [$var -eq 0] then EXTRACT_WORDPRESS() fi
In addition to checking...