Hi
I have a Ansible playbook as below:
test.env.yml
---
- hosts: localhost
tasks:
- debug: msg="{{ lookup('env','SUDO_USER') }} is an environment
variable"
Now i have setup a python virtual env and want to invoke the playbook using
python:
my Python script is as follows:
#!/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
--
You received this message because you are subscribed to the Google Groups
"Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/ansible-project/79fcc5fd-79c0-49dd-9882-fe639d42bdff%40googlegroups.com.