我目前正在运行以下命令
# su - postgres
$ psql
postgres=# create role user_name with login encrypted password 'password';
postgres=# create database database_name owner user_name template template0 encoding 'UTF8' lc_collate 'en_US.UTF-8' lc_ctype 'en_US.UTF-8';
我想利用postgresql module in ansible这样做。
到目前为止,我已尝试过这个
- name: create the "{{ ovirt_engine_dwh_db_name }}" db from the command line
command: |
psql -c "create role {{ ovirt_engine_dwh_db_user }} with login encrypted password '{{ ovirt_engine_dwh_db_password }}';"
psql -c "create database {{ ovirt_engine_dwh_db_name }} owner {{ ovirt_engine_dwh_db_user }} template template0 encoding 'UTF8' lc_collate 'en_US.UTF-8' lc_ctype 'en_US.UTF-8';"
become_user: postgres
但是,如果我尝试这个错误,那就是jinja2模板不会替换我在defaults/main.yml
中定义的变量
答案 0 :(得分:3)
您需要使用postgresql_user和postgresql_db将任务拆分为两个单独的任务。
- name: Create the "{{ ovirt_engine_dwh_db_user }}" user
postgresql_user:
name: "{{ ovirt_engine_dwh_db_user }}"
password: "{{ ovirt_engine_dwh_db_password }}"
encrypted: True
become_user: postgres
- name: create the "{{ ovirt_engine_dwh_db_name }}" db
postgresql_db:
name: "{{ ovirt_engine_dwh_db_name }}"
owner: "{{ ovirt_engine_dwh_db_user }}"
template: template0
encoding: UTF8
lc_collate: 'en_US.UTF-8'
lc_ctype: 'en_US.UTF-8'
become_user: postgres
请注意ovirt_engine_dwh_db_password
可以是纯文本或md5加密。我可以使用下面的命令在Ubuntu 16.04上创建加密密码。
export USER=bar
export PASS=verysecretpassword
echo "md5`echo -n "${PASS}${USER}" | md5sum | awk '{print $1}'`"