Conditional check failed

880 views
Skip to first unread message

plat00n

unread,
Jun 13, 2016, 9:19:36 AM6/13/16
to Ansible Project
Hello,

I'm posting here because bcoca told me do that ! ( I have already opened a ticket but bcoca has closed it >> LINK HERE << )

I hope someone will help me :) Thanks

##### ANSIBLE VERSION
ansible 2.1.0.0

##### OS / ENVIRONMENT
CentOS Linux release 7.2.1511 (Core)

##### SUMMARY
My roles don't work in 2.1.0.0 but in 1.9 it was ok

##### STEPS TO REPRODUCE
for exemple : create one role (my own mysql in the exemple)

set (only one) variable on the invfile (the inventory file)  :
role_mysql_version=5.7
OR
role_mysql_version=5.7

Create 3 files on the tasks folder

main.yml
```
---

- name: INCLUDE TASKS for MYSQL 5.6
  include: mysql_5.6_OFF.CentOS6.tasks.yml
  when: role_mysql_version == "5.6" and "{{ ansible_distribution }}{{ ansible_distribution_version.split('.')[0] }}" == "CentOS6"
  tags: tag_role_mysql

- name: INCLUDE TASKS for MYSQL 5.7
  include: mysql_5.7_OFF.CentOS7.tasks.yml
  when: role_mysql_version == "5.7" and "{{ ansible_distribution }}{{ ansible_distribution_major_version }}" == "CentOS7"
  tags: tag_role_mysql

- name: TEST CONCATENATE (for test)
  shell: i=`echo {{ ansible_distribution }}{{ ansible_distribution_major_version }}`; echo $i
  register: os_vers
  tags: tag_role_mysql

- name: INCLUDE TASKS for MYSQL 5.7 (test)
  include: mysql_5.7_OFF.CentOS7.tasks.yml
  when: role_mysql_version == '5.7' and {{ os_vers.stdout }} == 'CentOS7'
  tags: tag_role_mysql
```
mysql_5.6_OFF.CentOS6.tasks.yml

```
- name: COPY multiples confs as a previous conf (dated)
  become: yes
  shell: if (/usr/bin/test -e {{ item }}); then cp {{ item }} {{ item }}.`date +%Y%m%d`-`date +%H%M` ;fi
  with_items:
    - /etc/my.cnf
    - /var/lib/mysql/my.cnf
    - /var/lib/mysql/.bashrc
    - /var/lib/mysql/.bash_profile
```
mysql_5.7_OFF.CentOS7.tasks.yml

```
- name: COPY multiples confs as a previous conf (dated)
  become: yes
  shell: if (/usr/bin/test -e {{ item }}); then cp {{ item }} {{ item }}.`date +%Y%m%d`-`date +%H%M` ;fi
  with_items:
    - /etc/my.cnf
    - /var/lib/mysql/my.cnf
    - /var/lib/mysql/.bashrc
    - /var/lib/mysql/.bash_profile
```
Launch : ansible-playbook -i invfile role.yml -vvvv

##### EXPECTED RESULTS
Include file and play tasks from the included file

##### ACTUAL RESULTS

```
TASK [mysql : INCLUDE TASKS for MYSQL 5.6] *************************************
task path: /projet/data/myuser/ansible/roles/mysql/tasks/main.yml:3
skipping: [d1cedtest404] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true}

TASK [mysql : TEST CONCATENATE] ************************************************
task path: /projet/data/myuser/ansible/roles/mysql/tasks/main.yml:13
changed: [d1cedtest404] => {"changed": true, "cmd": "i=`echo CentOS7`; echo $i", "delta": "0:00:00.002989", "end": "2016-06-10 17:38:59.395801", "rc": 0, "start": "2016-06-10 17:38:59.392812", "stderr": "", "stdout": "CentOS7", "stdout_lines": ["CentOS7"], "warnings": []}

TASK [mysql : INCLUDE TASKS for MYSQL 5.7] *************************************
task path: /projet/data/myuser/ansible/roles/mysql/tasks/main.yml:18
skipping: [d1cedtest404] => {"changed": false, "skip_reason": "Conditional check failed", "skipped": true}

```
This lines were working on 1.9 , in 2.1.0 it's skipped :/ If you try to display :
{{ ansible_distribution }}{{ ansible_distribution_major_version }}
"{{ ansible_distribution }}{{ ansible_distribution_version.split('.')[0] }}"
role_mysql_version

This will return the correct value (for the conditionnals)

I tried this (unsuccesfully) :
```
#  when: role_mysql_version == 5.7 and
#  when: ( role_mysql_version == 5.7 ) and ( ansible_distribution ~ ansible_distribution_major_version  == CentOS7 )
#  when: ( ansible_distribution ~ ansible_distribution_major_version  == CentOS7 )
#  when: role_mysql_version == "5.7" and "{{ ansible_distribution }}{{ ansible_distribution_major_version }}" == "CentOS7"
#  when: role_mysql_version == 5.7 and {{ os_vers.stdout }} == CentOS7
```
Any idea ?

Thanks

plat00n

unread,
Jun 13, 2016, 11:00:41 AM6/13/16
to Ansible Project
Hello,

SOLUTION FOUND =>

For information :

in ver 1.9 , variables could be wrote in the inventory file like this : role_mysql_version=5.7
in ver 2.0 , variables must be writed like this : role_mysql_version="5.7"

BR

Brian Coca

unread,
Jun 13, 2016, 11:06:52 AM6/13/16
to ansible...@googlegroups.com
also your when: clauses are using unnecessary mustaches, which makes them subject to double interpolation bugs.

  when: role_mysql_version == "5.6" and ansible_distribution == "CentoOS" and ansible_distribution_version.split('.')[0] == 6

or more readable:

  when: 
    - role_mysql_version == "5.6"
    - ansible_distribution == "CentoOS"
    - ansible_distribution_major_version == "6"

----------
Brian Coca
Reply all
Reply to author
Forward
0 new messages