I am working to run a Postgres Docker container with Ansible playbook, but I'm having problems with passing the POSTGRES_USER and the POSTGRES_PASSWORD as env.
This is my playbook:
- name: Run Postgresql container
hosts: db_farm
gather_facts: no
tasks:
- name: Start docker service
service:
name: docker
state: started
- name: install pip dependencies
pip:
name: docker
- name: Create Postgres Container
docker_container:
name: hitch_postgres
image: postgres:12
state: started
recreate: yes
ports:
- "5432:5432"
volumes:
- /home/hitch_postgres_data:/var/lib/postgresql/data
env:
POSTGRES_USER: "myuser"
POSTGRES_PASSWORD: "myuser"
But when trying to connect to the server by first going into the container with docker exec -it hitch_postgres bash and running either psql -U myuser or psql -U postgresit shows this errors:
psql: error: FATAL: role "myuser" does not exist
psql: error: FATAL: role "postgres" does not exist
I know Ansible has a module called postgresql_userbut when using it like this:
postgresql_user:
name: myuser
password: myuser
It throws this error:
So, how to pass the environmental variables from Ansible that by using the Docker CLI would be as simple as adding -e POSTGRES_USER=123 -e POSTGRES_PASSWORD=123?

/home/hitch_postgres_datais empty. If there is a database, postgres won't make a new user and won't change the password. That is if you run this command with some another user before - it stays there and won't go unless you do something.