'Question about "scope" of a "when" in a "block" in an Ansible playbook
I am trying to modify an existing playbook that looks like:
---
## 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: "{{ play_mysql_dest_dir }}"
state: directory
group: root
owner: root
mode: 0755
.
.
.
## Delete mysql staging directory
- name: Delete mysql staging directory
file:
path: "{{ play_mysql_dest_dir }}"
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
I was wondering, to WHICH tasks does the "when" get applied to?
Also, if I want to eliminate that last task (the "Start tomcat, if not started" one), do I just move the "when" line to the end of the "Start mysql, if not started" task (just for this question I am commenting out those lines, to make the change more visible), e.g.:
---
## 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: "{{ play_mysql_dest_dir }}"
state: directory
group: root
owner: root
mode: 0755
.
.
.
## Delete mysql staging directory
- name: Delete mysql staging directory
file:
path: "{{ play_mysql_dest_dir }}"
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
I think that the intention of the original developer was that the "when" should apply to each/all of the tasks starting from the "-block", but I am wondering if the "indentation" of the "when" line is correct (or not)? I am fairly new to working with Ansible :(...
Thanks, Jim
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
