# help while running sudo in Ansible playbooks

**URL:** <https://forum.ansible.com/t/help-while-running-sudo-in-ansible-playbooks/34446>\
**Category:** Ansible Project\
**Created:** [January 15, 2021, 5:08am UTC](https://forum.ansible.com/t/help-while-running-sudo-in-ansible-playbooks/34446 "2021-01-15T05:08:22Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ramakrishna\_V](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@Ramakrishna\_V](https://forum.ansible.com/u/Ramakrishna_V)\
**Post date:** [January 15, 2021, 5:08am UTC](https://forum.ansible.com/t/help-while-running-sudo-in-ansible-playbooks/34446/1 "2021-01-15T05:08:22Z")

</div>

Hi Ansible team,

I am Ram. We need some help in running the Ansible playbook ( specifically how to add sudo while running playbooks ). in your environment, we can run all admin related commands with sudo acess ( by adding sudo in front of command . For example, sudo vgs or sudo vi /etc/sudoers ). We don’t have root access and hence we don’t wanted to run the playbooks with direct root access .

Most of our servers ( Redhat / Solaris / Aix ) doesn’t have root access. The possibility of running all admin activities are by running sudo as admin ( sysunx ) user.

I mean, after logging to the server with sysunx account, we run the admin related command as sudo . How to simulate this in Ansible.

#playbook code

[root@sgdlvapp03infra splunk\_forwarder]# cat sudo\_test\_solaris.yml

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 15, 2021, 6:23am UTC](https://forum.ansible.com/t/help-while-running-sudo-in-ansible-playbooks/34446/2 "2021-01-15T06:23:06Z")

</div>

> Hi Ansible team,
> 
> I am Ram. We need some help in running the Ansible playbook ( specifically how to add sudo while running playbooks ).  
> in your environment, we can run all admin related commands with sudo acess ( by adding sudo in front of command . For  
> example, sudo vgs or sudo vi /etc/sudoers ). We don't have root access and hence we don't wanted to run the playbooks  
> with direct root access .
> 
> Most of our servers ( Redhat / Solaris / Aix ) doesn't have root access. The possibility of running all admin activities  
> are by running sudo \<command\> as admin ( sysunx ) user.
> 
> I mean, after logging to the server with sysunx account, we run the admin related command as sudo \<command\>. How to  
> simulate this in Ansible.

You need to add "become: yes" to the touch task (or to the whole playbook):

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Rackd

---

<div class="post-metadata">

**Author:** ![Ramakrishna\_V](https://avatars.discourse-cdn.com/v4/letter/r/ecae2f/32.png) [@Ramakrishna\_V](https://forum.ansible.com/u/Ramakrishna_V)\
**Post date:** [January 15, 2021, 7:16am UTC](https://forum.ansible.com/t/help-while-running-sudo-in-ansible-playbooks/34446/3 "2021-01-15T07:16:07Z")

</div>

After adding ‘become: true’ also no use, its not working.

My use case is to add sudo while running playbooks ( to run sudo comamnds, no password is required ) .

[root@sgdlvapp03infra splunk\_forwarder]# ssh -q [sysunx@10.4.67.141](mailto:sysunx@10.4.67.141) “touch /opt/testfile” → without sudo, command is fialining  
Password:  
touch: cannot change times on /opt/testfile: Permission denied  
[root@sgdlvapp03infra splunk\_forwarder]#  
[root@sgdlvapp03infra splunk\_forwarder]#  
[root@sgdlvapp03infra splunk\_forwarder]# ssh -q [sysunx@10.4.67.141](mailto:sysunx@10.4.67.141) “sudo touch /opt/testfile” → with sudo, command is wokring. No password is required.  
Password:  
[root@sgdlvapp03infra splunk\_forwarder]#  
[root@sgdlvapp03infra splunk\_forwarder]#  
[root@sgdlvapp03infra splunk\_forwarder]# ssh -q [sysunx@10.4.67.141](mailto:sysunx@10.4.67.141) “ls /opt/testfile”  
Password:  
/opt/testfile  
[root@sgdlvapp03infra splunk\_forwarder]#

PLaybook

[root@sgdlvapp03infra splunk\_forwarder]# cat sudo\_test\_solaris.yml

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 15, 2021, 7:24am UTC](https://forum.ansible.com/t/help-while-running-sudo-in-ansible-playbooks/34446/4 "2021-01-15T07:24:23Z")

</div>

> After adding 'become: true' also no use, its not working.

You need to tell Ansible the sudo password:

[https://docs.ansible.com/ansible/latest/user\_guide/become.html](https://docs.ansible.com/ansible/latest/user_guide/become.html)

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![jean-yves](https://avatars.discourse-cdn.com/v4/letter/j/a587f6/32.png) [@jean-yves](https://forum.ansible.com/u/jean-yves)\
**Post date:** [January 15, 2021, 8:11am UTC](https://forum.ansible.com/t/help-while-running-sudo-in-ansible-playbooks/34446/5 "2021-01-15T08:11:13Z")

</div>

Hi,

Ansible is designed to have all sudo (or su) access (but not remote root acess to be more specific)....So you should be sure thats is no way to have it !

See "Privilege escalation must be general \<[https://docs.ansible.com/ansible/latest/user\_guide/become.html#id9&gt;&quot](https://docs.ansible.com/ansible/latest/user_guide/become.html#id9&gt;&quot); on [https://docs.ansible.com/ansible/latest/user\_guide/become.html#only-one-method-may-be-enabled-per-host](https://docs.ansible.com/ansible/latest/user_guide/become.html#only-one-method-may-be-enabled-per-host)

If not, you can still use the same command you already use with "sudo command" with the shell module (using become: no)... but's ugly and you loose idempotence work of all the module (except shell) that are provided by ansible community

Regards,

JYL

---

<div class="post-metadata">

**Author:** ![racke](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/racke/32/4767_2.png) [@racke](https://forum.ansible.com/u/racke)\
**Post date:** [January 15, 2021, 10:35am UTC](https://forum.ansible.com/t/help-while-running-sudo-in-ansible-playbooks/34446/6 "2021-01-15T10:35:05Z")

</div>

> > Hi Ansible team,
> > 
> > I am Ram. We need some help in running the Ansible playbook ( specifically how to add sudo while running playbooks  
> > ). in your environment, we can run all admin related commands with sudo acess ( by adding sudo in front of command .  
> > For example, sudo vgs or sudo vi /etc/sudoers ). We don't have root access and hence we don't wanted to run the  
> > playbooks with direct root access .
> > 
> > Most of our servers ( Redhat / Solaris / Aix ) doesn't have root access. The possibility of running all admin  
> > activities are by running sudo \<command\> as admin ( sysunx ) user.
> 
> Hi,
> 
> Ansible is designed to have all sudo (or su) access (but not remote root acess to be more specific)....So you should be  
> sure thats is no way to have it !
> 
> See "Privilege escalation must be general \<[https://docs.ansible.com/ansible/latest/user\_guide/become.html#id9&gt;&quot](https://docs.ansible.com/ansible/latest/user_guide/become.html#id9&gt;&quot); on  
> [https://docs.ansible.com/ansible/latest/user\_guide/become.html#only-one-method-may-be-enabled-per-host](https://docs.ansible.com/ansible/latest/user_guide/become.html#only-one-method-may-be-enabled-per-host)
> 
> If not, you can still use the same command you already use with "sudo command" with the shell module (using become:  
> no)... but's ugly and you loose idempotence work of all the module (except shell) that are provided by ansible community

I really don't see the point to promote using Ansible as glorified shell, especially as it seems that the only the sudo  
password is missing in this case.

Regards  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Racke

---

<div class="post-metadata">

**Author:** ![Dick\_Visser](https://avatars.discourse-cdn.com/v4/letter/d/d9b06d/32.png) [@Dick\_Visser](https://forum.ansible.com/u/Dick_Visser)\
**Post date:** [January 15, 2021, 10:45am UTC](https://forum.ansible.com/t/help-while-running-sudo-in-ansible-playbooks/34446/7 "2021-01-15T10:45:58Z")

</div>

Unrelated to ansible, but 'vi /etc/sudoers' is dangerous, use visudo instead.
