简体   繁体   English

如何将字典列表从Python脚本导入Ansible剧本?

[英]How do I import List of dictionaries from Python script into Ansible playbook?

I have a python variable that is defined in a seperate python script formatted like so - 我有一个在单独的python脚本中定义的python变量,其格式如下:

site_list = [   {'name': "MyTestSite",
                 'alias': 'site1',
                 'website_url':  ['website1.test.com' ,'website1.othertest.com'],
                 'projectname':'Testsite',
                 'id':'12345678'},

                {'name': '"OtherTestSite"',
                 'alias': 'foobar',
                 'website_url':  ['foobar.test.com' ,'foobar2.test.com'],
                 'projectname':'foobar',
                 'id':'98765432'},

                 ... (lots of other entries)

I'm looking to try get the variable into Ansible and use it there, however it seems like I would have to rewrite the variable to be in a .yml format (based on https://docs.ansible.com/ansible/latest/modules/include_vars_module.html ), rather than be able to just import the python script and use it from there. 我正在尝试将变量放入Ansible并在其中使用它,但是似乎我必须将变量重写为.yml格式(基于https://docs.ansible.com/ansible/latest /modules/include_vars_module.html ),而不是只能导入python脚本并从那里使用它。

Is it possible to just import the python variable as it is, rather than rewriting it using yml and putting it into the group_vars in the Ansible directory? 是否可以按原样导入python变量,而不是使用yml重写并将其放入Ansible目录中的group_vars中?

The only other way I could see retreiving that variable would be to print it inside of the Python script, call the Python script from Ansible, and then store the output as a variable (but that solution seems kinda clunky to me). 我能看到的唯一的其他方法是将该变量打印到Python脚本内,从Ansible调用Python脚本,然后将输出存储为变量(但是这种解决方案在我看来有点笨拙)。

Thanks. 谢谢。

I suggest using lookup plugin pipe and python file that dump variable to json: 我建议使用将变量转储到json的查找插件管道和python文件:

Python file vars.py: Python文件vars.py:


import json

initial = [{'name': "MyTestSite",
                 'alias': 'site1',
                 'website_url':  ['website1.test.com' ,'website1.othertest.com'],
                 'projectname':'Testsite',
                 'id':'12345678'},

                {'name': '"OtherTestSite"',
                 'alias': 'foobar',
                 'website_url':  ['foobar.test.com' ,'foobar2.test.com'],
                 'projectname':'foobar',
                 'id':'98765432'}]


print(json.dumps(initial))

And playbook code: 和剧本代码:

---
- name: Test command
  hosts: localhost
  tasks:
  - name: Load variable to fact
    set_fact:
      the_var: "{{ lookup('pipe', 'python vars.py') | from_json }}"
  - name: Test command
    debug:
      msg: "{{ item }}"
    with_list: "{{ the_var }}"

Don't forget upload file to nodes before use it. 使用文件之前,请不要忘记将文件上传到节点。

Tested with ansible 2.7.6 经过ansible 2.7.6测试

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

相关问题 如何使用 Ansible Playbook 导入 python 文件? - How do I import a python file using Ansible Playbook? Unbale 从 Ansible playbook 运行 python 脚本 - Unbale to run python script from Ansible playbook 创建主机列表以便传递给Ansible剧本中的Python脚本 - Creating hosts list in order to pass to Python script in Ansible playbook 如何使用虚拟环境在 ansible 剧本中运行 python 脚本? - how to run python script in ansible playbook using virtual enviroment? 如何从 python 控制台将 python 脚本作为模块导入? - How do I import a python script as a module from the python console? 如何从python提示符执行(不导入)python脚本? - How do I execute (not import) a python script from a python prompt? 如何使用 ansible playbook 在 python 中运行 Ansible 模块 - How to run Ansible Module in python with an ansible playbook 如何在python中运行ansible和ansible-playbook - How to run ansible and ansible-playbook in python 有没有办法从 Ansible 剧本调用 Python 脚本并将 output 作为变量再次传递给 Ansible 剧本以进行下一个任务? - Is there any way to call a Python script from an Ansible playbook and pass the output as variable to an Ansible playbook back again for the next task? 如何在 for 循环中直接在 Ansible 剧本中调用 python? - How can I call python directly inside Ansible playbook, in a for loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM