从 ansible 中的输出生成 Jinja2 中的模板

发布于 2025-01-10 14:50:06 字数 2572 浏览 0 评论 0原文

这是我的问题:需要从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心碎的声音 2025-01-17 14:50:07

如果我理解正确的话,您从 yaml 文件中提取数据,并想要创建一个包含使用 jinja2 模板化的数据的文件。

我建议不要使用模板模块,而是使用 blockinfile 模块与模板查找相结合,如下所示:

- name: jinja2
  blockinfile:
    create: true
    block: "{{ lookup('template', 'template.j2') }}"
    path: /ansible/VRF_STATUS.txt

这样,模板化值将附加到文件中。如果您没有配置如何处理标记,您的文件中将会有一些“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:

- name: jinja2
  blockinfile:
    create: true
    block: "{{ lookup('template', 'template.j2') }}"
    path: /ansible/VRF_STATUS.txt

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文