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

Issue with the success/succeeded filter #25027

Closed
jainnikhil30 opened this issue May 25, 2017 · 6 comments
Closed

Issue with the success/succeeded filter #25027

jainnikhil30 opened this issue May 25, 2017 · 6 comments
Assignees
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug. c:plugins/filter

Comments

@jainnikhil30
Copy link
Contributor

jainnikhil30 commented May 25, 2017

ISSUE TYPE
  • Bug Report
COMPONENT NAME

Success filter

ANSIBLE VERSION
2.3.0.0
CONFIGURATION
OS / ENVIRONMENT
SUMMARY

Success filter not working as expected. So if there is a task1 which is skipped and if we have a task2 which has a filter as when: result|success after the skipped task1, task2 still runs. Although theoretically task1 was skipped so result should have "skipped" value and hence task2 should either error out or should be skipped. Attaching the playbook for better understanding.

STEPS TO REPRODUCE
--
- hosts: localhost
  tasks:
    - name: Run a ps command
      shell: ps -ef | grep fred | grep -v grep
      ignore_errors: yes
      register: result
      when: ansible_distribution == 'CentOS'
    - debug: var=result

    - name: if rc zero then run this task
      shell: echo "rc zero"
      register: result2
      ignore_errors: yes
      when: result.rc == 0
    - debug: var=result2


    - name: if success then run this task
      shell: echo "it works"
      register: result4
      when: result|succeeded
    - debug: var=result4
EXPECTED RESULTS

If the first task1 " Run a ps command" is skipped
task2 "if rc zero then run this task" and task3 "if success then run this task" should error out or should be skipped because of the when condition.

ACTUAL RESULTS

task3 "if success then run this task" run successfully.

PLAY [localhost] ***************************************************************12:30:04


TASK [Gathering Facts] *********************************************************12:30:04

ok: [localhost]


TASK [Run a ps command] ********************************************************12:30:05

skipping: [localhost]


TASK [debug] *******************************************************************12:30:05

ok: [localhost] => {

    "changed": false, 

    "result": {

        "changed": false, 

        "skip_reason": "Conditional result was False", 

        "skipped": true

    }

}


TASK [if rc zero then run this task] *******************************************12:30:05

fatal: [localhost]: FAILED! => {"failed": true, "msg": "The conditional check 'result.rc == 0' failed. The error was: error while evaluating conditional (result.rc == 0): 'dict object' has no attribute 'rc'\n\nThe error appears to have been in '/var/lib/awx/projects/andyc/rc_test.yml': line 11, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: if rc zero then run this task\n      ^ here\n"}

...ignoring


TASK [debug] *******************************************************************12:30:06

ok: [localhost] => {

    "changed": false, 

    "result2": {

        "failed": true, 

        "msg": "The conditional check 'result.rc == 0' failed. The error was: error while evaluating conditional (result.rc == 0): 'dict object' has no attribute 'rc'\n\nThe error appears to have been in '/var/lib/awx/projects/andyc/rc_test.yml': line 11, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n    - name: if rc zero then run this task\n      ^ here\n"

    }

}


TASK [if success then run this task] *******************************************12:30:06

changed: [localhost]


TASK [debug] *******************************************************************12:30:06

ok: [localhost] => {

    "changed": false, 

    "result4": {

        "changed": true, 

        "cmd": "echo \"it works\"", 

        "delta": "0:00:00.007862", 

        "end": "2017-05-24 12:30:06.927419", 

        "rc": 0, 

        "start": "2017-05-24 12:30:06.919557", 

        "stderr": "", 

        "stderr_lines": [], 

        "stdout": "it works", 

        "stdout_lines": [

            "it works"

        ]

    }

}


PLAY RECAP *********************************************************************12:30:07

localhost                  : ok=6    changed=1    unreachable=0    failed=0   
@ansibot ansibot added affects_2.3 This issue/PR affects Ansible v2.3 bug_report c:plugins/filter needs_triage Needs a first human triage before being processed. labels May 25, 2017
@bcoca
Copy link
Member

bcoca commented May 25, 2017

success is currently defined as 'not failed'

@Shaps
Copy link
Contributor

Shaps commented May 26, 2017

@jainnikhil30 adding and not result|skipped to your last condition will make the task skip also if the first task was skipped

@bcoca bcoca self-assigned this May 31, 2017
@bcoca bcoca removed the needs_triage Needs a first human triage before being processed. label May 31, 2017
@bcoca
Copy link
Member

bcoca commented May 31, 2017

Possible Misunderstanding

Hi!

Thanks very much for your submission to Ansible. It sincerely means a lot to us.

We believe the ticket you have filed is being somewhat misunderstood, as one thing works a little differently than stated.

The filter is working as designed, skipped status is a 'form of success' as either the condition was not met so the task 'worked as expected' or the module itself forced a skip as requirements were not met in a 'non failure' way (check mode).

In the future, this might be a topic more well suited for the user list, which you can also post here if you'd like some more help with the above.

Thank you once again for this and your interest in Ansible!

@bcoca bcoca closed this as completed May 31, 2017
@jctanner
Copy link
Contributor

The "success" test code is defined in https://github.com/ansible/ansible/blob/devel/lib/ansible/plugins/test/core.py#L37-L39

def success(result):
    ''' Test if task result yields success '''
    return not failed(result)

@DylanYoung
Copy link

What about a completed filter that does as the OP was expecting? It seems more useful than saving 4 characters over not result|failed, no?

@sblaisot
Copy link

sblaisot commented Jan 5, 2018

The filter is working as designed

This is not what have been stated back in 2015 in #13161

@ansibot ansibot added bug This issue/PR relates to a bug. and removed bug_report labels Mar 7, 2018
@ansible ansible locked and limited conversation to collaborators Apr 26, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
affects_2.3 This issue/PR affects Ansible v2.3 bug This issue/PR relates to a bug. c:plugins/filter
Projects
None yet
Development

No branches or pull requests

7 participants