Using Ansible Shell Module
To create Database and User
Here I am creating : airflow db, user airflow_user and password airflow_password
Set postgres_user(the main user) password in PGPASSWORD variable
- name: Created Airflow Database and User in Postgres
shell: /usr/bin/psql --host=10.0.0.39 --dbname=postgres --username=postgres -c "CREATE DATABASE airflow" ; /usr/bin/psql --host=10.0.0.39 --dbname=postgres --username=postgres -c "CREATE USER airflow_user WITH PASSWORD 'airflow_password'" ; /usr/bin/psql --host=10.0.0.39 --dbname=postgres --username=postgres -c "GRANT ALL PRIVILEGES ON DATABASE airflow TO airflow_user " ;
environment:
PGPASSWORD: postgres_password
register: postgres_query_status
failed_when: "'already exists' not in postgres_query_status.stderr and 'CREATE' not in postgres_query_status.stdout"
tags:
- postgres