How can I build Ansible with custom Python?

I found a way to pass PYTHONPATH to Ansible based on this StackOverflow answer:

- gather_facts: false
  hosts: localhost              
  tasks:          
    - command: python3 -c 'import yaml'
      environment:
        PYTHONPATH: '{{ ansible_env.PYTHONPATH }}'

By using ansible_env I can access the environment variables of the host from which Ansible is being run, and hence set it for the task. Not pretty, but it does work.

1 Like