Regarding not to proceed further if ansible task fails

124 views
Skip to first unread message

Ashok Reddy

unread,
Sep 5, 2023, 11:23:11 PM9/5/23
to ansible...@googlegroups.com
Hi All, 

When I tried to download packages from jfrog repository, If it gets downloaded, it is okay. But if it is unable to download, we should not proceed further to execute other tasks. 

How to write ansible task? 

If we use register and based on the condition, shall we stop to expect tasks?

Can you please help me?

David Logan

unread,
Sep 6, 2023, 5:56:27 AM9/6/23
to ansible...@googlegroups.com
Hi Ashok,


There are some good ideas on how to halt and abort a playbook if errors occur

Regards

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAKRLcHhW59QN_ek7%2BZegO2af7ChoM9dxpA_sLbx1-Lxy%3DEghQA%40mail.gmail.com.


--
if in trouble, or in doubt
run in circles, scream and shout

Ashok Reddy

unread,
Sep 6, 2023, 10:15:50 AM9/6/23
to ansible...@googlegroups.com
Hi , 

Can someone help me?

Brian Coca

unread,
Sep 6, 2023, 10:35:08 AM9/6/23
to ansible...@googlegroups.com
This is the default behavior, if task #1 fails, subsequent tasks will
not run for that host. In the previous response they point you on how
to continue on error, the implication being the default is to stop on
error.

--
----------
Brian Coca

Ashok Reddy

unread,
Sep 6, 2023, 7:47:37 PM9/6/23
to ansible...@googlegroups.com
Hi Brian

Noted and Thanks for your response.
 But, when we tried to download packages, the package is not available and then the task fails .
  
The next tasks also executed and It does not come out of the script.

In that case, I have written a task to verify whether file exists or not and then any_fail_on_error: true .

Is it correct?

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Dick Visser

unread,
Sep 6, 2023, 7:53:08 PM9/6/23
to ansible...@googlegroups.com
Hii

What you describe is nonstandard behaviour. 
Instead of guessing, please show us your playbook and any other relevant code.


Sent from Gmail Mobile


Ashok Reddy

unread,
Sep 8, 2023, 2:49:09 AM9/8/23
to ansible...@googlegroups.com

Hi , 

The following script, which only skip the task and execute further: 

 
But, What I did : If the file doesn’t exist, it cannot proceed to execute tasks and cone out ofcthe script:

Is it correct?

Can someone help me with this?

Ashok Reddy

unread,
Sep 10, 2023, 11:51:00 PM9/10/23
to ansible...@googlegroups.com
Hi All, 

Can you please help?

Todd Lewis

unread,
Sep 11, 2023, 11:37:42 AM9/11/23
to Ansible Project
I don't wish to defend the horrid things that Google Groups does to pasted text, particularly where whitespace matters.

However, photos of text is about useless. For example, there's no practical way to run a linter on pictures of text. It's impossible to say with certainty that a task in a clipped photo is correct.

You've been working with these tasks now for almost a week. Is there any behavior you see that makes you think they don't do what you expect? If so, then copy-and-past (not screenshots!) the evidence from the run log, include the relevant tasks, data, etc., and explain what you expect contrasted with what is actually happening.
Otherwise, yes, it is correct! (?)

Ashok Reddy

unread,
Sep 11, 2023, 7:54:10 PM9/11/23
to ansible...@googlegroups.com
Hi , 

I have tried but it didn’t stop when task fails.

If we use failed_when: true, it can work.

Can you please help?

Ashok Reddy

unread,
Sep 11, 2023, 11:09:48 PM9/11/23
to Ansible Project, uto...@gmail.com
Hi Brian, 

The logs are: 


Even if the task fails, it can proceed to execute further tasks . But don’t want that.

What I am trying to achieve is If the task fails, it will not proceed further to execute tasks and come out of the script 



On Tue, 12 Sep 2023 at 9:58 AM, Todd Lewis <uto...@gmail.com> wrote:
[private message; off-list]

Ashok,

I don't know how much more plainly I can say this.

Post (copy and paste) the relevant tasks.

Post the job log. Run it with two "v"s — ansible-playbook -vv …

There are billions of ways things can fail. Do you expect us to know why yours failed when you've provided nothing more than the assertion that it didn't do what you thought it would?

