从 ansible 中的输出生成 Jinja2 中的模板
这是我的问题:需要从 csv 文件数据中获取数据,并从该数据中运行一个命令并将结果呈现在 jinja2 模板中。问题是,每次我运行剧本时,这都会覆盖我的 jinja2 模板。有谁知道如何不覆盖这个?
name: check vrf configuration
hosts: HOST1
gather_facts: no
tasks:
- name: Read
read_csv:
path: /ansible/data-vlan.csv
register: csvfile_out
- name: Configuring
include: vrf.yml file={item}
with_items: "{{ csvfile_out.list }}"
data-vlan.csv
VRF
CUSTOMER1,
CUSTOMER2,
vrf.yml
- name: Extract Data
set_fact:
VRF: "{{ lookup('csvfile', '{{item.VRF}} file=data-vlan.csv col=0 delimiter=,') }}"
- name: show VRF
ios_command:
commands:
- show run vrf {{VRF}}
- name: jinja2
template:
src: template.j2
dest: /ansible/VRF_STATUS.txt
template.j2
{% for item in status.stdout_lines.0 %}
{% if 'interface' in item %}
no interface {{ item.split().1 }}
{% elif 'vrf context' in item %}
no {{ item }}
{% endif %}
{% endfor%}
输出:
],
"stdout_lines": [
[
"!Command: show running-config vrf CUSTOMER1",
"!Running configuration last done at: Fri Feb 18 11:35:21 2022",
"!Time: Mon Feb 28 11:37:51 2022",
"",
"version 7.0(3)I7(8) Bios:version 07.66 ",
"",
"interface Vlan10",
" vrf member CUSTOMER1",
"",
"interface Vlan12",
" vrf member CUSTOMER1",
"vrf context CUSTOMER1",
" description [CUSTOMER1]",
" ip route 0.0.0.0/0 10.1.1.1",
" address-family ipv4 unicast"
],
],
"stdout_lines": [
[
"!Command: show running-config vrf CUSTOMER1",
"!Running configuration last done at: Fri Feb 18 11:35:21 2022",
"!Time: Mon Feb 28 11:37:51 2022",
"",
"version 7.0(3)I7(8) Bios:version 07.66 ",
"",
"interface Vlan20",
" vrf member CUSTOMER2",
"",
"interface Vlan22",
" vrf member CUSTOMER2",
"vrf context CUSTOMER2",
" description [CUSTOMER2]",
" ip route 0.0.0.0/0 10.2.2.1",
" address-family ipv4 unicast"
],
VRF_STATUS.txt
no interface vlan20
no interface vlan22
no vrf context CUSTOMER2
**- 实际上希望有这样的两个客户:
no interface vlan10
no interface vlan12
no vrf context CUSTOMER1
no interface vlan20
no interface vlan22
no vrf context CUSTOMER2**
有什么想法吗?谢谢!
This is my question: need to take from the csv file data and from this data run one command and present in a jinja2 template the result. The problem is that everytime that I run the playbook, this override my jinja2 template. Does anyone know how to not override this?
name: check vrf configuration
hosts: HOST1
gather_facts: no
tasks:
- name: Read
read_csv:
path: /ansible/data-vlan.csv
register: csvfile_out
- name: Configuring
include: vrf.yml file={item}
with_items: "{{ csvfile_out.list }}"
data-vlan.csv
VRF
CUSTOMER1,
CUSTOMER2,
vrf.yml
- name: Extract Data
set_fact:
VRF: "{{ lookup('csvfile', '{{item.VRF}} file=data-vlan.csv col=0 delimiter=,') }}"
- name: show VRF
ios_command:
commands:
- show run vrf {{VRF}}
- name: jinja2
template:
src: template.j2
dest: /ansible/VRF_STATUS.txt
template.j2
{% for item in status.stdout_lines.0 %}
{% if 'interface' in item %}
no interface {{ item.split().1 }}
{% elif 'vrf context' in item %}
no {{ item }}
{% endif %}
{% endfor%}
output:
],
"stdout_lines": [
[
"!Command: show running-config vrf CUSTOMER1",
"!Running configuration last done at: Fri Feb 18 11:35:21 2022",
"!Time: Mon Feb 28 11:37:51 2022",
"",
"version 7.0(3)I7(8) Bios:version 07.66 ",
"",
"interface Vlan10",
" vrf member CUSTOMER1",
"",
"interface Vlan12",
" vrf member CUSTOMER1",
"vrf context CUSTOMER1",
" description [CUSTOMER1]",
" ip route 0.0.0.0/0 10.1.1.1",
" address-family ipv4 unicast"
],
],
"stdout_lines": [
[
"!Command: show running-config vrf CUSTOMER1",
"!Running configuration last done at: Fri Feb 18 11:35:21 2022",
"!Time: Mon Feb 28 11:37:51 2022",
"",
"version 7.0(3)I7(8) Bios:version 07.66 ",
"",
"interface Vlan20",
" vrf member CUSTOMER2",
"",
"interface Vlan22",
" vrf member CUSTOMER2",
"vrf context CUSTOMER2",
" description [CUSTOMER2]",
" ip route 0.0.0.0/0 10.2.2.1",
" address-family ipv4 unicast"
],
VRF_STATUS.txt
no interface vlan20
no interface vlan22
no vrf context CUSTOMER2
**- Actually would like to have the two customers like that:
no interface vlan10
no interface vlan12
no vrf context CUSTOMER1
no interface vlan20
no interface vlan22
no vrf context CUSTOMER2**
Any idea? thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话,您从 yaml 文件中提取数据,并想要创建一个包含使用 jinja2 模板化的数据的文件。
我建议不要使用模板模块,而是使用 blockinfile 模块与模板查找相结合,如下所示:
这样,模板化值将附加到文件中。如果您没有配置如何处理标记,您的文件中将会有一些“BEGIN”和“END”标记。请参阅 https://docs.ansible.com/ansible/latest /collections/ansible/builtin/blockinfile_module.html 了解更多信息。
if I understand correctly, you extract data from a yaml file and want to create a file containing that data that were templated with jinja2.
I suggest to not use the template module, but the blockinfile module combined with a template lookup like so:
this way, the templated values are appended to the file. if you do not configure on how to handle the markers, you will have some "BEGIN" and "END" markers in your file. see https://docs.ansible.com/ansible/latest/collections/ansible/builtin/blockinfile_module.html for more infos.