Discussion:
[ansible-project] Use IP in hosts as variables in roles
Alexandre Grais
2015-07-27 22:05:34 UTC
Permalink
Hi Ansible guys,

I am using ansible to provision my django application composed of:
- A Django Webserver on (Machine A IP : 1.1.1.1)
- A PostgreSQL Database (Machine B IP: 2.2.2.2)
- RabbitMQ Server (Machine C)
- Celery Woekers (Machine D)

[database]
2.2.2.2

[django]
1.1.1.1



The main question I have is the following:
I'm using ansible to generate my settings.py for production. In this file,
we need to define the database such as:
########## DATABASE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'toto', # To be compliant with automatic exports from
production
'USER': 'postgres',
'PASSWORD': '',
'HOST': '2.2.2.2', # MACHINE B IP ADRESS
'PORT': '5432',
}
}
########## END DATABASE CONFIGURATION

Right now, I am hard coding the IP of my database in the settings.py file.
But I don't feel confortable with this approach. What if I need to deploy
an other environment ? I was wondering how I could get dynamically the IP
of my database server and use it as a variable in my differents templates.

If you have any ideas about how to proceed, I would be really happy to hear
from you.

Regards.
--
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 ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/95926e56-a34c-4f1b-bf85-be10e990cc3a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
J Hawkesworth
2015-07-29 05:25:03 UTC
Permalink
Have a look at:

http://docs.ansible.com/ansible/faq.html#how-do-i-loop-over-a-list-of-hosts-in-a-group-inside-of-a-template
and
http://docs.ansible.com/ansible/faq.html#how-do-i-access-a-variable-of-the-first-host-in-a-group
I think this might get you pretty close to what you want.

Jon
Post by Alexandre Grais
Hi Ansible guys,
- A Django Webserver on (Machine A IP : 1.1.1.1)
- A PostgreSQL Database (Machine B IP: 2.2.2.2)
- RabbitMQ Server (Machine C)
- Celery Woekers (Machine D)
[database]
2.2.2.2
[django]
1.1.1.1
I'm using ansible to generate my settings.py for production. In this file,
########## DATABASE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'toto', # To be compliant with automatic exports from
production
'USER': 'postgres',
'PASSWORD': '',
'HOST': '2.2.2.2', # MACHINE B IP ADRESS
'PORT': '5432',
}
}
########## END DATABASE CONFIGURATION
Right now, I am hard coding the IP of my database in the settings.py file.
But I don't feel confortable with this approach. What if I need to deploy
an other environment ? I was wondering how I could get dynamically the IP
of my database server and use it as a variable in my differents templates.
If you have any ideas about how to proceed, I would be really happy to
hear from you.
Regards.
--
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 ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/28720485-380e-41f8-81c8-861898ae445f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...