How to get the "{{ playbook_dir }}" variable directly in the python source code without passing as option

208 views
Skip to first unread message

Sayed Anisul

unread,
Jul 31, 2018, 10:08:33 AM7/31/18
to Ansible Project
Hello !

Ansible has a variable "{{ playbook_dir }}" that gives the playbook directory path. What I understand is, while running the Ansible play, the sources related to Ansible playbook are copied to a temporary location for example:
/root/.ansible/tmp/ansible-tmp-1532003188.07-226176824704061/ and it is run from there. So, currently, for one of my method where I need to read a file, I need to pass "{{ playbook_dir }}" as a parameter while running the play, source code: https://gist.github.com/anis016/1c0a54a9fea7283ece474d0727ddb879#file-hive-py-L350 (Also, please check the play.yml below).
Is there any other way from where I can get the Playbook directory path directly and can use in my python source code without passing as a separate option?


play.yml
---
- name: hive playbook
  hosts: yum_hosts
  tasks:
  - name: Install yum requirements
    become: yes
    raw: yum install -y gcc openssl-devel python-devel libffi libffi-devel cyrus-sasl-devel

  - name: Install hive and kerberos modules
    become: yes
    pip:
      name: "{{ item }}"
      extra_args: --trusted-host files.pythonhosted.org --trusted-host pypi.org --trusted-host pypi.python.org
    with_items:
      - thrift_sasl==0.2.1
      - impyla

  - name: execute a DDL statement from a HQL file.
    hive:
      operation: file
      parameters: "--outputformat=csv2 --showHeader=false -f sample.hql --hivevar environment=default"
      playbook_path: "{{ playbook_dir }}"
      quorum: "sandbox.hortonworks.com:2181"
    register: result_execute

  - name: dump
    debug:
      msg: '{{ result_execute }}'


ANSIBLE VERSION
ansible 2.5.5
  config file = None
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /root/python_workspace/ansible-hive/venv_hive/lib/python2.6/site-packages/ansible
  executable location = /root/python_workspace/ansible-hive/venv_hive/bin/ansible
  python version = 2.6.6 (r266:84292, Aug 18 2016, 15:13:37) [GCC 4.4.7 20120313 (Red Hat 4.4.7-17)]

OS / ENVIRONMENT

Linux sandbox.hortonworks.com 4.11.0-1.el7.elrepo.x86_64 #1 SMP Mon May 1 09:01:59 EDT 2017 x86_64 x86_64 x86_64 GNU/Linux



EXPECTED RESULTS

Should be able to access "{{ playbook_dir }}" directly in the python source code without passing as the options.



ACTUAL RESULTS

Currently, need to pass "{{ playbook_dir }}" as an option to access from python source. please check the play.yml file above.


Thank you

Reply all
Reply to author
Forward
0 new messages