# unable to run plays with sudo rights

**URL:** <https://forum.ansible.com/t/unable-to-run-plays-with-sudo-rights/14623>\
**Category:** Ansible Project\
**Tags:** ubuntu\
**Created:** [February 21, 2014, 7:32pm UTC](https://forum.ansible.com/t/unable-to-run-plays-with-sudo-rights/14623 "2014-02-21T19:32:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SLS](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@SLS](https://forum.ansible.com/u/SLS)\
**Post date:** [February 21, 2014, 7:32pm UTC](https://forum.ansible.com/t/unable-to-run-plays-with-sudo-rights/14623/1 "2014-02-21T19:32:11Z")

</div>

I’m having trouble running ansible plays as root. I have a user (user\_x) on an Ubuntu server (12.04) who has sudo rights and I can run sudo commands on the server. I am able to ping the server and gather facts as user\_x. However, when I run a play that requires sudo, in this case

> -name: ensure apache & php are installed
> 
> apt: pkg={{ item }} state=present update\_cache=yes cache\_valid\_time=86400
> 
> with\_items:
> 
> - apache2
> 
> - php5

The play fails with the error

> msg: 'apt-get install ‘apache2’ ‘php5’ ’ failed: E: Could not open lock file /var/lib/dpkg/lock - open (13: Permission denied)

> E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

The variables are the defaults and I’m calling the playbook like so

> ansible-playbook site.yml --limit staging

Any help would be appreciated.

---

<div class="post-metadata">

**Author:** ![Timothy\_Gerla](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@Timothy\_Gerla](https://forum.ansible.com/u/Timothy_Gerla)\
**Post date:** [February 21, 2014, 8:25pm UTC](https://forum.ansible.com/t/unable-to-run-plays-with-sudo-rights/14623/2 "2014-02-21T20:25:48Z")

</div>

Hi Steve,

Are you specifying “sudo: true” in the task or in the play? You can do that, or you can pass “–sudo” to the command line for a one-off setting for the entire playbook. But you do have to tell Ansible when/where to sudo.

Hope this helps!

-Tim

---

<div class="post-metadata">

**Author:** ![SLS](https://avatars.discourse-cdn.com/v4/letter/s/8c91f0/32.png) [@SLS](https://forum.ansible.com/u/SLS)\
**Post date:** [February 21, 2014, 10:45pm UTC](https://forum.ansible.com/t/unable-to-run-plays-with-sudo-rights/14623/3 "2014-02-21T22:45:33Z")

</div>

Thanks, Tim.

I misunderstood and was trying to set sudo as a variable rather than a task/handler parameter.

Having watched your great _Quick Start Video_, I didn’t realize I’d have to use the -K option when calling the playbook, since Ubuntu’s default is to not enable a root user. In order to have the kind of effortless automation you demonstrate in the video I’d have to: a) run as root, b) use NOPASSWD in the sudoers file, or c) install the user\_x’s ssh key in the root known\_hosts file. Am I understanding this correctly?

Thanks much!  
Steve
