Using conditions in your code
In most of our examples so far, we have created simple sets of tasks that always run. However, as you generate tasks (whether in roles or playbooks) that you want to apply to a wider array of hosts, sooner or later, you will want to perform some kind of conditional action. This might be to only perform a task in response to the results of a previous task, or it might be to only perform a task in response to a specific fact gathered from a managed node. In this section, we will provide some practical examples of conditional logic to apply to your Ansible tasks to demonstrate how to use this feature.
As ever, we’ll need an inventory to get started, and we’ll reuse the inventory we have used throughout this chapter:
[frontends] web01.example.org https_port=8443 web02.example.org http_proxy=proxy.example.org [frontends:vars] ntp_server=ntp.web.example.org proxy=proxy.web.example.org [apps] app01.example.org app02.example.org [webapp:children...