【问题标题】:Ansible module lineinfile with variable path具有可变路径的 Ansible 模块 lineinfile
【发布时间】: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 形成变量路径?

【问题讨论】:

    标签: bash ansible


    【解决方案1】:

    这似乎工作正常:

    - name: Got it!
      command: bash -c "pg_lsclusters -h | awk '{print $1; exit}'" 
      register: version
    - set_fact: version='{{version.stdout}}'
    - lineinfile:
        path: "/etc/postgresql/{{version}}/main/postgresql.conf"
        regexp: '^#?\s*log_connections\s*='
        line: 'log_connections = on'
        state: present
    

    【讨论】:

    • 我现在改用command: ls /etc/postgresql/。它更简单(也足够好),并且具有即使PostgreSQL服务未启动也可以确定版本的优点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 2021-08-14
    • 2020-09-09
    • 2019-12-28
    相关资源
    最近更新 更多