Ansible - Check if multiple directories exist - if so run a script on each directory - How?
<p>Im creating a deployment playbook for our web services.  Each web service is in its own directory such as:</p>

<pre><code>/webapps/service-one/
/webapps/service-two/
/webapps/service-three/
</code></pre>

<p>I want to check to see if the service directory exists, and if so, I want to run a shell script that stops the service gracefully. Currently, I am able to complete this step by using <code>ignore_errors: yes</code>.</p>

<pre><code>- name: Stop services
  with_items: services_to_stop
  shell: "/webapps/scripts/stopService.sh {{item}}"
  ignore_errors: yes
</code></pre>

<p>While this works, the output is very messy if one of the directories doesnt exist or a service is being deployed for the first time.  I effectively want to something like one of these:</p>

<p>This: </p>

<pre><code>- name: Stop services
  with_items: services_to_stop
  shell: "/webapps/scripts/stopService.sh {{item}}"
  when: shell: [ -d /webapps/{{item}} ] 
</code></pre>

<p>or this:</p>

<pre><code>- name: Stop services
  with_items: services_to_stop
  shell: "/webapps/scripts/stopService.sh {{item}}"
  stat: 
    path: /webapps/{{item}}
  register: path
  when: path.stat.exists == True
</code></pre>