通过剧本替换外部文件中的变量

Replacing variables in external file through playbook

我想使用变量 (vars.yaml) 来替换配置文件 (config.yaml) 中的内容,并在 playbook (playbook.yaml) 中自动执行该过程。但是我找不到一种通过 playbook.yaml.

自动搜索外部文件中的变量的过程

我的文件如下所示: vars.yaml

---
var1: content1
var2: content2
var3: content3

config.yaml:

apiVersion: 1
 
datasources:
  - name: {{ var1 }}
    type: {{ var2 }}
    access: proxy
    url: {{ var3 }}

playbook.yaml:

---
- name: Deploy
  hosts: localhost
  tasks:
    - include_vars: ./vars.yaml

    - name: Update config file
      # Something to replace all vars in the config file using the content of vars.yaml

你能帮我告诉我是否有什么可能而不是为每个变量做一个正则表达式......?

带有未解析变量的文件实际上是一个template。就这样命名吧

shell> cat config.yaml.j2
apiVersion: 1

datasources:
  - name: {{ var1 }}
    type: {{ var2 }}
    access: proxy
    url: {{ var3 }}

使用模板模块更新config.yaml,例如

    - include_vars: vars.yaml
    - name: Update config file
      template:
        src: config.yaml.j2
        dest: config.yaml

给予

shell> cat config.yaml
apiVersion: 1

datasources:
  - name: content1
    type: content2
    access: proxy
    url: content3