Now i have setup a python virtual env and want to invoke the playbook using python:
#!/opt/raka/ansible_python27/.venv/bin/python
from __future__ import print_function
import argparse
import os
import subprocess
import shlex
import yaml
command = 'sudo ANSIBLE_CONFIG=/opt/raka/ansible_python27/ansible_config/ansible.cfg ANSIBLE_FORCE_COLOR=true ANSIBLE_DIFF_CONTEXT=0 /opt/raka/ansible_python27/.venv/bin/ansible-playbook /home/raka/test.env.yml'
print('Running Playbook: ', command)
subprocess.call(shlex.split('sudo ANSIBLE_CONFIG=/opt/raka/ansible_python27/ansible_config/ansible.cfg ANSIBLE_FORCE_COLOR=true ANSIBLE_DIFF_CONTEXT=0 /opt/raka/ansible_python27/.venv/bin/ansible-playbook /home/raka/test.env.yml'))
Now when i execute the python script using sudo i get root user as SUDO_USER
sudo ./test.py
Running Playbook: sudo ANSIBLE_CONFIG=/opt/raka/ansible_python27/ansible_config/ansible.cfg ANSIBLE_FORCE_COLOR=true ANSIBLE_DIFF_CONTEXT=0 /opt/raka/ansible_python27/.venv/bin/ansible-playbook /home/rparida1/test.env.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *****************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************
ok: [localhost]
TASK [debug] *********************************************************************************************************************************************************
ok: [localhost] => {
"msg": "root is an environment variable"
}
PLAY RECAP ***********************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
But when i execute it without sudo i get proper results.
./test.py
Running Playbook: sudo ANSIBLE_CONFIG=/opt/raka/ansible_python27/ansible_config/ansible.cfg ANSIBLE_FORCE_COLOR=true ANSIBLE_DIFF_CONTEXT=0 /opt/raka/ansible_python27/.venv/bin/ansible-playbook /home/rparida1/test.env.yml
[WARNING]: No inventory was parsed, only implicit localhost is available
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'
PLAY [localhost] *****************************************************************************************************************************************************
TASK [Gathering Facts] ***********************************************************************************************************************************************
ok: [localhost]
TASK [debug] *********************************************************************************************************************************************************
ok: [localhost] => {
"msg": "rparida1 is an environment variable"
}
PLAY RECAP ***********************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
Kindly let me know how can i extract the proper value of SUDO_USER using sudo