Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionals inside a conditional role should not get processed #4074

Closed
yanivyalda opened this issue Sep 10, 2013 · 4 comments
Closed

Conditionals inside a conditional role should not get processed #4074

yanivyalda opened this issue Sep 10, 2013 · 4 comments
Labels
bug This issue/PR relates to a bug.

Comments

@yanivyalda
Copy link

I think i've found a problem with conditionals inside a role that is part of a playbook condtional. Here is my setup running on latest dev branch.

ansible-playbook --version
ansible-playbook 1.3 (devel 808d959) last updated 2013/09/10 10:53:29 (GMT +1100)

playbook: test.yml

---
- hosts: all
  user: ubuntu
  sudo: true

  vars:
    install_application: false

  vars_files:
    - group_vars/common.yml

  roles:
    - role: myapplication
      when: '$install_application == true'

myapplication role task main.yml

---

- debug: msg='Install myApplication? $install_application'

- name: Task 1
  command: "echo Task 1"
  register: task1_var

- debug: msg='Task1 ${task1_var.rc}'

- name: Task 2
  command: "echo Task 2"
  when: (task1_var.rc == 0)

running the ansible-playbook with following command

ansible-playbook test.yml -i hosts -e "install_application=true"
PLAY [all] ********************************************************************

TASK: [debug msg='Install myApplication? true'] *******************************
ok: [10.0.0.1] => {"item": "", "msg": "Install myApplication? true"}

TASK: [Task 1] ****************************************************************
changed: [10.0.0.1]

TASK: [debug msg='Task1 ${task1_var.rc}'] *************************************
ok: [10.0.0.1] => {"item": "", "msg": "Task1 0"}

TASK: [Task 2] ****************************************************************
changed: [10.0.0.1]

PLAY RECAP ********************************************************************
10.0.0.1               : ok=4    changed=2    unreachable=0    failed=0

Thats fine but running with false still processes Task 2 conditional

ansible-playbook test.yml -i hosts -e "install_application=false"

PLAY [all] ********************************************************************

TASK: [debug msg='Install myApplication? false'] ******************************
skipping: [10.0.0.1]

TASK: [Task 1] ****************************************************************
skipping: [10.0.0.1]

TASK: [debug msg='Task1 ${task1_var.rc}'] *************************************
skipping: [10.0.0.1]

TASK: [Task 2] ****************************************************************
fatal: [10.0.0.1] => error while evaluating conditional: {% if (task1_var.rc == 0) %} True {% else %} False {% endif %}

FATAL: all hosts have already failed -- aborting

I have tried
when: ${task1_var.changed} == true
but I would like to check the actual result

might be related to #3539

@mpdehaan
Copy link
Contributor

Can you please try changing this to:

when: install_application

and see if you have the same issue?

When using "when" statements there is no need for template evaluation, plus it's also nice to be using the new-style variables everywhere as the old forms will eventually be deprecated. I want to make sure it's not because of this style of evaluation that you're getting the false positive on the first comparison.

Thanks!

@yanivyalda
Copy link
Author

Hi Michael.

I didn't think I could use when: twice

so for Task 1 added
when: install_application
and Task 2
when: task1_var.rc == 0 //add my variable test here
when: install_application

All working fine

@srgvg
Copy link
Contributor

srgvg commented Sep 12, 2013

You can't functionally "repeat" when: statements; it's just the latter that wins.

@yanivyalda
Copy link
Author

aha so my first when never gets processed as when: install_application overrides it

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 6, 2018
@ansible ansible locked and limited conversation to collaborators Apr 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug This issue/PR relates to a bug.
Projects
None yet
Development

No branches or pull requests

4 participants