Skip to content

Ansible playbook fails when ANSIBLE_PLAYBOOK_USER_NAME or ANSIBLE_PLAYBOOK_USER_ID is not set

Summary

When container is run without ANSIBLE_PLAYBOOK_USER_ID variable execution fails with following error:

docker run --rm --mount "type=bind,source=${PWD},target=/playbook" --env  "ANSIBLE_PLAYBOOK_USER_NAME=${USER}" containers.projects.task.gda.pl/containers/ansible-playbook:18.08 -i inventory.yml example.yml 
useradd: invalid user ID 'porzech'
chown: invalid user: 'porzech'
su: user porzech does not exist
su: user porzech does not exist

When container is run without ANSIBLE_PLAYBOOK_USER_NAME variable user is not added and following error is written:

docker run --rm --mount "type=bind,source=${PWD},target=/playbook" --env  "ANSIBLE_PLAYBOOK_USER_ID=${UID}" containers.projects.task.gda.pl/containers/ansible-playbook:18.08 -i inventory.yml example.yml
Usage: useradd [options] LOGIN
       useradd -D
       useradd -D [options]

Options:
  ...
chown: missing operand after '/home//'
Try 'chown --help' for more information
...

Proposal

"entrypoint.sh" script should check if variables are set and:

  1. if one of them is not set it should be run as root user.
  2. if one of them is not set it should fail and if both are not set it should be run as root
Edited by Piotr Orzechowski