使用厨师食谱识别 PostgreSQL 版本

Identify PostgreSQL version using chef recipes

是否可以在 chef 的食谱中识别数据库服务器上 运行 的 PostgreSQL 版本。

现在我可以使用 awk 命令识别它

示例:

[postgres@hostname]$ ps -aux  | grep postgres
psharm89 39532  0.0  0.0 112644   960 pts/2    S+   12:31   0:00 grep -- color=auto postgres
postgres 56958  0.0  0.9 2345396 77444 ?       S    May21   1:10 /opt/postgres/9.5/bin/postgres -D /opt/pgdata/9.5/data
 postgres 56959  0.0  0.0 166416  1836 ?        Ss   May21   0:57 postgres: logger process
 postgres 56961  0.0  2.1 2347908 173768 ?      Ss   May21   0:45 postgres: checkpointer process
 postgres 56962  0.0  0.2 2345412 21096 ?       Ss   May21   0:14 postgres: writer process
 postgres 56963  0.0  0.2 2345352 18284 ?       Ss   May21   0:25 postgres: wal writer process
 postgres 56964  0.0  0.0 2345788 2820 ?        Ss   May21   1:14 postgres: autovacuum launcher process
 postgres 56965  0.0  0.0 168668  2156 ?        Ss   May21   2:22 postgres: stats collector process

[postgres@hostname]$ PGHOME=$(ps -aux  | grep postgres | grep -- -D |grep -v grep |awk '{NF=NF-2;print $NF}'|awk -F"/bin" '{print }')
[postgres@hostname]$ PGVER=$(/bin/basename $PGHOME)
[postgres@hostname]$ echo $PGVER
 9.5

但是,同样我需要在 chef 的食谱中找到答案,这样我就可以创建变量 PGVER=9.5 并在我的 postgresql 数据库配置食谱中使用它。

这通常不是您使用 Chef 的方式。 Chef 将强制执行正在使用的 Postgres 版本。

但如果你必须,你可以使用这样的东西:

pgver = shell_out!('ps -aux').stdout[%r{postgres/(9\.\d+)/bin}, 1]

这主要只是将字符串解析移动到 Ruby 而不是 Bash,但基本思想相同。