简体   繁体   中英

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? I have a python script that requires about 30 environment variables to render a template. I cannot use jinja2 because there is a bug in jinja2 when you use sets and includes. 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

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'))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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