Can't get sudo commands working with Ansible
-
21-12-2019 - |
Question
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?
Solution
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.