【问题标题】:Lookup inside lookup - ansible在查找内部查找 - ansible
【发布时间】:2018-09-05 16:40:42
【问题描述】:

我正在尝试使用查找插件从查找文件的查找函数中查找环境变量。

所以文件名为 _hosts.txt,我希望查找函数将 ENV 替换为传递的环境变量。

我查看了 ansible 文档进行查找,但仍然无法找出错误。

这里是代码块:

- name: "Update the /etc/hosts file"
  blockinfile:
   block: "{{ lookup('file', ' + lookup('env', 'ENV') +_hosts.txt') }}"
   dest: "/etc/hosts"
   backup: yes

输出:

失败了! => {“msg”:“模板字符串时模板错误:预期 令牌',',得到'env'。字符串:{{查找('文件','+查找('env', 'ENV') +_hosts.txt') }}"}

我知道这是一个语法问题,但就是不知道它是什么。

【问题讨论】:

    标签: ansible


    【解决方案1】:

    使用辅助变量:

    - name: "Update the /etc/hosts file"
      blockinfile:
        block: "{{ lookup('file', filename) }}"
      vars:
        filename: "{{ lookup('env', 'ENV') }}_hosts.txt"
    

    或者你可以写成一行:

    block: "{{ lookup('file', lookup('env', 'ENV') + '_hosts.txt' ) }}"
    

    【讨论】:

    • 使用辅助变量对我有用。谢谢你。我想知道为什么查找内部的查找不起作用。感谢您的帮助!
    • 引号不匹配。我还认为需要嵌套引号,但第二次看你可以将它写在一行中。
    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 2021-02-24
    • 1970-01-01
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-05
    • 1970-01-01
    相关资源
    最近更新 更多