Give us the job log and the playbook, and we will try to help you. Without those, we'd be wasting our time and yours with baseless guessing.
--
Todd

David Logan

unread,
Sep 12, 2023, 12:24:52 AM9/12/23
to ansible...@googlegroups.com
From the documentation

By default, Ansible continues to execute tasks as long as there are hosts that have not yet failed. You might like to set a maximum failure percentage or similar.

Do you have this running on multiple hosts? or is it on a single host?


Regards
David

John Petro

unread,
Sep 12, 2023, 8:38:23 AM9/12/23
to ansible...@googlegroups.com
I don't know if this was mentioned at all, but have you looked at the meta module?  I used it to break out of a play when something failed.


--John

Brian Coca

unread,
Sep 12, 2023, 10:13:24 AM9/12/23
to ansible...@googlegroups.com
from the output you have EXPLICIT ignores to the failures, that is why Ansible is continuing to more tasks, remove the ignores if you don't want the failure to be ignored.

--
----------
Brian Coca

Ashok Reddy

unread,
Sep 13, 2023, 3:43:54 AM9/13/23
to ansible...@googlegroups.com
Hi Brian, 

Did not configure the ignore_errors in playbook

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Ashok Reddy

unread,
Sep 13, 2023, 3:46:48 AM9/13/23
to ansible...@googlegroups.com

Ashok Reddy

unread,
Sep 13, 2023, 3:47:21 AM9/13/23
to ansible...@googlegroups.com

Brian Coca

unread,
Sep 13, 2023, 10:05:45 AM9/13/23
to ansible...@googlegroups.com
Not in the part you are showing, but the output you showed has `ignoring ..` which you only get with that keyword.

--
----------
Brian Coca

Dick Visser

unread,
Sep 13, 2023, 10:14:28 AM9/13/23
to ansible...@googlegroups.com
On Wed, 13 Sept 2023 at 09:43, Ashok Reddy <ashok...@gmail.com> wrote:
Hi Brian, 

Did not configure the ignore_errors in playbook

You didn't show any playbook so far, just (fuzzy pictures of computer screens with) task files that are included.
If you can post the TEXTUAL version of the playbook that includes them.
Please do not post any more photos of computer screens.... just the actual text.
So: no pictures - just text.


Ashok Reddy

unread,
Sep 13, 2023, 9:25:34 PM9/13/23
to ansible...@googlegroups.com
Hi All,

PFA for the ansible playbook


--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
deploy_tasks_main (1).yml

Todd Lewis

unread,
Sep 13, 2023, 11:42:42 PM9/13/23
to Ansible Project
That's a task file, not a playbook. But at least you've quit posting pictures of your screen. The problem does not appear to be in that file.

Of the 5 things I asked for, you've now done the third one.
  1. the command line you use to invoke this playbook
  2. the playbook itself
  3. the task file that is being pulled in from the playbook (please, not another picture of it!)
  4. the job log (copy-and-paste, not a picture of part of it!)
  5. output from the following command:
    ansible-config dump --only-changed

