Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I'm getting 'msg: Failed to lock apt for exclusive operation' when trying to install some dependencies in a playbook using apt, with the vagrant user. I'm suspecting it tells me this because it can't authenticate the vagrant user while running the task.

This is the role declaration I'm using.

- name: ruby installer
  hosts: all
  sudo: True
  sudo_user: vagrant
  roles:
  - ruby

And this is the task that fails:

    - name: Install ruby building dependencies
      apt: name={{ item }} 
           state=installed 
           update_cache=yes

      with_items:
      - dependency

When removing "update_cache=yes" and re-running the playbook I'm getting: E: Unable to lock the administration directory (/var/lib/dpkg/), are you root?

Obviously when running it with root it works perfectly fine, but I need it to run under a different user other then root because of some ruby (rvm, rbenv specific) tasks.

How do I properly authenticate the vagrant user when running my playbook? Can someone point me in the right direction?

share|improve this question
add comment

2 Answers

I ended up with this role declaration:

- name: ruby installer
  hosts: all
  sudo: True
  user: vagrant
  roles:
  - ruby

And this task for installing the dependencies:

- name: Install ruby building dependencies
  apt: name={{ item }} 
       state=installed 
       update_cache=yes
  sudo_user: root

  with_items:
  -dependency

It now uses root for the apt task and vagrant for the other ruby tasks in the playbook.

share|improve this answer
add comment

apt installs system-level packages and as such, using apt requires root-equivalent privileges.

If you need to perform further application deployment steps as a different user, you will need to pull those steps out of your deb package installation process.

share|improve this answer
    
Can you be more specific? How do I pull those steps out of the deb package installation process? Is there a way to 'sudo' the vagrant user in Ansible, and avoid using the root user? Thanks. –  Hyperfocus Jan 15 at 7:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.