简体   繁体   English

从python中的模板生成yaml文件

[英]Generating yaml file from template in python

I have a python script queries an api and returns a few different values. 我有一个python脚本查询api并返回一些不同的值。 From this I'm trying to generate an ansible playbook with the values inserted from an jinja2 template, which looks like this 通过这种方式,我尝试使用从jinja2模板插入的值生成一个ansible剧本,如下所示

- name: prep change
  hosts: {{ device }}
  gather_facts: no 
  connection: local

  tasks:

  - name: run arista cmd
    eos_command:
     commands: show ip bgp neighbor | i {{ ip }}

device and ip are values returned from the api call in the python script. deviceip是python脚本中api调用返回的值。 Does python allow a jinja2 template be used to create a .yml file? python是否允许使用jinja2模板创建.yml文件?

Appreciate that this is not answering your specific question, but if your python script is for the purposes of collecting the necessary data to generate the playbook, and it is not possible just to incorporate calls to the API directly in your playbook, could you consider turning your python script into an Ansible Inventory script? 意识到这并不能回答您的特定问题,但是如果您的python脚本是为了收集必要数据以生成剧本的目的,并且不可能仅将对API的调用直接包含在剧本中,您是否可以考虑转向您的python脚本转换为Ansible库存脚本?

When you run ansible_playbook.yml -i /path/to/inventory/dir playbook.yml , if the files within the directory are executable, Ansible will execute and accept correctly formatted output as the inventory data for your playbook. 当您运行ansible_playbook.yml -i /path/to/inventory/dir playbook.yml ,如果目录中的文件是可执行文件,则Ansible将执行并接受格式正确的输出作为您的剧本的清单数据。 This way you could make your playbook static, and feed it the data it needs via the dynamic inventory. 这样,您可以将您的剧本设为静态,并通过动态清单向其提供所需的数据。

Docs are here , plus a couple of example blog posts here and here . 文档在这里这里这里还有一些示例博客文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM