Hi ,
I am using ansible python api(being called from a pecan application running in a apache2 server)
i am trying to set os.environ[‘ANSIBLE_CONFIG’] = “/etc/differentlocation/ansible.cfg” but it doesnt work .
i need this custom location to work since there might be more than 1 application on the same server which use ansible for various reasons. I am using virtual env in python so keep my deployment isolated and hence dont want to keep ansible.cfg in common location like /etc/ansible or ~/ansible or current directoty
I believe in order for that to work, it would have to be set before any Ansible imports happen.
Once imports start happening, it is likely that ansible.constants will be imported and the config path selected and the constants will be set.
Re-importing or re-evaluating ansible.constants is unlikely to help due to mutability and references pointing to different memory locations.
I’ve not used pecan before and my Apache experience is old, but could you define the environment var in pecan or Apache instead of in the application code?
Thanks Matt, I will try that. Before trying’s this out i tried declaring the environment variable in apache’s config as shown in this link http://httpd.apache.org/docs/2.2/mod/mod_env.html#setenv
Some thing like SetEnv ANSIBLE_CONFIG /etc/differentlocation/ansible.cfg
But problem is it doesnt set the variable as environment variable directly. I think i need to extract it and then reassign it to the os.environ[‘ANSIBLE_CONFIG’]
I tried it out by assigning os.environ[‘ANSIBLE_
CONFIG’] = “/etc/differentlocation/ansible.cfg” before any imports happen. I did following
1>Before the imports–>> Didnt work
2>From calling code which calls this python file -->> doesnt work
I dont have write access on this location so i am sort of blocked right now
/home/badger/ansible.cfg I just tooks the default and set hostfile:
hostfile = /home/badger/hosts
In /home/badger/hosts I just had a host listed that I didn't have in
my system hosts.
tester ansible_ssh_host=127.0.0.9
$ python2
import os
os.environ['ANSIBLE_CONFIG']='/srv/ansible/ansible-friday/bin/ansible.cfg'
from ansible import runner
r = runner.Runner(module_name='command', module_args='hostname', pattern='tester')
r.run()
Hi Toshio
I tried your steps and when i do it from python prompt it works. But when i try it when my application runs on apache(using pecan) it doesnt work. So looks like it is being initialized before it can reach the calling code ?
I am trying to see if i can do something in virtual environment since my pecan project is being deployed as a virtual env within apache
I've never used pecan so I do not know. Applications running from
apache can have some issues with initialization, though. If you're
using mod_wsgi, I've had some success with doing initialization steps
in the wsgi script itself in a past life. However, I think you might
run into issues as it sounds like you want different requests to have
different initialization values and wsgi tries to make python web
applications long running processes. On the other hand, having
different requests served by different wsgi/apache processes may work.
I think I've had success with that strategy before.
Yeah we don’t really support API usage more so than “what easily works already works”.
We typically would recommend Ansible Tower for REST API triggering as the internals of the API methods are subject to change.
This is to not say you can’t do these things - just to say it’s not a primary consideration.
That all being said, it should be fine to set the OS environment variable like what Toshio has - -it could be your way of invoking it is clearing the environment or you are loading it first.
I do not recommend invoking playbooks from Apache jobs and leaving them hanging out there - Tower is a MUCH better way to trigger and poll for status in that case.