我有一个自动安装Patroni集群的ansible项目。patroni.conf文件中存在问题。
每个主机都需要不同的配置。
例如:
主机1配置:
scope: postgres
name: postgresql0
restapi:
listen: 10.0.13.242:8008
connect_address: 10.0.13.242:8008
etcd:
host: 10.0.13.247:2379
bootstrap:
dcs:
ttl: 30
loop_wait: 10
retry_timeout: 10
maximum_lag_on_failover: 1048576
postgresql:
use_pg_rewind: true
initdb:
- encoding: UTF8
- data-checksums
pg_hba:
- host replication replicator 127.0.0.1/32 md5
- host replication replicator 10.0.13.242/24 md5
- host replication replicator 10.0.13.243/24 md5
- host replication replicator 10.0.13.244/24 md5
- host replication replicator 10.0.13.245/24 md5
- host all all 0.0.0.0/0 md5
users:
admin:
password: admin
options:
- createrole
- createdb
postgresql:
listen: 10.0.13.242:5432
connect_address: 10.0.13.242:5432
data_dir: /data/patroni
pgpass: /tmp/pgpass
authentication:
replication:
username: replicator
password: replicator_pass
superuser:
username: postgres
password: postgres
parameters:
unix_socket_directories: '.'
tags:
nofailover: false
noloadbalance: false
clonefrom: false
nosync: false东道2会议:
scope: postgres
name: postgresql0
restapi:
listen: 10.0.13.243:8008
connect_address: 10.0.13.243:8008
etcd:
host: 10.0.13.247:2379
bootstrap:
dcs:
ttl: 30
loop_wait: 10
retry_timeout: 10
maximum_lag_on_failover: 1048576
postgresql:
use_pg_rewind: true
initdb:
- encoding: UTF8
- data-checksums
pg_hba:
- host replication replicator 127.0.0.1/32 md5
- host replication replicator 10.0.13.242/24 md5
- host replication replicator 10.0.13.243/24 md5
- host replication replicator 10.0.13.244/24 md5
- host replication replicator 10.0.13.245/24 md5
- host all all 0.0.0.0/0 md5
users:
admin:
password: admin
options:
- createrole
- createdb
postgresql:
listen: 10.0.13.243:5432
connect_address: 10.0.13.243:5432
data_dir: /data/patroni
pgpass: /tmp/pgpass
authentication:
replication:
username: replicator
password: replicator_pass
superuser:
username: postgres
password: postgres
parameters:
unix_socket_directories: '.'
tags:
nofailover: false
noloadbalance: false
clonefrom: false
nosync: false问:我如何编写bash脚本,该脚本向用户询问主机名,替换listen和connect_address行,然后询问名称并在" name“行中替换它。我如何循环这个脚本呢?
发布于 2020-06-21 19:31:18
您可以使用无源模板特征完成这一任务。基本上,您将获取.conf文件的副本,并使用jinja2格式化变量替换需要动态定义的任何值。看起来可能是这样的:
scope: postgres
name: {{ desired_name }}
restapi:
listen: {{ ansible_default_ipv4.address }}:8008
connect_address: {{ ansible_default_ipv4.address }}:8008
etcd:
host: 10.0.13.247:2379
bootstrap:
dcs:
ttl: 30
loop_wait: 10
retry_timeout: 10
maximum_lag_on_failover: 1048576
postgresql:
use_pg_rewind: true
initdb:
- encoding: UTF8
- data-checksums
pg_hba:
- host replication replicator 127.0.0.1/32 md5
- host replication replicator 10.0.13.242/24 md5
- host replication replicator 10.0.13.243/24 md5
- host replication replicator 10.0.13.244/24 md5
- host replication replicator 10.0.13.245/24 md5
- host all all 0.0.0.0/0 md5
users:
admin:
password: admin
options:
- createrole
- createdb
postgresql:
listen: 10.0.13.243:5432
connect_address: 10.0.13.243:5432
data_dir: /data/patroni
pgpass: /tmp/pgpass
authentication:
replication:
username: replicator
password: replicator_pass
superuser:
username: postgres
password: postgres
parameters:
unix_socket_directories: '.'
tags:
nofailover: false
noloadbalance: false
clonefrom: false
nosync: false现在,模板模块的工作方式与复制模块非常相似,因为它只是将conf文件从ansible服务器移动到目标(在剧本中指定源和目的地)。不同的是,对于其中的每个变量,ansible都填充了信息。这可以来自事实,也可以来自一个文件,在剧本中,作为附加的args添加到您运行的命令甚至在剧本执行过程中被提示中。
在我的示例中,将从目标主机检索ip地址,并在传输时将其插入到conf文件中,并且需要指定desired_name变量,或者通过上面链接的方法指定提示符,或者在最终运行的ansible命令上使用-- either args标志。
https://askubuntu.com/questions/1228781
复制相似问题