title: Question about "scope" of a "when" in a "block" in an Ansible playbook
body: <p>I am trying to modify an existing playbook that looks like:</p>
  <pre class="lang-yaml prettyprint-override"><code>---
  ## Capture currently installed mysql version
  - name: Capture currently installed mysql version
    shell: mysql -V
    register: current_mysql_version
    ignore_errors: yes
  
  - block:
    ## Stop running mysql instance
    - name: Stop running mysql instance
      service:
        name: mysqld
        state: stopped
        enabled: no
      ignore_errors: yes
  
    ## Create mysql stage directory
    - name: Create mysql stage directory
      file:
        path: &quot;{{ play_mysql_dest_dir }}&quot;
        state: directory
        group: root
        owner: root
        mode: 0755
  .
  .
  .
  
    ## Delete mysql staging directory
    - name: Delete mysql staging directory
      file:
        path: &quot;{{ play_mysql_dest_dir }}&quot;
        state: absent
  
    ## Start mysql
    - name: Start mysql, if not started
      service:
        name: mysqld
        state: started
  
    ## Start tomcat
    - name: Start tomcat, if not started
      service:
        name: tomcat
        state: started
    when: current_mysql_version.stderr is search('command not found') or not play_latest_mysql_version in current_mysql_version.stdout
  </code></pre>
  <p>I was wondering, to WHICH tasks does the &quot;when&quot; get applied to?</p>
  <p>Also, if I want to eliminate that last task (the &quot;Start tomcat, if not started&quot; one), do I just move the &quot;when&quot; line to the end of the &quot;Start mysql, if not started&quot; task (just for this question I am commenting out those lines, to make the change more visible), e.g.:</p>
  <pre class="lang-yaml prettyprint-override"><code>---
  ## Capture currently installed mysql version
  - name: Capture currently installed mysql version
    shell: mysql -V
    register: current_mysql_version
    ignore_errors: yes
  
  - block:
    ## Stop running mysql instance
    - name: Stop running mysql instance
      service:
        name: mysqld
        state: stopped
        enabled: no
      ignore_errors: yes
  
    ## Create mysql stage directory
    - name: Create mysql stage directory
      file:
        path: &quot;{{ play_mysql_dest_dir }}&quot;
        state: directory
        group: root
        owner: root
        mode: 0755
  .
  .
  .
  
    ## Delete mysql staging directory
    - name: Delete mysql staging directory
      file:
        path: &quot;{{ play_mysql_dest_dir }}&quot;
        state: absent
  
    ## Start mysql
    - name: Start mysql, if not started
      service:
        name: mysqld
        state: started
    when: current_mysql_version.stderr is search('command not found') or not play_latest_mysql_version in current_mysql_version.stdout
  
    ## Start tomcat
  #      - name: Start tomcat, if not started
  #        service:
  #          name: tomcat
  #          state: started
  #      when: current_mysql_version.stderr is search('command not found') or not play_latest_mysql_version in current_mysql_version.stdout
  </code></pre>
  <p>I think that the intention of the original developer was that the &quot;when&quot; should apply to each/all of the tasks starting from the &quot;-block&quot;, but I am wondering if the &quot;indentation&quot; of the &quot;when&quot; line is correct (or not)?  I am fairly new to working with Ansible :(...</p>
  <p>Thanks,
  Jim</p>