这是我的模板,其中包含一些要替换的占位符:
{{ __PLACEHOLDER1__ }} to test.
{{ __PLACEHOLDER2__ }} to test.
{{ __PLACEHOLDER3__ }} to test.
以下是包含这些占位符值的yaml文件:
__PLACEHOLDER1__: placeholder replaced 1
__PLACEHOLDER2__: placeholder replaced 2
__PLACEHOLDER3__: placeholder replaced 3
最后是我的剧本:
- hosts: localhost
become: true
become_method: sudo
become_user: "{{ deployer }}"
vars:
ansible_become_pass: "{{ deployer_pass }}"
root_dir: "."
vars_files:
- "{{ deployer_sec }}"
- "{{ placeholder_vals }}"
tasks:
- name: 'Grab files with placeholders in {{ root_dir }}'
find:
pattern: "*.template"
path: "{{ root_dir }}"
recurse: yes
register: files_with_placeholders
- name: 'Replace placeholders'
template:
src: "{{ item.path }}"
dest: "{{ (item.path | splitext)[0] }}"
with_items: "{{ files_with_placeholders.files }}"
到目前为止很好...但是如果我有这样的模板怎么办:
{{ PLACEHOLDER1 }} to test.
{{ PLACEHOLDER2 }} to test.
{{ PLACEHOLDER3 }} to test.
在运行我的剧本之前,我需要转换模板中的占位符以匹配yaml文件中定义的变量:
{{ PLACEHOLDER1 }} should become {{ __PLACEHOLDER1__ }}
{{ PLACEHOLDER2 }} should become {{ __PLACEHOLDER1__ }}
{{ PLACEHOLDER3 }} should become {{ __PLACEHOLDER1__ }}
在处理模板之前,如何用新的占位符替换原始占位符?