# Ansible playbook should exit Immediately when it learns Incorrect sudo password is entered

**URL:** <https://forum.ansible.com/t/ansible-playbook-should-exit-immediately-when-it-learns-incorrect-sudo-password-is-entered/27936>\
**Category:** Ansible Project\
**Created:** [July 2, 2018, 10:27pm UTC](https://forum.ansible.com/t/ansible-playbook-should-exit-immediately-when-it-learns-incorrect-sudo-password-is-entered/27936 "2018-07-02T22:27:37Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhijith.devaraju](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@abhijith.devaraju](https://forum.ansible.com/u/abhijith.devaraju)\
**Post date:** [July 2, 2018, 10:27pm UTC](https://forum.ansible.com/t/ansible-playbook-should-exit-immediately-when-it-learns-incorrect-sudo-password-is-entered/27936/1 "2018-07-02T22:27:37Z")

</div>

Hello Everyone,

I am trying to find a way to add a check where playbook will exit Immediately if incorrect sudo password is entered by a user. As of now, If incorrect password is entered ansible would execute the playbook and each host will through error on the screen as Incorrect sudo password. I am using -K option along with my ansible command to prompt for sudo password. (I cannot store the password anywhere on the server to validate).

FAILED! =\> {“msg”: “Incorrect sudo password”}

Is there a way Ansible would not execute the playbook & not throw errors for each host as soon as it learns that the sudo password is Incorrect for one host and exit the playbook ?

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [July 2, 2018, 10:51pm UTC](https://forum.ansible.com/t/ansible-playbook-should-exit-immediately-when-it-learns-incorrect-sudo-password-is-entered/27936/2 "2018-07-02T22:51:54Z")

</div>

Create a task that runs at the become with become: yes. That will tell you if the password is valid or not and will stop the executable if it fails.

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [July 2, 2018, 10:56pm UTC](https://forum.ansible.com/t/ansible-playbook-should-exit-immediately-when-it-learns-incorrect-sudo-password-is-entered/27936/3 "2018-07-02T22:56:16Z")

</div>

s/runs at the become/runs at the beginning/

---

<div class="post-metadata">

**Author:** ![abhijith.devaraju](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@abhijith.devaraju](https://forum.ansible.com/u/abhijith.devaraju)\
**Post date:** [July 2, 2018, 11:00pm UTC](https://forum.ansible.com/t/ansible-playbook-should-exit-immediately-when-it-learns-incorrect-sudo-password-is-entered/27936/4 "2018-07-02T23:00:38Z")

</div>

Hi Jordan,

Thanks for the response. Could you give me an example of how it should look like ? I am using become: true at the top for all my tasks. Please see below for reference:

---

<div class="post-metadata">

**Author:** ![jborean](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/jborean/32/1525_2.png) [@jborean](https://forum.ansible.com/u/jborean)\
**Post date:** [July 3, 2018, 12:28am UTC](https://forum.ansible.com/t/ansible-playbook-should-exit-immediately-when-it-learns-incorrect-sudo-password-is-entered/27936/5 "2018-07-03T00:28:46Z")

</div>

Ahh I misunderstood what you are asking for, here is one way it could potentially be done

`

- name: verify we can connect and become works  
gather\_facts: no  
become: yes  
any\_errors\_fatal: yes

tasks:

- name: run a simple module with become to verify it works  
ping:

- name: continue playbook once we know we can connect and become works  
remote\_user: ops  
become: yes  
strategy: free  
tasks:  
… # add the normal tasks here  
`

This runs 2 plays in your playbook where the first will run a simple command with become to verify that it works for all hosts and then continue onto the next play which is your normal one. When you specify “any\_errors\_fatal”[1] it will abort the play with a failure and Ansible will not continue onto the 2nd play as the first failed.

[https://docs.ansible.com/ansible/latest/user\_guide/playbooks\_error\_handling.html#aborting-the-play](https://docs.ansible.com/ansible/latest/user_guide/playbooks_error_handling.html#aborting-the-play)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 3, 2018, 12:32am UTC](https://forum.ansible.com/t/ansible-playbook-should-exit-immediately-when-it-learns-incorrect-sudo-password-is-entered/27936/6 "2018-07-03T00:32:33Z")

</div>

> this should not be default as hosts can have different password configured and run successfully

---

<div class="post-metadata">

**Author:** ![erichymowitz](https://avatars.discourse-cdn.com/v4/letter/e/9d8465/32.png) [@erichymowitz](https://forum.ansible.com/u/erichymowitz)\
**Post date:** [July 3, 2018, 12:27pm UTC](https://forum.ansible.com/t/ansible-playbook-should-exit-immediately-when-it-learns-incorrect-sudo-password-is-entered/27936/7 "2018-07-03T12:27:57Z")

</div>

Correct me if I’m wrong, but an ansible command/playbook will only prompt you once for the sudo password; therefore, it’s safe to assume that if you’re running a single command/play on multiple hosts, they have the same sudo password.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/ansible/original/1X/1882d9a01da5d8996a862359b0913cc568a39b84.svg) [@system](https://forum.ansible.com/u/system)\
**Post date:** [July 3, 2018, 12:43pm UTC](https://forum.ansible.com/t/ansible-playbook-should-exit-immediately-when-it-learns-incorrect-sudo-password-is-entered/27936/8 "2018-07-03T12:43:19Z")

</div>

> From prompt, yes, but you also have ansible\_sudo\_pass to set

individual (or group of) hosts, having one does not discount the  
other.

---

<div class="post-metadata">

**Author:** ![abhijith.devaraju](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@abhijith.devaraju](https://forum.ansible.com/u/abhijith.devaraju)\
**Post date:** [July 3, 2018, 9:03pm UTC](https://forum.ansible.com/t/ansible-playbook-should-exit-immediately-when-it-learns-incorrect-sudo-password-is-entered/27936/9 "2018-07-03T21:03:16Z")

</div>

Hi Jordan,

I can try this but the problem here will be If there are any hosts which are unreachable then bcz of the fatal error ansible would exit out which I don’t want to happen. Is there any special way to check only for the purpose of authentication ?
