4
votes

I'm trying to use Ansible to provision a Vagrant box and a EC2 server. It works fine on the Vagrant box when using --connection=local, but it seems to just ignore the sudo: True line

When I do this:

---
- hosts: remote
  vars_files:
    - vars.yml
  gather_facts: false
  sudo: True
  remote_user: root

  tasks:
  - name: test
    file: state=directory path=~/test

It creates the directory in the normal user's home directory, owned by the normal user. The user doesn't have a password, and I can use commands with sudo without being prompted for one. What am I missing?

1
Where do you want to create that directory? In root's home dir? - Mxx
Creating the directory is just a test to see who is doing the creating. If root ran the command, like I would expect, it would be in /root/test, if the ubuntu user did then it would be at /home/ubuntu/test (which is the case every time). - jmickela
That's not how the sudo command works...You can easily see that by logging to that server and from your ~/ run sudo mkdir test. You'll see that it's created exactly where you are, not in root's ~/. - Mxx
That's true, but that was really only part of the test. The second part was ownership. The directory gets created in the normal user directory, and is owned by the normal user, meaning sudo isn't used at all. - jmickela

1 Answers

5
votes

You need to also add "sudo_user" option.

Details:
sudo uses the value of sudo_user to obtain username and sudo_user defaults to root, so in your example you login as root (this username came from "remote_user") and do
sudo su - root -c "mkdir ~/test"

http://docs.ansible.com/intro_configuration.html#sudo-user

And yes, the directory was made in sudo_user home.