(BTW, line 95 has a quoting problem. You probably want to single-quote the outer string so the inner double quotes do what you expect. This is not the problem you've been posting about though. It was made obvious by running ansible-lint on that task file.)

Ashok Reddy

unread,
Sep 18, 2023, 1:25:27 AM9/18/23
to ansible...@googlegroups.com
Hi Todd, 

Thanks for your help.  
I will send you 

Ashok Reddy

unread,
Sep 18, 2023, 1:52:18 AM9/18/23
to ansible...@googlegroups.com
Hi John, 

Thanks for providing your inputs. Let me try

Ashok Reddy

unread,
Sep 18, 2023, 3:03:20 AM9/18/23
to ansible...@googlegroups.com
Hi John, 

I have configured the meta_play: end_play in roles where the task has failed but it will not exit . 
Can you please guide me?

For example, if any task fails, it will come out the playbook and not to proceed further to execute tasks.  

Dick Visser

unread,
Sep 18, 2023, 4:02:23 AM9/18/23
to ansible...@googlegroups.com
On Mon, 18 Sept 2023 at 09:03, Ashok Reddy <ashok...@gmail.com> wrote:
Hi John, 

I have configured the meta_play: end_play in roles where the task has failed but it will not exit . 
Can you please guide me?

Please focus on the actual problem, and respond to Todd's mail and provide the requested data.

Ashok Reddy

unread,
Sep 18, 2023, 5:00:15 AM9/18/23
to ansible...@googlegroups.com
I will send 

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

John Petro

unread,
Sep 18, 2023, 8:59:06 AM9/18/23
to ansible...@googlegroups.com
Honestly, I can't.  I just used one of the examples from the documentation and that solved my problem.  My suggestion, is that you might want to poke at that.  I do a lot of trial and error stuff when it comes to ansible in a couple of virtual machines I set up on my desktop using either virtualbox or docker, depending on what I am trying to accomplish.  With regards to the meta module, I had used it once, but then the playbook was obsolete since the host I had written it for was retired, so I just got rid of the code, because there was no use for it.  

Ashok Reddy

unread,
Sep 18, 2023, 9:13:11 AM9/18/23
to ansible...@googlegroups.com
Hi John, 

Thanks for providing your inputs and suggestions.

David Logan

unread,
Sep 18, 2023, 7:13:29 PM9/18/23
to ansible...@googlegroups.com

If you set any_errors_fatal and a task returns an error, Ansible finishes the fatal task on all hosts in the current batch, then stops executing the play on all hosts. Subsequent tasks and plays are not executed. You can recover from fatal errors by adding a rescue section to the block. You can set any_errors_fatal at the play or block level.
- hosts: somehosts
  any_errors_fatal: true
  roles:
    - myrole

- hosts: somehosts
  tasks:
    - block:
        - include_tasks: mytasks.yml
      any_errors_fatal: true

You can use this feature when all tasks must be 100% successful to continue playbook execution. For example, if you run a service on machines in multiple data centers with load balancers to pass traffic from users to the service, you want all load balancers to be disabled before you stop the service for maintenance. To ensure that any failure in the task that disables the load balancers will stop all other tasks:


Ashok Reddy

unread,
Sep 18, 2023, 8:06:18 PM9/18/23
to ansible...@googlegroups.com
Hi David, 

Thanks for providing your inputs and suggestions. I have tried with any_errors_fatal and block level in roles but can’t make it work.

Note: I am trying to implement in roles level. 

David Logan

unread,
Sep 18, 2023, 9:26:56 PM9/18/23
to ansible...@googlegroups.com
This is at the playbook or block level from what I can see. The roles you are using should be included in a playbook and the playbook defines the any_errors_fatal: true (see the example).
This would include any included roles or tasks.

Ashok Reddy

unread,
Sep 18, 2023, 11:32:26 PM9/18/23
to ansible...@googlegroups.com

Hi all,

Please find below;



Jenkins configuration:

("${environment}" == "uat") {

                ansiblePlaybook(

                    playbook: 'deploy.yml',

                    inventory: 'hosts',

                    tags: 'downtime',

                    extraVars: [

                        app_env: 'uat',

                        jfrog_username: 

                        jfrog_password: 

                        branch: "${branch}",

                        maintanance: 'no',

                        build_version: version

deploy.yml:

- hosts: "{{ app_env }}"

  gather_facts: no

  remote_user: ansible

  become: true

  serial: 1

  tasks:

    - block:

        - include_tasks: main.yml

      any_errors_fatal: true  

  vars_prompt: 

    - name: "jfrog_username"

      prompt: "Enter JFrog username"

      private: no

    - name: "jfrog_password"

      prompt: "Enter JFrog password"

      private: yes

  tags: [ downtime, nodowntime ]


- hosts: "{{ app_env }}"

  gather_facts: no

  remote_user: ansible

  become: true

  serial: 1

  ignore_errors: no

  any_errors_fatal: true

  roles:

    - deploy

  vars_prompt:

    - name: "jfrog_username"

      prompt: "Enter JFrog username"

      private: no

    - name: "jfrog_password"

      prompt: "Enter JFrog password"

      private: yes

  tags: [ downtime, nodowntime ]



Jenkins Logs:


ansible-playbook deploy.yml -i hosts -t downtime -e app_env=uat -e jfrog_username= -e jfrog_password= -e branch=uat -e maintanance=no -e build_version=558

10:44:18  ERROR! no action detected in task. This often indicates a misspelled module name, or incorrect module path.

10:44:18  

10:44:18  The error appears to have been in '/var/jenkins_home/workspace/SG-MyInfo-Deployment/deploy.yml': line 28, column 11, but may

10:44:18  be elsewhere in the file depending on the exact syntax problem.

10:44:18  

10:44:18  The offending line appears to be:

10:44:18  

10:44:18      - block:

10:44:18          - include_tasks: main.yml

10:44:18            ^ here



TASK [deploy : Check if multiple files exist and fail playbook if any file does not exist] ***

14:50:07  fatal: [10.119.39.101]: FAILED! => {"failed": true, "msg": "the field 'args' has an invalid value, which appears to include a variable that is undefined. The error was: 'item' is undefined\n\nThe error appears to have been in '/var/jenkins_home/workspace/SG-MyInfo-Deployment/deploy/tasks/main.yml': line 8, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Check if multiple files exist and fail playbook if any file does not exist\n  ^ here\n"}

14:50:07  ...ignoring

14:50:07  ERROR! invalid meta action requested: end_host

14:50:07  



Dick Visser

unread,
Sep 19, 2023, 6:03:23 AM9/19/23
to ansible...@googlegroups.com
There is Jenkins in the mix.
And there are now more errors as well.

But I think the serial=1 is your problem. 
If that host fails, then there are no other hosts in the play. 
Try without serial=1


Sent from Gmail Mobile


Ashok Reddy

unread,
Sep 19, 2023, 6:27:53 AM9/19/23
to ansible...@googlegroups.com
Hi Visser, 

Thanks for your inputs and suggestion. But if I use serial is 1, the task will execute on one host at a time. Not sure whether i am making correct statement or not. As i know, this could not cause any issue.

Dick Visser

unread,
Sep 19, 2023, 7:04:45 AM9/19/23
to ansible...@googlegroups.com
I suggested to remove the entire serial statement



Sent from Gmail Mobile


Ashok Reddy

unread,
Sep 19, 2023, 8:21:20 AM9/19/23
to ansible...@googlegroups.com

Ashok Reddy

unread,
Sep 20, 2023, 5:06:57 AM9/20/23
to ansible...@googlegroups.com
Hi David, 

I tried using block level but it throws error

On Tue, 19 Sep 2023 at 7:13 AM, David Logan <skra...@gmail.com> wrote:

Ashok Reddy

unread,
Sep 20, 2023, 5:25:24 AM9/20/23
to ansible...@googlegroups.com
Hi Vesser, 

I tried without using serial=1 but can’t make it to work 

Todd Lewis

unread,
Sep 20, 2023, 10:20:21 AM9/20/23
to ansible...@googlegroups.com, uto...@gmail.com


On 9/20/23 5:24 AM, Ashok Reddy wrote:
I tried without using serial=1 but can’t make it to work 

On Wed, 20 Sep 2023 at 5:06 PM, Ashok Reddy <ashok...@gmail.com> wrote:
Hi David, 

I tried using block level but it throws error
Neither of these responses is informative.
What results - from the job log - constitute "can't make it work"? What exactly did you try? What did you change, and what did you change it to?
When you tell us that "it throws error" without including - from the job log - the error that it throws, what possible response can you expect besides "Does it now? And what error did it throw exactly?"

We have established from many days ago that something is causing the equivalent of ignore_errors: true. We saw this in the job log. This may be related to something in your Ansible configuration. The output from the command
ansible-config dump --only-changed
would tell us, but you still have not shared that with us. Please post the output from that command.


It could also be related to how your task file is being invoked from your playbook: import, include, indirectly through roles:, or some other way. Could you please tell us how this task file gets pulled into the play?


Another thing: What does your ansible-playbook command line look like? Knowing the parameters you are using might be very helpful in our trying to reproduce the issue you are having.


What does the top of your playbook look like? I mean the section before any tasks:, pretasks:, roles:, handlers:, etc. There are keywords and values there that directly affect how plays run.

We're trying to help you. That would be much easier to do if you provided the requested information.
--
Todd

Ashok Reddy

unread,
Sep 20, 2023, 7:51:31 PM9/20/23
to ansible...@googlegroups.com
Hi Todd, 

I will provide that information.

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages