首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要帮助解决问题

需要帮助解决问题
EN

Ask Ubuntu用户
提问于 2020-04-20 10:39:37
回答 1查看 88关注 0票数 1

我有一个自动安装Patroni集群的ansible项目。patroni.conf文件中存在问题。

每个主机都需要不同的配置。

例如:

主机1配置:

代码语言:javascript
复制
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会议:

代码语言:javascript
复制
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“行中替换它。我如何循环这个脚本呢?

EN

回答 1

Ask Ubuntu用户

发布于 2020-06-21 19:31:18

您可以使用无源模板特征完成这一任务。基本上,您将获取.conf文件的副本,并使用jinja2格式化变量替换需要动态定义的任何值。看起来可能是这样的:

代码语言:javascript
复制
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标志。

票数 0
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/1228781

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档