unable to run plays with sudo rights

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.

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

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