需要 ansible 帮助

需要 ansible 帮助

我有一个 ansible 项目,可以自动安装 Patroni HA 集群。问题出在 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”行。我该如何循环执行此脚本?

答案1

您可以使用以下方式实现此目的ansible 的模板功能。基本上,您复制 .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 都会填写信息。这可以来自事实,也可以来自剧本中的文件,作为您运行的命令的附加参数添加,甚至在剧本执行期间提示

在我的示例中,将在传输时从目标主机检索 ip 地址并将其插入到 conf 文件中,并且需要指定 desire_name 变量,可以通过上面链接的方法使用提示,也可以使用最终运行的 ansible 命令上的 --extra-args 标志。

相关内容