【发布时间】:2018-07-22 13:49:18
【问题描述】:
我需要使用 Ansible lineinfile 模块,使其在可变路径上运行。 (这适用于 Ansible 2.5.2。)在此示例中,文件名应取决于实际安装在远程主机上的 PostgreSQL 版本(而不是硬连线版本 9.6):
- lineinfile:
path: /etc/postgresql/9.6/main/postgresql.conf
regexp: '^#?\s*log_connections\s*='
line: 'log_connections = on'
state: present
在bash 我会使用例如获取版本和路径的表达式:
/etc/postgresq/$(pg_lsclusters -h | awk '{print $1}' | head -n 1)/main/postgresql.conf
对于 Ansible 的 lineinfile 模块,它显然不能作为参数 path 逐字使用:
失败了! => {“改变”:假,“味精”:“目的地 /etc/postgresq/$(pg_lsclusters -h | awk '{print $1}' | head -n 1)/main/postgresql.conf 不存在!", "rc": 257}
所以我的问题是:在这个用例中如何使用 Ansible 形成变量路径?
【问题讨论】: