简体   繁体   English

ansible 中有没有办法让我的 python 脚本获取所有定义的变量而不将它们全部传入?

[英]is there a way in ansible for my python script to get all the variables that are defined without passing them all in?

is there a way that myscript.py can have access to all these variables that my playbook has without passing them in from the command line?有没有一种方法可以让 myscript.py 访问我的剧本所具有的所有这些变量,而无需从命令行传递它们? I have a python script that requires about 30 environment variables to render a template.我有一个 python 脚本,它需要大约 30 个环境变量来呈现模板。 I cannot use jinja2 because there is a bug in jinja2 when you use sets and includes.我无法使用 jinja2,因为当您使用集合和包含时 jinja2 中存在错误。 so i had to write a script to do the work.所以我不得不编写一个脚本来完成这项工作。

- name: this is my name
  script: ./roles/jboss/myscript.py --target_hostname "{{ansible_hostname}}" --role_path "{{role_path}}" --is_production "{{is_production}}"  --rel_man "{{release_manifest_file}}" --rm_vendor_type "{{rm_vendor_type}}" --group_name "{{group_names}}"
  args:
    executable: python3
  delegate_to: localhost
  register: build_log_profile_output
  tags: always

I cannot use jinja2 because there is a bug in jinja2 when you use sets and includes我不能使用 jinja2 因为当您使用集合和包含时 jinja2 中存在错误

sounds like your actual question, but the spirit of answering what was asked: I believe the environment is likely the shortest path from where you are to where you want to be:听起来像您的实际问题,但回答问题的精神:我相信环境可能是从您所在的地方到您想去的地方的最短路径:

- name: this is my name
  script: ./roles/jboss/myscript.py
  args:
    executable: python3
  environment:
    ANSIBLE_JSON: '{{ vars | to_json }}'
  delegate_to: localhost
  register: build_log_profile_output

then in your script:然后在你的脚本中:

import json
import os
vars = json.loads(os.getenv('ANSIBLE_JSON'))

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

相关问题 有没有办法在python脚本中获取所有变量值? - Is there a way to get all values of variables in a python script? 如何在不导入的情况下获取python模块中定义的所有变量? - How to get all of the variables defined in a python module without imports? 无法定义所有变量 - Unable to get all variables defined 有没有办法在python中执行文件中的所有函数而不显式调用它们? - Is there a way in python to execute all functions in a file without explicitly calling them? Ansible:在自己的模块中获取所有变量吗? - Ansible: get all variables in own module? Python:如何获取在另一个模块中定义的所有局部变量 - Python: How to get all of the local variables defined in another module 如何获取python中function中定义的所有局部变量? - How to get all the local variables defined inside the function in python? 有什么方法可以获取所有可能的 Python 类变量,包括没有值的变量? - Any way to get all possible Python class variables, including ones without values? 我的 python 脚本在生成错误时,即使所有变量都是 int - My python script in generating a error, even if all variables are int 是否有一种简洁的方法来获取python类中定义的所有描述符? - Is there a succint way to get all the desciptors defined in a python class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM