<blockquote>
<p>When I run ansible against a host machine that has some commands I need to run as root, I can't run these with &quot;become&quot; because it runs the commands as root instead of just running them with sudo.</p>
</blockquote>
<p>That's not true; <a href="https://docs.ansible.com/ansible/latest/become.html" rel="nofollow noreferrer"><code>become</code></a> uses a variety of methods for privilege escalation, and defaults to using <code>sudo</code>.</p>
<p>Are you perhaps running into one of <a href="https://docs.ansible.com/ansible/latest/user_guide/become.html#risks-and-limitations-of-become" rel="nofollow noreferrer">the known limitations with the become module</a>, like <a href="https://docs.ansible.com/ansible/latest/user_guide/become.html#privilege-escalation-must-be-general" rel="nofollow noreferrer">trying to limit sudo access to certain commands</a>?</p>

CC BY-SA 3.0
2017-12-26T19:22:19.650Z
Well this seems like one of the limitations of ansible, when I run -vvv it's doing `sudo -H -S -n -u root /bin/sh -c 'bunch of stuff'`.  I figured it could just run the command I want to with sudo privileges, but I guess that's a little naive since it needs to run 1000 other types of commands with root in a more general way.
Peter Turner
CC BY-SA 4.0
2021-03-11T21:01:49.460Z
Came across this post and Ansible limitation as we are only allowed to run limited commands with sudo, don't have full sudo access to run any commands. Are there any alternatives other than running the command(s) as 'sudo /opt/apache/bin/apachectl start' either in Ansible shell or command module?
cnu
CC BY-SA 4.0
2021-03-15T19:14:23.587Z
@cnu You have three options: 1) Use the command module  2) Don't use sudo limitations in that way  3) Don't use ansible to trigger these commands.
Xiong Chiamiov
CC BY-SA 4.0
2021-03-15T19:43:35.267Z
@XiongChiamiov, Yes. Testing Ansible scripts with command module and that appears to be working fine other than producing "[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo".
cnu
CC BY-SA 4.0
2017-12-26T18:48:31.190Z
2021-03-15T19:12:59.863Z
2021-03-15T19:12:59.863Z
Xiong Chiamiov
<p>Did you try -K already? I always use this option and then a sudo password has to be entered</p>

CC BY-SA 3.0
2017-12-26T19:53:37.650Z
yeah, the problem is the ssh user I'm using doesn't have rights to start a shell as root, just to the commands I want to run via ansible.
Peter Turner
CC BY-SA 3.0
2017-12-26T21:30:04.157Z
I do not think that it is possible to run commands as root if the user cannot start a shell as root as ansible is basically running commands via ssh.
030
CC BY-SA 3.0
2017-12-26T19:48:19.023Z
2017-12-26T19:48:19.023Z
030
<p>When I run ansible against a host machine that has some commands I need to run as root, I can't run these with "become" because it runs the commands as root instead of just running them with sudo.</p>

<p>What I've been doing is just using <code>command: sudo ...</code>  and then suppressing warnings with </p>

<pre><code>args: 
  warn: no
</code></pre>

<p>is there a better, more standardized, way to run a linux commandline with sudo sans password?</p>

CC BY-SA 3.0
2017-12-26T18:16:55.913Z
What is the real reason for this? Ansible use `sudo` under the hood when you execute task with `become`.
Konstantin Suvorov
CC BY-SA 3.0
2017-12-26T19:16:13.163Z
@kon when I use `become` I get `"msg": "Missing sudo password"` for a fail reason
Peter Turner
<ansible><passwords>
How to run Ansible with sudo no password commands with become syntax
Peter Turner
# How to run Ansible with sudo no password commands with become syntax

(Asked by: username_0 on 2017-12-26)

When I run ansible against a host machine that has some commands I need to run as root, I can't run these with "become" because it runs the commands as root instead of just running them with sudo.
What I've been doing is just using command: sudo ...  and then suppressing warnings with 
args: 
  warn: no

is there a better, more standardized, way to run a linux commandline with sudo sans password?

**Tags:** ansible, passwords

> **Comments:**
> **username_1**: What is the real reason for this? Ansible use `sudo` under the hood when you execute task with `become`.  
> **username_0**: @kon when I use `become` I get `"msg": "Missing sudo password"` for a fail reason  

---

## 2 answers

### Answer by username_2 on 2017-12-26. Score: 7

> When I run ansible against a host machine that has some commands I need to run as root, I can't run these with "become" because it runs the commands as root instead of just running them with sudo.

That's not true; [`become`](https://docs.ansible.com/ansible/latest/become.html) uses a variety of methods for privilege escalation, and defaults to using `sudo`.

Are you perhaps running into one of [the known limitations with the become module](https://docs.ansible.com/ansible/latest/user_guide/become.html#risks-and-limitations-of-become), like [trying to limit sudo access to certain commands](https://docs.ansible.com/ansible/latest/user_guide/become.html#privilege-escalation-must-be-general)?

> **Comments:**
> **username_0**: Well this seems like one of the limitations of ansible, when I run -vvv it's doing `sudo -H -S -n -u root /bin/sh -c 'bunch of stuff'`.  I figured it could just run the command I want to with sudo privileges, but I guess that's a little naive since it needs to run 1000 other types of commands with root in a more general way.  
> **username_3**: Came across this post and Ansible limitation as we are only allowed to run limited commands with sudo, don't have full sudo access to run any commands. Are there any alternatives other than running the command(s) as 'sudo /opt/apache/bin/apachectl start' either in Ansible shell or command module?  
> **username_2**: @username_3 You have three options: 1) Use the command module  2) Don't use sudo limitations in that way  3) Don't use ansible to trigger these commands.  
> **username_3**: @XiongChiamiov, Yes. Testing Ansible scripts with command module and that appears to be working fine other than producing "[WARNING]: Consider using 'become', 'become_method', and 'become_user' rather than running sudo".  

---

### Answer by username_4 on 2017-12-26. Score: 2

Did you try -K already? I always use this option and then a sudo password has to be entered

> **Comments:**
> **username_0**: yeah, the problem is the ssh user I'm using doesn't have rights to start a shell as root, just to the commands I want to run via ansible.  
> **username_4**: I do not think that it is possible to run commands as root if the user cannot start a shell as root as ansible is basically running commands via ssh.  

---

