gpt4 book ai didi

ansible - 在 Ansible 中使用模板模块附加文件

转载 作者:行者123 更新时间:2023-12-04 16:38:26 25 4
gpt4 key购买 nike

所以我有一个使用 Jinja2 模板创建日志文件的 ansible 剧本。每次我运行剧本时,它都会从customers.yml 中提取客户信息并将完成的模板输出到“stunnel.conf”文件中。该模板工作正常,但我试图找到一种方法来附加以前的“stunnel.conf”,而不是使用模板模块覆盖它。我希望手动将文本添加到 'stunnel.conf' 的开头并且不被覆盖。你认为这可能吗?

Stunnel.conf

; GFAM - PBSTP
[customer-GFAM-34074]
cert = /etc/stunnel/stunnel.pem
accept = 34094
connect = 35094

; GUANFABANK - FXSIM
[customer-GUANFABANK-34051]
cert = /etc/stunnel/stunnel.pem
accept = 34095
connect = 35095

; ONEZERO2 - TRADESTREAM
[customer-ONEZERO2-39124]
cert = /etc/stunnel/stunnel.pem
accept = 34096
connect = 35096

; BTG-VELOCITY - PBSTP
[customer-BTG-VELOCITY-42533]
cert = /etc/stunnel/stunnel.pem
accept = 34097
connect = 35097

Jinja2 模板
{#CONTEXT: {{ customers }}#}
{% set currentport = 34093%}
{% for cust, config in customers.items() %}
; {{ cust }} - {{ config['type'] }}
[customer-{{ cust }}-{{ config['accept'] }}]
cert = {{ "/etc/stunnel/stunnel.pem" }}
{#accept = {{ config['accept'] }}#}
{#connect = {{ config['connect'] }}#}
accept = {{ currentport + 1 }}
connect = {{ currentport + 1001 }}
{% set currentport = currentport + 1 %}

{% endfor %}

剧本.yml
- include_vars:
file: /home/vagrant/stunnelSimAnsPractice/roles/ns16/vars/customers.yml
name: customers

- template:
src: /home/vagrant/stunnelSimAnsPractice/roles/ns16/templates/stunnel.j2
dest: /home/vagrant/stunnelSimAnsPractice/roles/ns16/output/stunnel.conf
owner: root
group: root

最佳答案

您可以使用blockinfile模块和 template在 stunnel.conf 中查找以管理每个客户端的 block :

- include_vars:
file: customers.yml
name: customers

- blockinfile:
dest: stunnel.conf
block: "{{ lookup('template', 'stunnel.j2') }}"
marker: "; {mark} ANSIBLE MANAGED BLOCK FOR {{ cust }}"
为了便于阅读,我缩短了文件路径。
这样,Ansible 将查找特定客户端的托管 block ( {{ cust }} 变量)并使用模板化 stunnel.j2 中的内容添加/替换。

关于ansible - 在 Ansible 中使用模板模块附加文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46366526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com