# Playbook that uses become only if root user not specified?

**URL:** <https://forum.ansible.com/t/playbook-that-uses-become-only-if-root-user-not-specified/24886>\
**Category:** Ansible Project\
**Created:** [July 4, 2017, 3:03am UTC](https://forum.ansible.com/t/playbook-that-uses-become-only-if-root-user-not-specified/24886 "2017-07-04T03:03:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ajay\_jiwanand](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ajay\_jiwanand](https://forum.ansible.com/u/ajay_jiwanand)\
**Post date:** [July 4, 2017, 3:03am UTC](https://forum.ansible.com/t/playbook-that-uses-become-only-if-root-user-not-specified/24886/1 "2017-07-04T03:03:08Z")

</div>

I am writing a playbook that will cover a wide ranged of hosts some that is integrated into AD with sudo privileges and others with just a local “root” account. I currently have the playbook written with the become module and it works fine for AD integrated hosts but when i specify a root user it throws the error “needs sudo password” when I set become\_user: root it works fine for root accounts but it gives sudo errors when using AD integrated users. Is it possible to have a playbook use sudo passwords when the user specified (through command line -u) is not root?

---

<div class="post-metadata">

**Author:** ![Daniel\_JD](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Daniel\_JD](https://forum.ansible.com/u/Daniel_JD)\
**Post date:** [July 4, 2017, 6:05am UTC](https://forum.ansible.com/t/playbook-that-uses-become-only-if-root-user-not-specified/24886/2 "2017-07-04T06:05:37Z")

</div>

You could use something like the following:

- name: Set sudo Passwort  
set\_fact:  
ansible\_become\_pass: “{{ my\_encrypted\_password }}”  
when: ansible\_user == ‘non-root-user’

---

<div class="post-metadata">

**Author:** ![ajay\_jiwanand](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ajay\_jiwanand](https://forum.ansible.com/u/ajay_jiwanand)\
**Post date:** [July 4, 2017, 5:46pm UTC](https://forum.ansible.com/t/playbook-that-uses-become-only-if-root-user-not-specified/24886/3 "2017-07-04T17:46:05Z")

</div>

Hey Daniel,

That definitely looks like it can work but that would mean that I would have to specify one password for sudo accross all devices? As there are tons of devices and tons of “admin” accounts for users who are able to perform these action it wont be possible for me to place one password under ansible\_become\_pass. Is it possible for the password to be taken from the password entered when running the playbook or for the playbook to skip become all together when ansible\_user != ‘root’?

I’ve tried this:

become: true  
when: ansible\_user != ‘root’

no luck

---

<div class="post-metadata">

**Author:** ![ajay\_jiwanand](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ajay\_jiwanand](https://forum.ansible.com/u/ajay_jiwanand)\
**Post date:** [July 4, 2017, 6:06pm UTC](https://forum.ansible.com/t/playbook-that-uses-become-only-if-root-user-not-specified/24886/4 "2017-07-04T18:06:29Z")

</div>

A very simple fix was to just change become:true to become:yes and root authentication works as long as you dont specify sudo password in command line (-K) and specify root account with -u

---

<div class="post-metadata">

**Author:** ![Daniel\_JD](https://avatars.discourse-cdn.com/v4/letter/d/f19dbf/32.png) [@Daniel\_JD](https://forum.ansible.com/u/Daniel_JD)\
**Post date:** [July 5, 2017, 6:27pm UTC](https://forum.ansible.com/t/playbook-that-uses-become-only-if-root-user-not-specified/24886/5 "2017-07-05T18:27:37Z")

</div>

I thought become: True and become: yes are “the same”?!

---

<div class="post-metadata">

**Author:** ![ajay\_jiwanand](https://avatars.discourse-cdn.com/v4/letter/a/f19dbf/32.png) [@ajay\_jiwanand](https://forum.ansible.com/u/ajay_jiwanand)\
**Post date:** [July 5, 2017, 10:55pm UTC](https://forum.ansible.com/t/playbook-that-uses-become-only-if-root-user-not-specified/24886/6 "2017-07-05T22:55:20Z")

</div>

That’s what I thought! Apparently they’re not because if I use true it works fine when specifying root account. I’ve tried googling to find the difference but no luck. I guess that’s my next question on here? Lol
