I am trying to provision an Ubuntu Xenial Vagrant guest with Ansible. It worked correctly on Ubuntu 14.04, but fails on 16.04. The error that I get is
chown failed: failed to look up user vagrant
The task that I am running is the following:
- name: Ensure /srv/web exists
become: yes
file:
path: /srv/web
state: directory
mode: 0755
owner: "{{ remote_user }}"
Searching hasn't found much help.
Thanks!
Edit: Further testing on a Digital Ocean 14.04 droplet also shows this issue.
Edit 2: Full output log at -vvvv level
SOLOUTION#1
Ubuntu 16.04 has python3, not 2.7.x. and Ansible doesn't support for Python 3 yet.
For Ubuntu 16.04 I use the following play to get Python 2.7 installed.
---
- hosts: xenials
gather_facts: no
become: yes
tasks:
- name: Install python 2.7
raw: apt-get update -qq && apt-get install -qq python2.7
- name: check if softlink exists
stat:
path: /usr/bin/python
register: python_file
- name: create softlink for python 2.7
raw: ln -s /usr/bin/python2.7 /usr/bin/python
when: python_file.stat.islnk is not defined
- name: Ensure /srv/web exists
file:
path: /srv/web
state: directory
mode: 0755
owner: "{{ remote_user }}" # hope you have defined this variable in your ansible.cfg
key line is gather_facts: no
which prevents Ansible to execute code on the remote server that the remote server cannot yet support.Without this line,play would fail.
That provides an /usr/bin/python2.7
, which I explicitly point to in my inventory file.
[xenials]
192.168.33.100
[xenials:vars]
ansible_python_interpreter=/usr/bin/python2.7
Note that there is nothing special about the name xenials
. It's just a group I have defined in my inventory.
This ansible_python_interpreter
only need for the first time, after that you can remove it because we have created the softlink for the python2.7
Hope that help you.
SOLOUTION#2
Reason of this error:
I have reviewed the gist that contain the detail log and figured out this:
- I am sure you are using the official vagrant box
ubuntu/xenial64
- This official vagrant box doesn't have
vagrant
user
"msg": "chown failed: failed to look up user vagrant"
that's the error trace which help you find the exact error.
Solution-1 to this error:
Download some other vagrant box, I recommend this vagrant box geerlingguy/ubuntu1604
it has really good reputation
Solution-2 to this error:
With the official vagrant box, you can do the same as I am doing to mitigate this error. Add the following to your Vagrantfile
:
config.vm.provision "shell" do |s|
ssh_pub_key = File.readlines("#{Dir.home}/.ssh/id_rsa.pub").first.strip
ssh_user = ENV['USER']
s.inline = <<-SHELL
adduser --disabled-password --gecos "" #{ssh_user}
mkdir -p /home/#{ssh_user}/.ssh
touch /home/#{ssh_user}/.ssh/authorized_keys
chown -R #{ssh_user}. /home/#{ssh_user}/.ssh
echo "#{ssh_user} ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/99-#{ssh_user}
echo #{ssh_pub_key} >> /home/#{ssh_user}/.ssh/authorized_keys
chmod 0440 /etc/sudoers.d/99-#{ssh_user}
SHELL
end
What it will do is, just create your host login user to the guest and upload the rsa key for it. So you can run the playbook to the vagrant machine as if it is the remote machine. If you'll face any problem further, please let me know.
Hope this help you.
I had the same problem. I am using ubuntu/xenial64
.
I noticed that there is no vagrant
user. When I SSH in using Vagrant, the ubuntu
user is used.
I created an ansible.cnf
:
[default]
remote_user = ubuntu
After that I encountered another issue: no python. I fixed this with:
sudo apt-get install python2.7
sudo apt-get install python-pip
Now, the ansible ping
module works for my host.
$ ansible elastichsearch -m ping
192.168,101.10 | SUCCESS => {
"changed": false,
"ping": "pong"
}