Reputation: 764
I am using ansible 2.1. Most webserver's has one IP only, but some server's has two IPs. Each webserver has special website for internal usage, i have special variable to define which IP is used for this site: if INTERNALWEBSITE_LISTEN_IP is null - used main IP, if not - custom IP from host_vars.
content of roles/web_std/defaults/main.yml
:
r_web_std:
INTERNALWEBSITE_LISTEN_IP: null
content on inventory/host_vars/100.200.1.2
:
r_web_std:
INTERNALWEBSITE_LISTEN_IP: "100.200.1.4"
(this host has two IP: 100.200.1.2 and 100.200.1.4, and 100.200.1.2 is default IP)
and here is fragment from server.conf.j2
:
DocumentRoot /www/{% if r_web_std.INTERNALWEBSITE_LISTEN_IP == null %}{{ ansible_default_ipv4.address }}{% else %}{{ r_web_std.INTERNALWEBSITE_LISTEN_IP }}{% endif %}/www
ErrorLog /var/log/httpd/{% if r_web_std.INTERNALWEBSITE_LISTEN_IP == null %}{{ ansible_default_ipv4.address }}{% else %}{{ r_web_std.INTERNALWEBSITE_LISTEN_IP }}{% endif %}/error.log
CustomLog "/var/log/httpd/{% if r_web_std.INTERNALWEBSITE_LISTEN_IP == null %}{{ ansible_default_ipv4.address }}{% else %}{{ r_web_std.INTERNALWEBSITE_LISTEN_IP }}{% endif %}/access.log" combined
My eyes!
Is it possible to simplify this?
{% if r_web_std.INTERNALWEBSITE_LISTEN_IP == null %}
{{ ansible_default_ipv4.address }}
{% else %}
{{ r_web_std.INTERNALWEBSITE_LISTEN_IP }}
{% endif %}
Upvotes: 1
Views: 349
Reputation: 60029
I like @Kyles approach. But for completeness, you can also use the default
filter:
{{ r_web_std.INTERNALWEBSITE_LISTEN_IP | default(ansible_default_ipv4.address) }}
Upvotes: 2
Reputation: 4455
Try changing roles/web_std/defaults/main.yml
to use
r_web_std:
INTERNALWEBSITE_LISTEN_IP: "{{ ansible_default_ipv4.address }}"
and just use INTERNALWEBSITE_LISTEN_IP
everywhere that needs it?
Upvotes: 2