mivek.postgresql
Ansible 角色:PostgreSQL
在 Debian/Ubuntu 服务器上安装和配置 PostgreSQL 服务器。
需求
没有特殊要求;注意此角色需要 root 权限,因此请在 playbook 中使用全局的 become: true
运行它,或者在您的 playbook 中像这样调用这个角色:
- hosts: database
become: true
roles:
- role: mivek.postgresql
角色变量
可用变量列表如下,并带有默认值(见 defaults/main.yml
):
postgresql_restarted_state: "restarted"
在配置更改时设置服务的状态。推荐值为 restarted
或 reloaded
。
postgresql_user: postgres
postgresql_group: postgres
运行 PostgreSQL 的用户和组。
postgresql_unix_socket_directories:
- /var/run/postgresql
创建 PostgreSQL 套接字的目录(通常一个,但可以有多个)。
postgresql_service_state: started
postgresql_service_enabled: true
控制 PostgreSQL 服务的状态以及在启动时是否启用。
postgresql_auth_method: scram-sha-256
要使用的认证方法。可以是 scram-sha-256 或 md5。
postgresql_global_config_options:
- option: unix_socket_directories
value: '{{ postgresql_unix_socket_directories | join(",") }}'
- option: log_directory
value: 'log'
- option: password_encryption
value: "{{ postgresql_auth_method }}"
将设置在 postgresql.conf
中的全局配置选项。
对于版本低于 9.3 的 PostgreSQL,您至少需要重写此变量并将 option
设置为 unix_socket_directory
。
如果您用其他路径(相对或绝对)重写了 option: log_directory
的值,则该角色会为您创建该路径。
postgresql_hba_entries:
- { type: local, database: all, user: postgres, auth_method: peer }
- { type: local, database: all, user: all, auth_method: peer }
- { type: host, database: all, user: all, address: '127.0.0.1/32', auth_method: md5 }
- { type: host, database: all, user: all, address: '::1/128', auth_method: md5 }
- { type: host, database: all, user: all, addresses: ['10.0.0.1/32', '10.0.0.2/32'], auth_method: md5 }
配置 基于主机的认证 条目,以便设置在 pg_hba.conf
中。条目的选项包括:
type
(必需)database
(必需)user
(必需)addresses
地址列表address
(这三者中至少需要一个)ip_address
ip_mask
auth_method
(必需)auth_options
(可选)
如果重写,请确保从 defaults/main.yml
中复制现有条目,如果需要保留现有条目。
postgresql_locales:
- 'en_US.UTF-8'
(仅限 Debian/Ubuntu)用于生成 PostgreSQL 数据库使用的地区设置。
postgresql_databases:
- name: exampledb # 必需;其余为可选
lc_collate: # 默认为 'en_US.UTF-8'
lc_ctype: # 默认为 'en_US.UTF-8'
encoding: # 默认为 'UTF-8'
template: # 默认为 'template0'
login_host: # 默认为 'localhost'
login_password: # 默认为未设置
login_user: # 默认为 'postgresql_user'
login_unix_socket: # 默认为 postgresql_unix_socket_directories 的第一个
port: # 默认为未设置
owner: # 默认为 postgresql_user
state: # 默认为 'present'
确保在服务器上存在的数据库列表。只有 name
是必需的;所有其他属性为可选。
postgresql_users:
- name: jdoe # 必需;其余为可选
password: # 默认为未设置
encrypted: # 默认为未设置
priv: # 默认为未设置
role_attr_flags: # 默认为未设置
db: # 默认为未设置
login_host: # 默认为 'localhost'
login_password: # 默认为未设置
login_user: # 默认为 '{{ postgresql_user }}'
login_unix_socket: # 默认为 postgresql_unix_socket_directories 的第一个
port: # 默认为未设置
state: # 默认为 'present'
确保在服务器上存在的用户列表。只有 name
是必需的;所有其他属性为可选。
postgresql_privs:
- database: "{{ item.database }}"
login_host: "{{ item.login_host | default('localhost') }}"
login_password: "{{ item.login_password | default(omit) }}"
login_user: "{{ item.login_user | default(postgresql_user) }}"
login_unix_socket: "{{ item.login_unix_socket | default(postgresql_unix_socket_directories[0]) }}"
objs: "{{ item.objs | default(omit) }}"
privs: "{{ item.privs | default(omit) }}"
roles: "{{ item.roles }}"
schema: "{{ item.schema | default(omit) }}"
type: "{{ item.type | default(omit) }}"
state: "{{ item.state | default('present') }}"
确保在服务器上存在的权限列表。只有 database
和 roles
是必需的。
postgresql_pgpass_users:
- hostname: localhost
port: 5432
database: db1
name: jdoe
要添加到 pgpass
的用户列表。密码不是必需的,系统会从 postgresql_users
变量中获取。
postgresql_users_no_log: true
在管理用户时,是否输出用户数据(可能包含敏感信息,如密码)。
postgresql_privs_no_log: true
在管理权限时,是否输出权限数据。
postgresql_version: [操作系统特定]
postgresql_data_dir: [操作系统特定]
postgresql_bin_path: [操作系统特定]
postgresql_config_path: [操作系统特定]
postgresql_daemon: [操作系统特定]
postgresql_packages: [操作系统特定]
操作系统特定变量,这些变量由此角色的 vars
目录中的包含文件设置。除非您使用的是未通过系统软件包安装的 PostgreSQL 版本,否则不应重写这些变量。
依赖关系
无。
示例 Playbook
- hosts: database
become: true
roles:
- mivek.postgresql
在 vars/main.yml
中:
postgresql_databases:
- name: example_db
postgresql_users:
- name: example_user
password: supersecure
许可协议
MIT / BSD
作者信息
该角色由 Jeff Geerling 创建于 2016 年,他是 Ansible for DevOps 的作者。
ansible-galaxy install mivek.postgresql