问题描述
有没有一种方法,myscript.py可以访问我的剧本中的所有这些变量而无需从命令行传递它们?我有一个python脚本,需要大约30个环境变量来呈现模板。我不能使用jinja2,因为在使用set和include时jinja2中存在错误。所以我不得不写一个脚本来完成这项工作。
- 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
解决方法
我不能使用jinja2,因为当您使用集合和包含时jinja2中存在一个错误
听起来像您的实际问题,但回答问题的精神是:我相信环境可能是您从身处到想要成为的最短路径:
- 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
然后在您的脚本中:
import json
import os
vars = json.loads(os.getenv('ANSIBLE_JSON'))